首页 > 编程语言 >C++,OpenCV图形绘制与文字输出(7)

C++,OpenCV图形绘制与文字输出(7)

时间:2023-04-09 17:32:24浏览次数:41  
标签:连接线 img Point int C++ OpenCV Scalar LINE 绘制

绘线

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0);
//线的样式
enum LineTypes {
    FILLED  = -1,
    LINE_4  = 4, //!< 4-connected line
    LINE_8  = 8, //!< 8-connected line
    LINE_AA = 16 //!< antialiased line
};
/*******************************************************************
*			img: 			绘制在那个图像上
*			pt1:			起点
*			pt2:			终点
*			color:			颜色
*			thickness:		厚度(宽度) 
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

绘圆

void circle(InputOutputArray img, Point center, int radius,const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);
//线的样式
enum LineTypes {
    FILLED  = -1,
    LINE_4  = 4, //!< 4-connected line
    LINE_8  = 8, //!< 8-connected line
    LINE_AA = 16 //!< antialiased line
};
/*******************************************************************
*			img: 			绘制在那个图像上
*			center:			圆心坐标
*			radius:			半径
*			color:			颜色
*			thickness:		厚度(宽度)
*								-1: 	填充圆
*								其他值:  空心
*			lineType:		 线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

绘矩形

void rectangle(InputOutputArray img, Rect rec,const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);
/*******************************************************************
*			img: 			绘制在那个图像上
*			rec:			矩形大小  Rect(x,y,w,h);  
*					x,y:	起始坐标
*					w,h:	宽度和高度
*			color:			颜色
*			thickness:		厚度(宽度)
*								-1: 	填充矩形
*								其他值:  空心矩形
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

绘椭圆

void ellipse(InputOutputArray img, Point center, Size axes,double angle, double startAngle, double endAngle,
const Scalar& color, int thickness = 1,int lineType = LINE_8, int shift = 0);
/*******************************************************************
*			img: 			绘制在那个图像上
*			center:			椭圆圆心
*			axes:			矩形内置椭圆
*			angle:			倾斜角
*			startAngle:		扩展的弧度 0
*			endAngle:		 扩展的弧度  360
*			color:			颜色
*			thickness:		线宽度
*								-1: 	填充矩形
*								其他值:  空心矩形
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

绘制多边形

polylines简单版

void polylines(InputOutputArray img, InputArrayOfArrays pts,bool isClosed, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0 );
/*******************************************************************
*			img: 			绘制在那个图像上
*			pts:			点集
*			isClosed:		是否封闭
*			color:			颜色
*			thickness:		厚度(宽度)
*								-1: 	引发中断
*								其他值:  空心矩形
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

polylines复杂版

void polylines(InputOutputArray img, const Point* const* pts, const int* npts,int ncontours, bool isClosed, const Scalar& color,int thickness = 1, int lineType = LINE_8, int shift = 0 );
/*******************************************************************
*			img: 			绘制在那个图像上
*			pts:			点集
*			npts:			点数目
*			ncontours:	    待绘制折线数
*			isClosed:		是否封闭
*			color:			颜色
*			thickness:		厚度(宽度)
*								-1: 	应发中断
*								其他值:  空心形状
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*********************************************************************/

fillPoly简单版

void fillPoly(InputOutputArray img, InputArrayOfArrays pts,const Scalar& color, int lineType = LINE_8, int shift = 0,Point offset = Point() );
/*******************************************************************
*			img: 			绘制在那个图像上
*			pts:			点集
*			color:			颜色
*			thickness:		厚度(宽度)
*								-1: 	引发中断
*								其他值:  空心矩形
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*			offset: 忽略
*********************************************************************/

fillPoly复杂版

void fillPoly(InputOutputArray img, const Point** pts,const int* npts, int ncontours,const Scalar& color, int lineType = LINE_8, int shift = 0,Point offset = Point() );
/*******************************************************************
*			img: 			绘制在那个图像上
*			pts:			点集
*			npts:			 点数
*			color:			颜色
*			ncontours:		待绘制折线数
*			thickness:		厚度(宽度)
*								-1: 	引发中断
*								其他值:  空心矩形
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			shift: 			坐标点小数点位数(可忽略不写)
*			offset:			
*********************************************************************/

文字输出

void putText( InputOutputArray img, const String& text, Point org,int fontFace, double fontScale, Scalar color,int thickness = 1, int lineType = LINE_8,bool bottomLeftOrigin = false );
/*******************************************************************
*			img: 			绘制在那个图像上
*			text:			绘制文字
*			org:			文本框左下角
*			fontFace:		字体
*			fontScale:		缩放
*			color:			颜色
*			thickness		线宽度
*			lineType:		线的样式
*					FILLED: 线填充的
*					LINE_4:	4邻接连接线
*					LINE_8: 8邻接连接线
*					LINE_AA:反锯齿连接线(高斯滤波)
*			bottomLeftOrigin: 		起点位置
*								true:  左上角  反转倒立显示
*								false: 左下角  正常显示
*********************************************************************/
//opencv 不识别汉字
//fontFace: 字体
enum HersheyFonts {
    FONT_HERSHEY_SIMPLEX        = 0, //!< normal size sans-serif font   //灯芯体
    FONT_HERSHEY_PLAIN          = 1, //!< small size sans-serif font
    FONT_HERSHEY_DUPLEX         = 2, //!< normal size sans-serif font (more complex than FONT_HERSHEY_SIMPLEX)
    FONT_HERSHEY_COMPLEX        = 3, //!< normal size serif font
    FONT_HERSHEY_TRIPLEX        = 4, //!< normal size serif font (more complex than FONT_HERSHEY_COMPLEX)
    FONT_HERSHEY_COMPLEX_SMALL  = 5, //!< smaller version of FONT_HERSHEY_COMPLEX
    FONT_HERSHEY_SCRIPT_SIMPLEX = 6, //!< hand-writing style font
    FONT_HERSHEY_SCRIPT_COMPLEX = 7, //!< more complex variant of FONT_HERSHEY_SCRIPT_SIMPLEX
    FONT_ITALIC                 = 16 //!< flag for italic font
};

综合代码

#include <iostream>
#include <opencv2/opencv.hpp>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std;
using namespace cv;
class Shape 
{
public:
	Shape() :mat(imread("mm.jpg")) {}
	void DrawLine(int x = 0, int y = 0, int xx = 600, int yy = 460) 
	{
		line(mat, Point(x, y), Point(xx, yy), Scalar(0, 0, 255), 2, LINE_AA);
	}
	void DrawCircle(int x = 300, int y = 230, int r = 10) 
	{
		circle(mat, Point(x, y), r, Scalar(0, 255, 0), -1, FILLED);	 //填充圆
		circle(mat, Point(x, y), r+2, Scalar(0, 0, 255), 2, FILLED); //空心圆
	}
	void DrawRectangle(int x = 100, int y = 100, int w = 40, int h = 40) 
	{
		rectangle(mat, Rect(x, y, w, h), Scalar(255, 0, 0), -1, LINE_4);
		rectangle(mat, Rect(x - 1, y - 1, w + 2, h + 2), Scalar(0, 255, 0));
	}
	void DrawEllipse(int x = 400, int y = 200, Size size = { 100,200 }) 
	{
		ellipse(mat, Point(x, y), size, 180, 0, 360, Scalar(255, 0, 0),1);
		ellipse(mat, Point(x, y), size, 90, 0, 360, Scalar(255, 0, 0),-1);
	}
	void DrawText() 
	{
		putText(mat, "opencv test draw shape", Point(50, 50), FONT_ITALIC, 1.0, Scalar(0, 0, 255),
		2,LINE_AA,false);
	}
	void Show(string wName = "shape") 
	{
		imshow(wName, mat);
		waitKey(0);
	}
	void DrawPolygon()
	{
		vector<Point> pixel;
		for (int i = 0; i < 5; i++) 
		{
			pixel.push_back(Point(rand() % 600, rand() % 460));
		}
		//简单版本
		polylines(mat, pixel, true, Scalar(0, 0, 255), 2);
		fillPoly(mat, pixel, Scalar(0, 255, 0), 1);
		
		//复杂版本
		//int size = 5;
		//auto p = pixel.data();
		//polylines(mat, &p, &size, 1, true, Scalar(0, 0, 255),1);
		//const Point** pts = const_cast<const Point**>(&p);
		//fillPoly(mat, pts, &size, 1, Scalar(255, 0, 255),1);
	}
protected:
	Mat mat;
};
int main() 
{
	srand((unsigned int)time(nullptr));
	Shape* pshape = new Shape;
	pshape->DrawLine();
	pshape->DrawCircle();
	pshape->DrawRectangle();
	pshape->DrawEllipse();
	pshape->DrawText();
	pshape->DrawPolygon();

	pshape->Show();
	delete pshape;
	return 0;
}


标签:连接线,img,Point,int,C++,OpenCV,Scalar,LINE,绘制
From: https://blog.51cto.com/u_15959862/6178908

相关文章

  • MordernC++之左值(引用)与右值(引用)
    左值与右值C++中左值与右值的概念是从C中继承而来,一种简单的定义是左值能够出现再表达式的左边或者右边,而右值只能出现在表达式的右边。inta=5; //a是左值,5是右值intb=a; //b是左值,a也是左值intc=a+b; //c是左值,a+b是右值另一种区分左值和右值的方法是:有......
  • C++函数高级
    目录一.函数的默认参数1.默认参数的性质 2.函数默认参数的注意事项二.函数的占位参数三.函数的重载  1.重载的性质和条件(1)修改参数的个数(2)修改参数的类型(3)修改参数的顺序2.函数重载的注意事项 (1)当引用&作为函数参数(2)不可以用函数的默认参数作为重载条件 一.函数的默认参数1.......
  • C++内存对齐
    0x1什么是内存对齐,为什么需要它?尽管内存是以字节为单位,但是大部分处理器并不是按字节块来存取内存的.它一般会以双字节,4字节,8字节,16字节甚至32字节为单位来存取内存,这些存取单位称为内存存取粒度。现在考虑4字节存取粒度的处理器取int类型变量(32位系统),该处理器只能从地址为4......
  • 关于绘制UML
       任何建模语言都以静态建模机制为基础,UNL也不例外。UML的静态建模机制包括用例图、类图、对象图、包图等。用例图从用户的角度描述系统的功能,由用例(usecase)、参与者(actor)以及他们的关系连线组成。用例从用户角度描述系统的行为,他将系统的一个功能描述成一系列的事件......
  • C++伪随机数
    直接上代码吧用的是vs2019#include<iostream>usingnamespacestd;intmain(){ //系统生成随机数//rand()%100生成0~99 srand(time(NULL));//随机数种子,不加这行下一行就是伪随机数 intrandom_num=rand()%100+1;//1-100; //cout<<random_nu......
  • C++ map注意事项
    std::map<int,std::string>map;判断key是否存在时不能使用:std::stringstr=map[9];  //这样不存在时会新增!!!需要这样判断:std::map<int,std::string>::interatoriter;iter=map.find(9);if(iter!=map.end())  //存在else//不存在......
  • C++逆向分析——对象拷贝
    对象拷贝我们通常存储对象,都用数组、列表之类的来存储,那如下所示我们使用数组来存储对象,但是在工作中发现这个数组不够用了,就需要一个更大的数据,但我们重新创建一个数组还需要把原来的数据复制过来;在C语言中可以使用函数来进行拷贝,直接拷贝内存,在C++中实际上跟C语言要做的事情是......
  • C++逆向分析——友元、内部类、命名空间和static
    友元友元可以理解为:朋友、元素;老师认为这个友元是C++中的一个垃圾,因为友元的存在破坏了面向对象的封装性,不推荐使用,之所以有这个章节是因为有人不了解这个概念。注意:在一些新版本的C++编译器里面已经不再提供类似于友元这样的特性了。大家都知道在C++中对象的私有成员,外部是无......
  • C++逆向分析——运算符重载
    运算符重载现在有一个类,其中有一个函数用于比较2个类的成员大小:#include<stdio.h> classNumber{private:intx;inty;public:Number(intx,inty){this->x=x;this->y=y;}intMax(Number&n){returnthis->x>n.x&&this->y>n.......
  • C++逆向分析——模版
    模版假设有一个冒泡排序的函数:voidSort(int*arr,intnLength){inti,k;for(i=0;i<nLength;i++){for(k=0;k<nLength-1-i;k++){if(arr[k]>arr[k+1]){inttemp=arr[k];arr[k]=arr[k+1];arr[k+1]=temp;}}}}但是这个冒泡......