C++,OpenCV图形绘制与文字输出(7)
  Jk5625xsZPHl 2023年11月02日 33 0

绘线

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;
}


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  gBkHYLY8jvYd   2023年12月06日   50   0   0 #includecii++
  lh6O4DgR0ZQ8   2023年11月22日   26   0   0 Memory字段sed
  gBkHYLY8jvYd   2023年12月10日   18   0   0 #include邻域灰度图像
  gBkHYLY8jvYd   2023年12月10日   22   0   0 #include数组i++
  gBkHYLY8jvYd   2023年12月06日   19   0   0 #includeios数据
  gBkHYLY8jvYd   2023年12月08日   20   0   0 #includecii++
  gBkHYLY8jvYd   2023年11月22日   23   0   0 #include十进制高精度
  gBkHYLY8jvYd   2023年11月22日   26   0   0 #includeiosci
Jk5625xsZPHl