其他分享
首页 > 其他分享> > OpenCV基础(五)---图形绘制

OpenCV基础(五)---图形绘制

作者:互联网

图形绘制

OpenCV提供了直线,矩形,圆,椭圆的绘制函数,下面介绍用法

直线绘制:line()

函数原型: 

void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color,
              int thickness = 1, int lineType = LINE_8, int shift = 0);

参数声明:

代码:

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 using namespace cv;
 6  
 7 int main(int argc, char** argv) {
 8 
 9     Mat srcImage = imread("D:/shenjianxin.png");
10     if (!srcImage.data) {
11         cout << "could not load image" << endl;
12         return -1;
13     }
14 
15     Point p1 = Point(200, 300);     
16     Point p2 = Point(400, 300);
17     Scalar color = Scalar(0, 0, 255);
18     line(srcImage, p1, p2, color, 2, 8, 0);
19     imshow("直线绘制", srcImage);
20 
21     waitKey(0);
22     return 0;
23 }

效果图:

矩形绘制:rectangle()

函数原型:

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

函数声明:

代码:

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 using namespace cv;
 6  
 7 int main(int argc, char** argv) {
 8 
 9     Mat srcImage = imread("D:/shenjianxin.png");
10     if (!srcImage.data) {
11         cout << "could not load image" << endl;
12         return -1;
13     }
14 
15     Rect rect = Rect(200, 100, 200, 200);
16     Scalar color = Scalar(255, 0, 0);
17     rectangle(srcImage, rect, color, 2, LINE_8);
18     imshow("矩形绘制", srcImage);
19 
20     waitKey(0);
21     return 0;
22 }

效果图:

圆形绘制:circle()

函数原型:

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

函数声明:

代码:

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 using namespace cv;
 6  
 7 int main(int argc, char** argv) {
 8 
 9     Mat srcImage = imread("D:/shenjianxin.png");
10     if (!srcImage.data) {
11         cout << "could not load image" << endl;
12         return -1;
13     }
14 
15     Scalar color = Scalar(0, 0, 255);
16     Point center = Point(srcImage.cols / 2, srcImage.rows / 2);
17     circle(srcImage, center, 99, color, 2, 8);
18     imshow("圆形绘制", srcImage);
19 
20     waitKey(0);
21     return 0;
22 }

效果图:

椭圆绘制:ellipse()

函数原型:

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

函数声明:

代码:

 1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 using namespace cv;
 6  
 7 int main(int argc, char** argv) {
 8 
 9     Mat srcImage = imread("D:/shenjianxin.png");
10     if (!srcImage.data) {
11         cout << "could not load image" << endl;
12         return -1;
13     }
14 
15     Scalar color = Scalar(0, 255, 0);
16     ellipse(srcImage, Point(srcImage.cols / 2, srcImage.rows / 2), Size(srcImage.cols / 4, srcImage.rows / 4), 0, 0, 360, color, 2, LINE_8);
17     imshow("椭圆绘制", srcImage);
18 
19     waitKey(0);
20     return 0;
21 }

效果图:

标签:srcImage,img,int,thickness,OpenCV,using,图形,include,绘制
来源: https://www.cnblogs.com/zmm1996/p/10664009.html