学OpenCV

================================================================================

  1 #include <iostream>
  2 #include <opencv2/opencv.hpp>
  3 
  4 using namespace std;
  5 using namespace cv;
  6 
  7 int Test1()
  8 {
  9     Mat img = Mat::zeros(Size(512,512),CV_8UC3);
 10 
 11     circle(img, Point(50, 50), 25, Scalar(255, 255, 255), -1);
 12     circle(img, Point(100, 50), 25, Scalar(255, 255, 255), 4);
 13 
 14     line(img, Point(100, 100), Point(200, 100), Scalar(255, 255, 255), 2, LINE_4, 0);
 15 
 16     ellipse(img, Point(300, 255), Size(100, 70), 30, 0, 90, Scalar(255, 255, 255), -1);
 17     ellipse(img, RotatedRect(Point2f(150, 100), Size2f(30, 20), 30), Scalar(0, 0, 255), 2);
 18     vector<Point> vctPnt;
 19     ellipse2Poly(Point(200, 400), Size(100, 70), 0, 0, 360, 2, vctPnt);
 20     int iSize = vctPnt.size();
 21     for (int i = 0; i < iSize - 1; i++)
 22     {
 23         line(img, vctPnt[i], vctPnt[i + 1], Scalar(255, 0, 0), 2);
 24     }
 25     line(img, vctPnt[iSize - 1 - 1], vctPnt[iSize - 1], Scalar(255, 0, 0), 2);
 26 
 27     rectangle(img, Point(50, 400), Point(100, 450), Scalar(125, 125, 125), -1);
 28     rectangle(img, Rect(400,450,60,50),Scalar(0,125,125),2);
 29 
 30     Point pnt1[6];
 31     pnt1[0] = { 72,200 };
 32     pnt1[1] = { 142,204 };
 33     pnt1[2] = { 226,263 };
 34     pnt1[3] = { 172,310 };
 35     pnt1[4] = { 117,319 };
 36     pnt1[5] = { 15,260 };
 37     Point pnt2[6];
 38     pnt2[0] = { 359,339 };
 39     pnt2[1] = { 447,351 };
 40     pnt2[2] = { 504,349 };
 41     pnt2[3] = { 484,433 };
 42     pnt2[4] = { 418,449 };
 43     pnt2[5] = { 354,402 };
 44     Point pnt3[5];
 45     pnt3[0] = {350,83,};
 46     pnt3[1] = {463,90};
 47     pnt3[2] = {500,171};
 48     pnt3[3] = {421,194};
 49     pnt3[4] = {338,141};
 50     const Point* ppnt[] = { pnt1,pnt2,pnt3 };
 51     int ppntSize[] = { 6,6,5 };
 52     fillPoly(img, ppnt, ppntSize, 3, Scalar(125, 125, 0));
 53 
 54     putText(img, "BLABLABLABLABLA", Point(100, 400), 2, 1, Scalar(0, 125, 125));
 55 
 56     imshow("img", img);
 57 
 58     return 0;
 59 }
 60 
 61 int Test2()
 62 {
 63     Mat img2 = Mat::zeros(Size(512, 512), CV_8UC3);
 64 
 65     vector<Point> vctPnt1;
 66     vctPnt1.push_back({ 72,200 });
 67     vctPnt1.push_back({ 142,204 });
 68     vctPnt1.push_back({ 226,263 });
 69     vctPnt1.push_back({ 172,310 });
 70     vctPnt1.push_back({ 117,319 });
 71     vctPnt1.push_back({ 15,260 });
 72     vector<Point> vctPnt2;
 73     vctPnt2.push_back({ 359,339 });
 74     vctPnt2.push_back({ 447,351 });
 75     vctPnt2.push_back({ 504,349 });
 76     vctPnt2.push_back({ 484,433 });
 77     vctPnt2.push_back({ 418,449 });
 78     vctPnt2.push_back({ 354,402 });
 79     vector<Point> vctPnt3;
 80     vctPnt3.push_back({ 350,83, });
 81     vctPnt3.push_back({ 463,90 });
 82     vctPnt3.push_back({ 500,171 });
 83     vctPnt3.push_back({ 421,194 });
 84     vctPnt3.push_back({ 338,141 });
 85     vector<vector<Point>> vctVctPnt;
 86     vctVctPnt.push_back(vctPnt1);
 87     vctVctPnt.push_back(vctPnt2);
 88     vctVctPnt.push_back(vctPnt3);
 89     fillPoly(img2, vctVctPnt, Scalar(125, 125,0));
 90 
 91     imshow("img2", img2);
 92 
 93     return 0;
 94 }
 95 int main()
 96 {
 97     Test1();
 98     Test2();
 99     waitKey(0);
100 
101     return 0;
102 }