opencv學習之路(28)、輪廓查找與繪制(七)——位置關系及輪廓匹配


一、點與輪廓的距離及位置關系

 1 #include "opencv2/opencv.hpp"
 2 #include <iostream>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 void main()
 7 {
 8     //計算點到輪廓的距離與位置關系
 9     Mat srcImg = imread("E://00.png");
10     imshow("src", srcImg);
11 
12     Mat dstImg = srcImg.clone();
13     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
14     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
15     imshow("threshold", srcImg);
16 
17     //查找輪廓
18     vector<vector<Point>> contours;
19     vector<Vec4i> hierarcy;
20     findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);//TREE:提取所有輪廓    NONE:畫出輪廓所有點
21     cout << "contours.size()=" << contours.size() << endl;
22     for (int i = 0; i < contours.size(); i++)//遍歷每個輪廓
23     {
24         for (int j = 0; j < contours[i].size(); j++)//遍歷輪廓每個點
25         {
26             cout << "(" << contours[i][j].x << "," << contours[i][j].y << ")" << endl;
27         }
28     }
29 
30     double a0 = pointPolygonTest(contours[0], Point(3, 3), true);//點到輪廓的最短距離
31     double b0 = pointPolygonTest(contours[0], Point(212, 184), false);//點與輪廓的位置關系:-1表示外部;0表示在輪廓上;1表示輪廓內部
32     cout << "a0=" << a0 << endl;
33     cout << "b0=" << b0 << endl;
34     waitKey(0);
35 }

a0之所以是負數,是因為點在輪廓外部

二、輪廓的矩

輪廓矩的介紹:

http://blog.csdn.net/cp32212116/article/details/38374015
http://blog.csdn.net/huixingshao/article/details/42060231

 1 #include "opencv2/opencv.hpp"
 2 #include <iostream>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 void main()
 7 {
 8     Mat srcImg = imread("E://00.png");
 9     imshow("src", srcImg);
10 
11     Mat dstImg = srcImg.clone();
12     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
13     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
14     //imshow("threshold", srcImg);
15 
16     vector<vector<Point>> contours;
17     vector<Vec4i> hierarcy;
18     findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
19     cout << "contours.size()=" << contours.size() << endl;
20     Moments moment0 = moments(contours[0], false);
21     cout << moment0.m00<< endl;
22     waitKey(0);
23 }

三、形狀匹配--matchShapes()

注意與模板匹配matchTemplate()相區分。形狀匹配對於旋轉、尺度、位移都能適應。

 1 #include "opencv2/opencv.hpp"
 2 #include <iostream>
 3 #include <iomanip>
 4 using namespace std;
 5 using namespace cv;
 6 
 7 void main()
 8 {
 9     Mat srcImg = imread("1.jpg");  //模板圖像
10     imshow("src", srcImg);
11     cvtColor(srcImg, srcImg, CV_BGR2GRAY);
12     threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);
13     vector<vector<Point>> contours;
14     vector<Vec4i> hierarcy;
15     findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
16 
17     Mat srcImg2 = imread("2.jpg");  //待測試圖片
18     imshow("src2", srcImg2);
19     Mat dstImg = srcImg2.clone();
20     cvtColor(srcImg2, srcImg2, CV_BGR2GRAY);
21     threshold(srcImg2, srcImg2, 100, 255, CV_THRESH_BINARY);
22     vector<vector<Point>> contours2;
23     vector<Vec4i> hierarcy2;
24     findContours(srcImg2, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
25     while (1)
26     {
27         for (int i = 0; i<contours2.size(); i++)
28         {
29             double matchRate = matchShapes(contours[0], contours2[i], CV_CONTOURS_MATCH_I1, 0.0);//形狀匹配:值越小越相似
30             cout << "index=" << i << "---" << setiosflags(ios::fixed) << matchRate << endl;//setiosflags(ios::fixed)是用定點方式表示實數,保留相同位數,相同格式輸出
31             if (matchRate <= 0.1)
32                 drawContours(dstImg, contours2, i, Scalar(0, 255, 0), 2, 8);
33             imshow("dst", dstImg);
34             /*char key = waitKey();
35             if (key == 27)
36                 break;*/
37         }
38         break;
39     }
40     waitKey(0);
41 }

 


免責聲明!

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



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