opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

时间:2021-10-29 19:41:09

一、点与轮廓的距离及位置关系

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

 #include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
//计算点到轮廓的距离与位置关系
Mat srcImg = imread("E://00.png");
imshow("src", srcImg); Mat dstImg = srcImg.clone();
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
imshow("threshold", srcImg); //查找轮廓
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);//TREE:提取所有轮廓 NONE:画出轮廓所有点
cout << "contours.size()=" << contours.size() << endl;
for (int i = ; i < contours.size(); i++)//遍历每个轮廓
{
for (int j = ; j < contours[i].size(); j++)//遍历轮廓每个点
{
cout << "(" << contours[i][j].x << "," << contours[i][j].y << ")" << endl;
}
} double a0 = pointPolygonTest(contours[], Point(, ), true);//点到轮廓的最短距离
double b0 = pointPolygonTest(contours[], Point(, ), false);//点与轮廓的位置关系:-1表示外部;0表示在轮廓上;1表示轮廓内部
cout << "a0=" << a0 << endl;
cout << "b0=" << b0 << endl;
waitKey();
}

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

a0之所以是负数,是因为点在轮廓外部

二、轮廓的矩

轮廓矩的介绍:

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

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

 #include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("E://00.png");
imshow("src", srcImg); Mat dstImg = srcImg.clone();
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
//imshow("threshold", srcImg); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE);
cout << "contours.size()=" << contours.size() << endl;
Moments moment0 = moments(contours[], false);
cout << moment0.m00<< endl;
waitKey();
}

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

三、形状匹配--matchShapes()

注意与模板匹配matchTemplate()相区分。形状匹配对于旋转、尺度、位移都能适应。

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配

 #include "opencv2/opencv.hpp"
#include <iostream>
#include <iomanip>
using namespace std;
using namespace cv; void main()
{
Mat srcImg = imread("1.jpg"); //模板图像
imshow("src", srcImg);
cvtColor(srcImg, srcImg, CV_BGR2GRAY);
threshold(srcImg, srcImg, , , CV_THRESH_BINARY);
vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(srcImg, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); Mat srcImg2 = imread("2.jpg"); //待测试图片
imshow("src2", srcImg2);
Mat dstImg = srcImg2.clone();
cvtColor(srcImg2, srcImg2, CV_BGR2GRAY);
threshold(srcImg2, srcImg2, , , CV_THRESH_BINARY);
vector<vector<Point>> contours2;
vector<Vec4i> hierarcy2;
findContours(srcImg2, contours2, hierarcy2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
while ()
{
for (int i = ; i<contours2.size(); i++)
{
double matchRate = matchShapes(contours[], contours2[i], CV_CONTOURS_MATCH_I1, 0.0);//形状匹配:值越小越相似
cout << "index=" << i << "---" << setiosflags(ios::fixed) << matchRate << endl;//setiosflags(ios::fixed)是用定点方式表示实数,保留相同位数,相同格式输出
if (matchRate <= 0.1)
drawContours(dstImg, contours2, i, Scalar(, , ), , );
imshow("dst", dstImg);
/*char key = waitKey();
if (key == 27)
break;*/
}
break;
}
waitKey();
}

opencv学习之路(28)、轮廓查找与绘制(七)——位置关系及轮廓匹配