Opencv-(35)轮廓匹配

轮廓匹配

基本原理

根据图形的图像几何矩和Hu矩计算图像的轮廓是否匹配

在这里插入图片描述在这里插入图片描述

代码演示

#include<opencv2/opencv.hpp>
#include<iostream>
using namespace cv;
using namespace std;
void contour_info(Mat& image, vector<vector<Point>>& contours);
int main() {
	Mat src1 = imread("D:/ps/te.png");
	Mat src2 = imread("D:/ps/te.png");
	if (src1.empty()|| src2.empty())
	{
		cout << "could not find the image";
		return -1;
	}
	namedWindow("input", WINDOW_FREERATIO);
	imshow("input1", src1);
	imshow("input2", src2);

	
	vector<vector<Point>>contours1;
	vector<vector<Point>>contours2;
	contour_info(src1, contours1);
	contour_info(src2, contours2);
	Moments mm2 = moments(contours2[0]);
	Mat hu2;
	HuMoments(mm2, hu2);

	for (size_t t = 0; t < contours1.size(); t++) {
		Moments mm = moments(contours2[t]);
		Mat hu;
		HuMoments(mm, hu);
		
		double dist = matchShapes(hu, hu2,CONTOURS_MATCH_I1, 0);
		if(dist < 1.0){
			printf(" matched distance valu: %.2f\n", dist);
		drawContours(src1, contours1, t, Scalar(0, 0, 255), 2, 8);
	}

	}



	//drawContours(src, contours, -1, Scalar(0, 0, 255), 2, 8);
	imshow("find contours demo", src1);
	waitKey(0);
	destroyAllWindows();
	return 0;
}




void contour_info(Mat& image, vector<vector<Point>>& contours) {

	//二值化
	Mat dst;
	GaussianBlur(image, image, Size(3, 3), 0);
	Mat gray, binary;
	cvtColor(image, gray, COLOR_BGR2GRAY);
	threshold(gray, binary, 0, 255, THRESH_BINARY | THRESH_OTSU);

	//轮廓发现
	imshow("binary", binary);
	//vector<vector<Point>>contours;
	vector<Vec4i>hierachy;
	findContours(binary, contours, hierachy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE, Point());

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值