OpenCV實戰(zhàn)之基于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án)計算原圖中所有輪廓的Hu矩,依次計算兩Hu矩的相似程度。在這里使用matchShapes API計算兩個Hu矩。函數返回值代表兩Hu矩的相似程度。完全相同返回值為0。即這里通過計算兩Hu矩的相似程度,找到返回值最小的那個作為成功匹配。
三、顯示效果
drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2); Rect rect = boundingRect(src_contours[MinIndex]); rectangle(src, rect, Scalar(0, 0, 255), 2);
最終效果如圖所示。


四、源碼
#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矩的距離,以此來判斷匹配程度。
到此這篇關于OpenCV實戰(zhàn)之基于Hu矩實現輪廓匹配的文章就介紹到這了,更多相關OpenCV Hu矩輪廓匹配內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

