OpenCV 计算机视觉(六)绘制图形

常用的图形绘制代码即结果如下:

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

using namespace std;
using namespace cv;

int main(int argc, char** argv) {
Mat image = imread("D:/OpenCVprj/image/test3.jpg");
namedWindow("image", CV_WINDOW_AUTOSIZE);
//line
Point p1;
p1.x = image.cols;
p1.y = image.rows;
Point p2 = Point(0, 0);
Scalar color = Scalar(0, 0, 255);
line(image, p1, p2, color, 1, LINE_8);

//rectangle
Scalar color1 = Scalar(0, 255, 0);
Rect rect = Rect(image.cols / 4, image.rows / 4, 2*image.cols / 4, 2*image.rows/4);
rectangle(image, rect, color1, 3, LINE_8);

//ellipse 椭圆
Scalar color2 = Scalar(255, 0, 0);
//90,表示旋转90°, 0,360, 表示画的弧大小, 1表示线粗细
ellipse(image, Point(image.cols/2, image.rows/2), Size(image.cols/4, image.rows/8), 90, 0, 360, color2, 1, LINE_8);

//circle
Scalar color3 = Scalar(255, 255, 0);
circle(image, Point(image.cols/2, image.rows/2), 200, color3, 5, LINE_8);

//fillpoly
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 color4 = Scalar(255, 0, 255);
fillPoly(image, ppts, npt, 1,color4, LINE_8);

//text
Scalar color5 = Scalar(0, 255, 0);
putText(image, "Hello OpenCV", Point(100, image.rows/2), CV_FONT_HERSHEY_COMPLEX, 2, color5);

imshow("image", image);
waitKey(0);
return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/haiboxiaobai/p/11226186.html