OpenCV学习7——绘制形状与文字

  1. 绘制图形与文字需要用到的重要的类及函数:
    ①Point类
    Point(x,y)表示平面上的一个点(x,y)
    ②Scalar类
    Scalar(b,g,r,alpha)表示所绘制图形的色彩向量,这其中有四个分向量。
    ③line()用于画线
    line()函数分析:
    CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8);

    图像参数所代表的含义易知。

    ④ellipse()用于画椭圆
    ellipse()函数分析:

    CV_EXPORTS_W void ellipse(InputOutputArray img,
                              Point center,
                              Size axes,
                              double angle, 
                              double startAngle, 
                     	      double endAngle,
                       	      const Scalar& color,
                        	  int thickness = 1,
                              int lineType = LINE_8);
    

    Size axes()所代表的是椭圆长轴与短轴的距离,其余参数易理解。
    图像参数所代表的含义易知。

    ⑤rectangle()用于画矩形
    rectangle()函数分析:

    CV_EXPORTS void rectangle(CV_IN_OUT Mat& img,
                              Rect rec,
                              const Scalar& color, 
                              int thickness = 1,
                              int lineType = LINE_8,
                              int shift = 0);
    

    Rect rec代表了所画矩形的性质,包括起始点的左边,矩形的长宽各为多少。
    ⑥circle()用于画圆
    circle()函数分析:

    CV_EXPORTS_W void circle(InputOutputArray img, 
    						 Point center, int radius,
                             const Scalar& color,
                             int thickness = 1,
                             int lineType = LINE_8);
    

    ⑦fillPoly()用于填充
    fillPoly()函数分析:见代码中所分析的。
    ⑧RNG函数用于随机画线
    RNG rng(int seed)以种子seed生成随机数
    生成高斯随机数gaussian(double sigma)
    生成正态分布随机数uniform(int a,int b)

  2. 实验代码及实验效果
    ①画线

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);

int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

在这里插入图片描述
②画矩形:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);

void rect_paint(int x, int y, int width, int height, int b, int g, int r);

int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	rect_paint(300,50,280,300,0,255,0);
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

void rect_paint(int x, int y, int width, int height, int b, int g, int r)
{
	Rect rect = Rect(x, y, width, height);
	Scalar color = Scalar(b, g, r);
	rectangle(src, rect, color, 2, LINE_8);
}

在这里插入图片描述
③画椭圆:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);
void rect_paint(int x, int y, int width, int height, int b, int g, int r);
void ellipse_paint();

int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	rect_paint(300,50,280,300,0,255,0);
	ellipse_paint();
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

void rect_paint(int x, int y, int width, int height, int b, int g, int r)
{
	Rect rect = Rect(x, y, width, height);
	Scalar color = Scalar(b, g, r);
	rectangle(src, rect, color, 2, LINE_8);
}

void ellipse_paint()
{
	Scalar color = Scalar(255, 0, 0);
	ellipse(src, Point(src.rows / 2, src.cols / 2), Size(src.rows / 4, src.cols / 16), 0, 0, 360, color, 1, LINE_8);

}

在这里插入图片描述
④画圆

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);
void rect_paint(int x, int y, int width, int height, int b, int g, int r);
void ellipse_paint();
void circle_paint();
int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	rect_paint(300,50,280,300,0,255,0);
	ellipse_paint();
	circle_paint();
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

void rect_paint(int x, int y, int width, int height, int b, int g, int r)
{
	Rect rect = Rect(x, y, width, height);
	Scalar color = Scalar(b, g, r);
	rectangle(src, rect, color, 2, LINE_8);
}

void ellipse_paint()
{
	Scalar color = Scalar(255, 0, 0);
	ellipse(src, Point(src.rows / 2, src.cols / 2), Size(src.rows / 4, src.cols / 16), 0, 0, 360, color, 1, LINE_8);

}

void circle_paint()
{

	Scalar color = Scalar(255, 255, 0);
	circle(src,Point(src.rows/8,src.cols/4),40,color,2,LINE_8);
}

在这里插入图片描述
⑤填充一个多边形:

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);
void rect_paint(int x, int y, int width, int height, int b, int g, int r);
void ellipse_paint();
void circle_paint();
void fillPoly_paint();
int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	rect_paint(300,50,280,300,0,255,0);
	ellipse_paint();
	circle_paint();
	fillPoly_paint();
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

void rect_paint(int x, int y, int width, int height, int b, int g, int r)
{
	Rect rect = Rect(x, y, width, height);
	Scalar color = Scalar(b, g, r);
	rectangle(src, rect, color, 2, LINE_8);
}

void ellipse_paint()
{
	Scalar color = Scalar(255, 0, 0);
	ellipse(src, Point(src.rows / 2, src.cols / 2), Size(src.rows / 4, src.cols / 16), 0, 0, 360, color, 1, LINE_8);

}

void circle_paint()
{

	Scalar color = Scalar(255, 255, 0);
	circle(src,Point(src.rows/8,src.cols/4),40,color,2,LINE_8);
}

void fillPoly_paint()
{
	Point pts[1][5];
	pts[0][0] = Point(100, 100);
	pts[0][1] = Point(100, 200);
	pts[0][2] = Point(200, 200);
	pts[0][3] = Point(200, 100);
	pts[0][4] = Point(100, 100);
	const Point *ppts[] = { pts[0] };
	int npt[] = { 5 };
	Scalar color = Scalar(255, 0, 255);
	fillPoly(src, ppts, npt, 1, color, LINE_8);
}

在这里插入图片描述

对于fillPoly()函数的参数问题:
参考于https://blog.csdn.net/yinkailin/article/details/12406975
fillPoly()函数原型为

void cvFillPoly(CvArr * img, 
		        CvPoint ** pts, 
				int * npts, 
				int contours, 
				CvScalar color, 
				int line_type = 8);

主要讨论参数
CvPoint ** pts,
int * npts,
int contours,
CvPoint **pts表示的是一个Point类型的二级指针,其也是一个数组,指向所要填充的多边形的点,之所以是二级指针是因为我们定义的ppts[x][y]为二维数组,该二维数组的每一行即表示一个将要填充的多边形,因而ppts又指向一个一维数组,该一维数组存储着各个多边形。
int *npts则与pts所对应,npts所指向的数组表示所要填充的多边形内有几个点,因此通过pts与npts这两个指向一个一维数组的指针就唯一确定了所要填充的多边形的个数以及所填充多边形的点的数目,即此时所要填充的多边形即唯一确定。
int contours,contours参数所表示的是对于pts指针所指向的多个多边形,我们所填充的数目的问题
⑥图像中文字的绘制(利用putText()函数来进行绘制)

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

void line_paint(int x1, int y1, int x2, int y2, int b, int g, int r);
void rect_paint(int x, int y, int width, int height, int b, int g, int r);
void ellipse_paint();
void circle_paint();
void fillPoly_paint();
int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	line_paint(0,0,50,50,0,0,255);
	rect_paint(300,50,280,300,0,255,0);
	ellipse_paint();
	circle_paint();
	fillPoly_paint();
	putText(src, "hello,opencv", Point(200, 200), CV_FONT_HERSHEY_COMPLEX, 1.0, Scalar(0, 0, 255), 1, LINE_8);
	namedWindow("paint", CV_WINDOW_AUTOSIZE);
	imshow("paint", src);
	waitKey(0);
	destroyAllWindows();
	return 0;
}

void line_paint(int x1, int y1, int x2, int y2,int b,int g,int r)
{
	Point p1 = Point(x1, y1);
	Point p2 = Point(x2, y2);
	Scalar color = Scalar(b, g, r);
	line(src, p1, p2, color, 2, LINE_8);
}

void rect_paint(int x, int y, int width, int height, int b, int g, int r)
{
	Rect rect = Rect(x, y, width, height);
	Scalar color = Scalar(b, g, r);
	rectangle(src, rect, color, 2, LINE_8);
}

void ellipse_paint()
{
	Scalar color = Scalar(255, 0, 0);
	ellipse(src, Point(src.rows / 2, src.cols / 2), Size(src.rows / 4, src.cols / 16), 0, 0, 360, color, 1, LINE_8);

}

void circle_paint()
{

	Scalar color = Scalar(255, 255, 0);
	circle(src,Point(src.rows/8,src.cols/4),40,color,2,LINE_8);
}

void fillPoly_paint()
{
	Point pts[1][5];
	pts[0][0] = Point(100, 100);
	pts[0][1] = Point(100, 200);
	pts[0][2] = Point(200, 200);
	pts[0][3] = Point(200, 100);
	pts[0][4] = Point(100, 100);
	const Point *ppts[] = { pts[0] };
	int npt[] = { 5 };
	Scalar color = Scalar(255, 0, 255);
	fillPoly(src, ppts, npt, 1, color, LINE_8);
}

在这里插入图片描述
putText()函数分析:

CV_EXPORTS_W void putText(  nputOutputArray img, 
						    const String& text, 
						    Point org,
                  			int fontFace,
                   	    	double fontScale, 
                    		Scalar color,
             			    int thickness = 1, 
                  			int lineType = LINE_8,);

第一个参数为所操作的图像。
第二个参数为希望在图像上绘制的文字。
第三个参数为开始绘制点的坐标。
第四个参数为字体的格式,笔者所学的教程中给笔者指出CV_FONT_HERSHEY_COMPLEX是一般情况下都能使程序正常进行的,笔者又测试了CV_FONT_BLACK发现也可正常运行,只不过是字体格式发生改变。
第五个参数为字体的颜色,通过Scalar()类来进行控制。
第六个参数为字体厚度。
第七个参数为字体划线格式。
⑦随机画线(利用RNG函数)

#include<opencv2/opencv.hpp>
#include<iostream>

using namespace std;
using namespace cv;

Mat src;

int main()
{
	src = imread("C:/Users/he104/Desktop/timg.jpg");
	if (src.empty())
	{
		cout << "could not load the src..." << endl;
		return -1;
	}
	Mat dst;
	dst = Mat::zeros(src.size(), src.type());
	RNG rng(12345);
	for (int i = 0; i < 0xffff; i++)
	{
		Point p1 = Point((rng.uniform(0, src.rows)), (rng.uniform(0, src.cols)));
		Point p2 = Point((rng.uniform(0, src.rows)), (rng.uniform(0, src.cols)));
		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		if (waitKey(50) > 0)break;
		line(dst, p1, p2, color, 2, LINE_8);
		namedWindow("random", CV_WINDOW_AUTOSIZE);
		imshow("random", dst);
	}
	waitKey(0);
	destroyAllWindows();
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值