OpenCV C++案例实战八《基于Hu矩轮廓匹配》
前言
本文将使用OpenCV C++ 基于Hu矩进行轮廓匹配。
一、查找轮廓
原图
测试图
vector<vector<Point>>findContour(Mat Image)
{
Mat gray;
cvtColor(Image, gray, COLOR_BGR2GRAY);
Mat thresh;
threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>>contours;
findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
vector<vector<Point>>EffectConts;
for (int i = 0; i < contours.size(); i++)
{
double area = contourArea(contours[i]);
if (area > 1000)
{
EffectConts.push_back(contours[i]);
}
}
return EffectConts;
}
如图所示,这就是找到的最外轮廓。接下来,我们基于轮廓进行匹配。
二、计算Hu矩
OpenCV提供moments API计算图像的中心矩;HuMoments API用于中心矩计算Hu矩。关于moments HuMoments相关知识请大家自行查找。
Moments m_test = moments(test_contours[0]);
Mat hu_test;
HuMoments(m_test, hu_test);
double MinDis = 1000;
int MinIndex = 0;
for (int i = 0; i < src_contours.size(); i++)
{
Moments m_src = moments(src_contours[i]);
Mat hu_src;
HuMoments(m_src, hu_src);
double dist = matchShapes(hu_test, hu_src, CONTOURS_MATCH_I1, 0);
if (dist < MinDis)
{
MinDis = dist;
MinIndex = i;
}
}
上面代码段大致思路是:首先计算测试图的Hu矩;然后使用一个for循环计算原图中所有轮廓的Hu矩,依次计算两Hu矩的相似程度。在这里使用matchShapes API计算两个Hu矩。函数返回值代表两Hu矩的相似程度。完全相同返回值为0。即这里通过计算两Hu矩的相似程度,找到返回值最小的那个作为成功匹配。
三、显示效果
drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2);
Rect rect <span class="token operator">=</span> <span class="token function">boundingRect</span><span class="token punctuation">(</span>src_contours<span class="token punctuation">[</span>MinIndex<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token function">rectangle</span><span class="token punctuation">(</span>src<span class="token punctuation">,</span> rect<span class="token punctuation">,</span> <span class="token function">Scalar</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">255</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
最终效果如图所示。
四、源码
#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
vector<vector<Point>>findContour(Mat Image)
{
Mat gray;
cvtColor(Image, gray, COLOR_BGR2GRAY);
Mat thresh;
threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
vector<vector<Point>>contours;
findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);
vector<vector<Point>>EffectConts;
for (int i = 0; i < contours.size(); i++)
{
double area = contourArea(contours[i]);
if (area > 1000)
{
EffectConts.push_back(contours[i]);
}
}
return EffectConts;
}
int main()
{
Mat src = imread(“test/hand.jpg”);
Mat test = imread(“test/test-3.jpg”);
if (src.empty() || test.empty())
{
cout << “No Image!” << endl;
system(“pause”);
return -1;
}
vector<vector<Point>>src_contours;
vector<vector<Point>>test_contours;
src_contours = findContour(src);
test_contours = findContour(test);
Moments m_test = moments(test_contours[0]);
Mat hu_test;
HuMoments(m_test, hu_test);
double MinDis = 1000;
int MinIndex = 0;
for (int i = 0; i < src_contours.size(); i++)
{
Moments m_src = moments(src_contours[i]);
Mat hu_src;
HuMoments(m_src, hu_src);
double dist = matchShapes(hu_test, hu_src, CONTOURS_MATCH_I1, 0);
if (dist < MinDis)
{
MinDis = dist;
MinIndex = i;
}
}
drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2);
Rect rect = boundingRect(src_contours[MinIndex]);
rectangle(src, rect, Scalar(0, 0, 255), 2);
imshow(“test”, test);
imshow(“Demo”, src);
waitKey(0);
system(“pause”);
return 0;
}
总结
本文使用OpenCV C++基于Hu矩轮廓匹配,关键步骤有以下几点。
1、查找轮廓。在这里,我是基于最外轮廓进行匹配。
2、计算轮廓的Hu矩,然后使用matchShapes计算两Hu矩的距离,以此来判断匹配程度。