1. 程式人生 > >【OpenCV學習筆記】三十、輪廓特徵屬性及應用(七)—位置關係及輪廓匹配

【OpenCV學習筆記】三十、輪廓特徵屬性及應用(七)—位置關係及輪廓匹配

輪廓特徵屬性及應用(七)—位置關係及輪廓匹配

1.計算點與輪廓的距離及位置關係——pointPolygonTest()

2.矩的計算——moments()

3.形狀匹配(比較兩個形狀或輪廓間的相似度)——matchShapes()

先上ppt:






程式碼:1.計算點到輪廓的距離與位置關係

///計算點到輪廓的距離與位置關係
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
	//1.查詢輪廓前的預處理
	Mat srcImg = imread("00.png",CV_LOAD_IMAGE_COLOR);
	Mat copyImg = srcImg.clone();
	cvtColor(srcImg,srcImg,CV_BGR2GRAY);
	threshold(srcImg,srcImg,100,255,CV_THRESH_BINARY);//確保黑中找白
	imshow("thresh",srcImg);
	//2.查詢輪廓
	vector<vector<Point>> contours;
	findContours(srcImg,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);//最外層輪廓
	drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
	//3.計算點到輪廓的距離與位置關係
	Point2f p1(20, 20);
	circle(copyImg,p1,3,Scalar(0,0,255),-1,8);
	double a0 = pointPolygonTest(contours[0], p1, true);//true表示點到輪廓的距離
	double b0 = pointPolygonTest(contours[0], p1, false);//false表示計算點與輪廓的位置關係
	cout << "a0=" << a0 << endl;
	cout << "b0=" << b0 << endl;
	imshow("contours",copyImg);
	waitKey(0);
	return 0;
}

執行結果:


程式碼:2.輪廓矩的計算

///輪廓矩的計算
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
	//1.查詢輪廓前的預處理
	Mat srcImg = imread("00.png", CV_LOAD_IMAGE_COLOR);
	Mat copyImg = srcImg.clone();
	cvtColor(srcImg, srcImg, CV_BGR2GRAY);
	threshold(srcImg, srcImg, 100, 255, CV_THRESH_BINARY);//確保黑中找白
	imshow("thresh", srcImg);
	//2.查詢輪廓
	vector<vector<Point>> contours;
	findContours(srcImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外層輪廓
	drawContours(copyImg, contours, -1, Scalar(0, 255, 0), 2, 8);
	//3.輪廓矩的計算
	Moments moments0 = moments(contours[0],false);//計算輪廓矩
	cout << moments0.m00<< endl;//輸出空間矩之一的m00
	imshow("contours", copyImg);
	waitKey(0);
	return 0;
}

執行結果:


程式碼:3.形狀匹配---比較兩個形狀或輪廓間的相似度

///形狀匹配---比較兩個形狀或輪廓間的相似度
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
	//1.查詢模版影象的輪廓
	Mat templateImg = imread("1.jpg", CV_LOAD_IMAGE_COLOR);
	Mat copyImg1 = templateImg.clone();
	cvtColor(templateImg, templateImg, CV_BGR2GRAY);
	threshold(templateImg, templateImg, 100, 255, CV_THRESH_BINARY);//確保黑中找白
	imshow("thresh1", templateImg);
	vector<vector<Point>> contours1;
	findContours(templateImg, contours1, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外層輪廓
	drawContours(copyImg1, contours1, -1, Scalar(0, 255, 0), 2, 8);
	//2.查詢待測試影象的輪廓
	Mat testImg = imread("2.jpg", CV_LOAD_IMAGE_COLOR);
	Mat copyImg2 = testImg.clone();
	cvtColor(testImg, testImg, CV_BGR2GRAY);
	threshold(testImg, testImg, 100, 255, CV_THRESH_BINARY);//確保黑中找白
	imshow("thresh2", testImg);
	vector<vector<Point>> contours2;
	findContours(testImg, contours2, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);//最外層輪廓
	//3.形狀匹配---比較兩個形狀或輪廓間的相似度
	for (int i = 0; i < contours2.size();i++)//遍歷待測試影象的輪廓
	{
		//返回此輪廓與模版輪廓之間的相似度,a0越小越相似
		double a0 = matchShapes(contours1[0],contours2[i],CV_CONTOURS_MATCH_I1,0);
		cout << "模版輪廓與待測試影象輪廓" << i << "的相似度:" << a0 << endl;//輸出兩個輪廓間的相似度
		if (a0<0.1)//如果此輪廓與模版輪廓的相似度小於0.1
		{
			drawContours(copyImg2, contours2, i, Scalar(0, 255, 0), 2, 8);//則在待測試影象上畫出此輪廓
		}
		imshow("copyImg2", copyImg2);
		if (waitKey(0) == 27)//等待按鍵進行下一個輪廓,ESC則退出
		{
			cout << "ESC退出" << endl;
			break;
		}
	}
	waitKey(0);
	return 0;
}

執行結果: