天天看点

Opencv入门——添加文字、绘制线段和几何图形等

Point表示一个2D平面的点(x, y)

用法:

Point p;
p.x = 20;
p.y = 10;
//或者
p = Point(20, 10);
           

Scalar表示四个元素的向量

int b = 123;
int g = 45;
int r = 89;
Scalar color = Scalar(b, g, r);
           

画线和各种图形

cv::line (LINE_4\LINE_8\LINE_AA)//画线 

cv::ellipse //画椭圆

cv::rectangle //画矩形

cv::circle //画圆

cv::fillPoly //画填充
           

随机数生成cv::RNG

  1. 生成高斯随机数gaussian (double sigma)
  2. 生成正态分布随机数uniform (int a, int b)

添加文字

putText(img, "Hello Opencv", Point(0, 150), FONT_HERSHEY_TRIPLEX, 1.0, Scalar(50, 20, 250), 1, 8);
//img为需要添加文字的图片,"Hello Opencv"是要添加的字符串,Point(x, y)表示从什么位置写字符串
//FONT_HERSHEY_TRIPLEX为设置的字体,1.0为字体大小,范围(1.0 - 8.0),Scalar(b, g, r)为设定的像素值
           

代码如下:

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

using namespace cv;
using namespace std;

Mat img, dst;

void lines()
{
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 100;
	p2.y = 100;
	Scalar color = Scalar(0, 0, 255);
	line(img, p1, p2, color, 1, LINE_8);
}

void Rectangle()
{
	Rect rect = Rect(50, 50, 150, 150);
	Scalar color = Scalar(255, 0, 255);
	rectangle(img, rect, color, 2, LINE_8);
}

void Ellipse()
{
	Scalar color = Scalar(255, 128, 0);
	//中心,长短轴,旋转度数,从0到360°画椭圆,颜色,线宽,8邻接连接线
	ellipse(img, Point(img.cols / 2, img.rows / 2), Size(img.cols / 4, img.rows / 8), 90, 0, 360, color, 4, LINE_8);
}

void Round()
{
	Scalar color = Scalar(12, 220, 74);
	Point center = Point(img.cols / 2, img.rows / 2);
	circle(img, center, 100, color, 3, 8);
}

void Polygon()
{
	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(22, 55, 88);
	fillPoly(img, ppts, npt, 1, color, 8);
}

void RandomLine()
{
	RNG rng(100000);
	Point pt1, pt2;
	int SIZE = 1000;
	Mat demo = Mat::zeros(SIZE, SIZE, img.type());
	namedWindow("random line image", WINDOW_AUTOSIZE);

	for (int i = 0; i < 100; i++)
	{
		pt1.x = rng.uniform(0, SIZE);
		pt2.x = rng.uniform(0, SIZE);
		pt1.y = rng.uniform(0, SIZE);
		pt2.y = rng.uniform(0, SIZE);

		Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
		if (waitKey(1) > 0)
		{
			break;
		}
		line(demo, pt1, pt2, color, 1, 8);

		imshow("random line image", demo);
	}
	imwrite("D:\\OpenCV\\images\\demo.jpg", demo);
	printf("Drawing completion!");
}

int main(int argc, char **argv)
{
	img = imread("D:\\OpenCV\\images\\9.jpg");
	if (img.empty())
	{
		cout << "image loading failed..." << endl;
		return -1;
	}

	lines();//画线框
	Rectangle();//画矩形
	Ellipse();//画椭圆
	Round();//画圆
	Polygon();//画矩形填充
	putText(img, "Hello Opencv", Point(0, 150), FONT_HERSHEY_TRIPLEX, 1.0, Scalar(50, 20, 250), 1, 8);//添加字符串
	RandomLine();//绘制随机线段,运行一下就知道~ ~ ~

	namedWindow("output", WINDOW_AUTOSIZE);
	imshow("output", img);

	waitKey(0);
	return 0;
}
           

放几张效果图

Opencv入门——添加文字、绘制线段和几何图形等

                                                             RandomLine()

Opencv入门——添加文字、绘制线段和几何图形等

 就到这里,准备睡觉~