Detailed introduction to Opencv drawing

1. When working on projects, it is often used to draw various graphics on the image. For example draw ROI. And often there is a need to write text on top of the image. However, Halcon is lacking in this aspect. Halcon only has few operators such as paint_region to draw graphics on the original image. At this time, the powerful functions of Opencv drawing can come in handy. In practical applications, it is necessary to convert to Mat type drawing first, and then send out the drawn image pointer, or convert to QImage, Hobject, etc.
2. Drawing code

//*******************************************************************
	//Opencv绘图测试
	//本地读取图片,第二个参数0代表读取单通道图,如果改成1代表读取3通道图。
	//此处案例是为了模拟相机输出单通道,然后转换到3通道图之后,利用Opencv画图。
	Mat GrayMat = imread("C:\\Users\\Dell\\Desktop\\11.bmp", 0);
	//创建与上面单通道图相同宽高的3通道图
	cv::Mat RgbMat(GrayMat.rows, GrayMat.cols, CV_8UC3);
	//把单通道图GrayMat利用RGB格式,转换到三通道图RgbMat
	cv::cvtColor(GrayMat, RgbMat, cv::COLOR_GRAY2RGB);
	//下面开始利用转换后的RGB图进行绘制图形
	//绘制矩形
	Point p1(50, 50), p2(500,500);
	rectangle(RgbMat, p1, p2, Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)),15);//颜色随机,线宽15pixel
	Rect rectangle1 = Rect(1000, 1000, 500, 500);
	rectangle(RgbMat, rectangle1, Scalar(255, 0, 0), 25);//颜色蓝色,线宽25pixel
	//绘制圆形
	circle(RgbMat, p2, 100, Scalar(0, 255, 0), 15);//颜色绿色,线宽15
	//绘制椭圆
	ellipse(RgbMat, p2, Size(500,50), 30, 0, 360, Scalar(0, 0, 255), 10);//中心p2,长短轴尺寸(500,50),角度30,起始角度,结束角度360度,颜色红色,线宽10
	//绘制直线
	line(RgbMat, Point(800,800), Point(1500, 1500), Scalar(0, 255, 255),5);//直线端点如上,颜色黄色
	//绘制箭头
	arrowedLine(RgbMat, Point(800, 800), Point(50, 1500), Scalar(255, 255, 0), 5);//箭头端点如上,颜色青色
	//绘制Mark点
	drawMarker(RgbMat, Point(1000, 100), Scalar(255, 0, 255), 0, 20, 2);//Mark点坐标如上,颜色橙色,型号是90度十字交叉线,尺寸20*20,线宽2
	//书写文字
	//void cv::putText(
	//	cv::Mat& img, // 待绘制的图像
	//	const string& text, // 待绘制的文字
	//	cv::Point origin, // 文本框的左下角
	//	int fontFace, // 字体 (如cv::FONT_HERSHEY_PLAIN)
	//	double fontScale, // 尺寸因子,值越大文字越大
	//	cv::Scalar color, // 线条的颜色(RGB)
	//	int thickness = 1, // 线条宽度
	//	int lineType = 8, // 线型(4邻域或8邻域,默认8邻域)
	//	bool bottomLeftOrigin = false // true='origin at lower left'

	cv::String text1("OK,Product is good");
	putText(RgbMat, text1, Point(10, 100), FONT_HERSHEY_PLAIN, 5, Scalar(0, 255, 0), 5, 8, false);
	cv::String text2("NG,Product is Bad");
	putText(RgbMat, text2, Point(10, 200), FONT_HERSHEY_PLAIN, 5, Scalar(0, 0, 255), 5, 8, false);

	cv::imwrite("C:\\Users\\Dell\\Desktop\\12.bmp", RgbMat);
	return 0;

Original image
insert image description here
Result image
insert image description here

Guess you like

Origin blog.csdn.net/Douhaoyu/article/details/128421785