OpenCV——基本圖形繪制(橢圓、圓、多邊形、直線、矩形)


 1 //繪制橢圓
 2 void DrawEllipse(Mat img, double angle)
 3 {
 4     int thickhness = 2;
 5     int lineType = 8;
 6 
 7     ellipse(img, 
 8         Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), 
 9         Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16),
10         angle, 
11         0, 360, 
12         Scalar(255, 129, 0), 
13         thickhness,
14         lineType);
15 
16 }

 

 1 //繪制實心圓
 2 void DrawFilledCircle(Mat img, Point center)
 3 {
 4     int thickness = -1;
 5     int lineType = 8;
 6 
 7     circle(img,
 8         center,
 9         WINDOW_WIDTH / 32,
10         Scalar(0, 0, 255),
11         thickness,
12         lineType);
13 }

當thickness為其他>0的值時為正常的空心圓

 

 1 void DrawPolygon(Mat img)
 2 {
 3     int lineType = 8;
 4 
 5     Point rookPoints[1][20];
 6     rookPoints[0][0] = Point(WINDOW_WIDTH/4,7* WINDOW_WIDTH/8);
 7     rookPoints[0][1] = Point(3* WINDOW_WIDTH/4,7* WINDOW_WIDTH/8);
 8     rookPoints[0][2] = Point(3* WINDOW_WIDTH/4,13* WINDOW_WIDTH/16);
 9     rookPoints[0][3] = Point(11* WINDOW_WIDTH/16,13* WINDOW_WIDTH/16);
10     rookPoints[0][4] = Point(19* WINDOW_WIDTH/32,3* WINDOW_WIDTH/8);
11     rookPoints[0][5] = Point(3* WINDOW_WIDTH/4,3* WINDOW_WIDTH/8);
12     rookPoints[0][6] = Point(3*WINDOW_WIDTH/4, WINDOW_WIDTH/8);
13     rookPoints[0][7] = Point(26* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
14     rookPoints[0][8] = Point(26* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
15     rookPoints[0][9] = Point(22* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
16     rookPoints[0][10] = Point(22* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
17     rookPoints[0][11] = Point(18* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
18     rookPoints[0][12] = Point(18* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
19     rookPoints[0][13] = Point(14* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
20     rookPoints[0][14] = Point(14* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
21     rookPoints[0][15] = Point(WINDOW_WIDTH/4, WINDOW_WIDTH/8);
22     rookPoints[0][16] = Point(WINDOW_WIDTH/4,3* WINDOW_WIDTH/8);
23     rookPoints[0][17] = Point(13* WINDOW_WIDTH/32,3* WINDOW_WIDTH/8);
24     rookPoints[0][18] = Point(5* WINDOW_WIDTH/16,13* WINDOW_WIDTH/16);
25     rookPoints[0][19] = Point(WINDOW_WIDTH/4,13* WINDOW_WIDTH/16);
26 
27     const Point* ppt[1] = { rookPoints[0] };
28     int npt[] = { 20 };
29 
30     fillPoly(img,
31         ppt,
32         npt,
33         1,//好像只能為1,填其他數程序出錯
34         Scalar(255, 255, 255),
35         lineType);
36 
37 }

 

 

1、cvPolyLine 繪制簡單或多樣的多邊形。
void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed,
                          CvScalar color, int thickness=1, int line_type=8, int shift=0 );
img       圖像。
pts       折線的頂點指針數組。
npts     折線的定點個數數組。也可以認為是pts指針數組的大小
contours   折線的線段數量。
is_closed  指出多邊形是否封閉。如果封閉,函數將起始點和結束點連線。
color         折線的顏色。
thickness  線條的粗細程度。
line_type  線段的類型。參見cvLine。
shift          頂點的小數點位數。

2、cvFillPoly用於一個單獨被多邊形輪廓所限定的區域內進行填充。

函數可以填充復雜的區域,例如,有漏洞的區域和有交叉點的區域等等。
void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours,CvScalar color, int line_type=8, int shift=0 );
img

          圖像。
pts           指向多邊形的數組指針。
npts         多邊形的頂點個數的數組。
contours   組成填充區域的線段的數量。
color         多邊形的顏色。
line_type  組成多邊形的線條的類型。
shift          頂點坐標的小數點位數

 

繪制直線

1 int thickness = 2;
2     int lineType = 8;
3     line(rookImage,//要繪制的圖
4         Point(0, 0),//起始點
5         Point(WINDOW_WIDTH, WINDOW_WIDTH),//終點
6         Scalar(255, 255, 255),
7         thickness,
8         lineType);

 

 

繪制矩形

 

1 rectangle (image3,  rec1,Scalar(0, 0, 255), -1, 8, 0)

 

rectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,

                  int thickness=1, int line_type=8, int shift=0 );
img
圖像.
pt1
矩形的一個頂點。
pt2
矩形對角線上的另一個頂點
color
線條顏色 (RGB) 或亮度(灰度圖像 )(grayscale image)。
thickness
組成矩形的線條的粗細程度。取負值時(如 CV_FILLED)函數繪制填充了色彩的矩形。
line_type
線條的類型。見cvLine的描述
shift
坐標點的小數點位數。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM