close

  • 繼續上一篇 OpenCV3.1 搜尋輪廓與遮罩應用

  • 現在要計算輪廓周長、面積和重心,並且用一多邊形曲線近似該輪廓。

  •    1:  
       2:     for (int i = 0; i < contours.size(); i++) {
       3:         cout << "contour # " << i << endl;
       4:         imgMask = Mat::zeros(imgSrc.size(), CV_8UC1);
       5:         drawContours(imgMask, contours, i, Scalar(255), -1);
       6:         Mat imgROI;
       7:         imgSrc.copyTo(imgROI, imgMask);
       8:         string name = std::to_string(i);
       9:         //imshow(name, imgROI);
      10:         cout << "mean color (BGR) = " << mean(imgSrc, imgMask) << endl;
      11:         Moments m = moments(contours[i]);
      12:         cout << "area = " << m.m00 <<endl;
      13:         double contArea = contourArea(contours[i]);
      14:         cout << "contour area = " << contArea << endl;
      15:         double cx = m.m10 / m.m00;
      16:         double cy = m.m01 / m.m00;
      17:         line(imgROI, Point(cx, cy), Point(cx, cy), Scalar(255, 0, 0), 5, 8);
      18:         char label[50];
      19:         sprintf_s(label, "(%.0f,%.0f)", cx, cy);
      20:         int offset = 100;
      21:         putText(imgROI, label, Point(cx-offset, cy), FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 1, 8);
      22:         double perimeter = arcLength(contours[i], true);
      23:         cout << "perimeter = " << perimeter << endl;
      24:         vector<Point> polyCurves;
      25:         double epsilon = 0.01;
      26:         approxPolyDP(contours[i], polyCurves, epsilon*arcLength(contours[i], true), true);
      27:         
      28:         cout << "polygonal curves = " << polyCurves.size() << endl;
      29:         for (int j = 0; j < polyCurves.size()-1; j++) 
      30:         {
      31:             Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
      32:             line(imgROI, polyCurves[j], polyCurves[j + 1], color, 3, 8);
      33:         }
      34:  
      35:         line(imgROI, polyCurves[0], polyCurves[polyCurves.size()-1], Scalar(0, 0,255), 3, 8);
      36:             
      37:         imshow(name, imgROI);
      38:         cout << "------------------------------" << endl;
      39:     }

image

多邊形曲線近似 epsilon = 0.01

image

image

image

image

多邊形曲線近似 epsilon = 0.02

image

image

image

image

--------------------------------------

輪廓極值(minX, maxX, minY, maxY)

   1:  
   2: bool sortByXAscend(Point a, Point b) { return (a.x<b.x); }
   3: bool sortByXDescend(Point a, Point b) { return (a.x>b.x); }
   4: bool sortByYAscend(Point a, Point b) { return (a.y<b.y); }
   5: bool sortByYDescend(Point a, Point b) { return (a.y>b.y); }


   1:  
   2:     sort(polyCurves.begin(), polyCurves.end(), sortByXAscend);
   3:     cout << "sortByXAsc : " << polyCurves << endl;
   4:     circle(imgROI, polyCurves[0], 10, Scalar(0, 0, 255),-1);
   5:     sort(polyCurves.begin(), polyCurves.end(), sortByXDescend);
   6:     cout << "sortByXDescend : " << polyCurves << endl;
   7:     circle(imgROI, polyCurves[0], 10, Scalar(0, 255, 0), -1);
   8:  
   9:     sort(polyCurves.begin(), polyCurves.end(), sortByYAscend);
  10:     cout << "sortByYAsc : " << polyCurves << endl;
  11:     circle(imgROI, polyCurves[0], 10, Scalar(255, 255, 0), -1);
  12:  
  13:     sort(polyCurves.begin(), polyCurves.end(), sortByYDescend);
  14:     cout << "sortByYDescend : " << polyCurves << endl;
  15:     circle(imgROI, polyCurves[0], 10, Scalar(255, 255, 255), -1);

image

image

image

image

image

image

image

image

參考資料

1. Contours - 2 : Brotherhood

2. Contours - 3 : Extraction

3. std::sort

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()