圖形的繪製與填充

opencv中提供了很多繪製圖形的函數,可以方便的進行圖形繪製

 

直線繪製:line()

函數原型: 

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

參數聲明:

  • InputOutputArray img:輸出圖像
  • Point pt1:線段的第一個點
  • Point pt2:線段的第二個點
  • const Scalar& color:直線顏色
  • int thickness = 1:直線粗細程度
  • int lineType = LINE_8:直線類型
  • int shift = 0:點座標的小數點位數

矩形繪製:rectangle()

函數原型:

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

函數聲明:

  • CV_IN_OUT Mat& img:輸出圖像
  •  Rect rec: 矩形的位置和長寬
  •  const Scalar& color:矩形顏色
  • int thickness = 1:線寬
  • int lineType = LINE_8:直線類型
  •  shit:點座標的小數點位數

圓形繪製:circle()

函數原型:

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

函數聲明:

  • img  圖像
  • center 圓心
  • radius 半徑
  • color 顏色
  • thickness 線寬
  • linetype 線型
  • shift 座標點的小數點位數

橢圓繪製: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);

函數聲明:

  • img 圖像
  • center 橢圓原心
  • axes  橢圓x軸長度的一半,y軸長度的一半
  • angle 橢圓旋轉角度
  • startAngle 起始角度
  • endAngle 終止角度
  • color 橢圓顏色
  • thickness 線寬
  • linetype 線型
  • shift 座標小數點位數

示例

 1 #include <iostream>
 2 #include <opencv2/opencv.hpp>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 int main(int argc, char** argv) {
 7     Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
 8     namedWindow("canvas", WINDOW_AUTOSIZE);
 9 
10     //繪製API演示
11     line(canvas, Point(10, 10), Point(400, 400), Scalar(255, 0, 0), 1, 8);
12     Rect rect(100, 100, 200, 200);
13     rectangle(canvas, rect, Scalar(255, 0, 0), 1, 8);
14     circle(canvas, Point(255, 255), 100, Scalar(0, 255, 0), 1, 8);
15     putText(canvas, "hello opencv", Point(100, 50), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0,255,0),2,8);
16     RotatedRect rt;
17     rt.center = Point2f(255, 255);
18     rt.angle = CV_PI / 4;
19     rt.size = Size(100,200);
20     ellipse(canvas, rt, Scalar(0, 255, 255), -1, 8);
21     imshow("canvas", canvas);
22 
23     Mat image = Mat::zeros(Size(512, 512), CV_8UC3);
24     int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
25     RNG rng(12345);
26     int rrg;
27     while (true) {
28         x1 = (int)rng.uniform(0, 512);
29         x2 = (int)rng.uniform(0, 512);
30         y1 = (int)rng.uniform(0, 512);
31         y2 = (int)rng.uniform(0, 512);
32         line(image, Point(x1, y1), Point(x2, y2), 
33             Scalar(rng.uniform(0, 256), rng.uniform(0, 256), rng.uniform(0, 256)), 1, LINE_8);
34         imshow("image", image);
35         char c = waitKey(20);
36         if (c == 27) {
37             break;
38         }
39     }
40     waitKey(0);
41     return 0;
42 }

其中putText用於打印文字

顯示的結果爲:

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章