OpenCV實戰(zhàn)之基于Hu矩實現(xiàn)輪廓匹配
前言
本文將使用OpenCV C++ 基于Hu矩進(jìn)行輪廓匹配。
一、查找輪廓
原圖
測試圖
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; }
如圖所示,這就是找到的最外輪廓。接下來,我們基于輪廓進(jìn)行匹配。
二、計算Hu矩
OpenCV提供moments API計算圖像的中心矩;HuMoments API用于中心矩計算Hu矩。關(guān)于moments HuMoments相關(guān)知識請大家自行查找。
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矩。函數(shù)返回值代表兩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; }
總結(jié)
本文使用OpenCV C++基于Hu矩輪廓匹配,關(guān)鍵步驟有以下幾點(diǎn)。
1、查找輪廓。在這里,我是基于最外輪廓進(jìn)行匹配。
2、計算輪廓的Hu矩,然后使用matchShapes計算兩Hu矩的距離,以此來判斷匹配程度。
到此這篇關(guān)于OpenCV實戰(zhàn)之基于Hu矩實現(xiàn)輪廓匹配的文章就介紹到這了,更多相關(guān)OpenCV Hu矩輪廓匹配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
數(shù)據(jù)結(jié)構(gòu) 中數(shù)制轉(zhuǎn)換(棧的應(yīng)用)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 中數(shù)制轉(zhuǎn)換(棧的應(yīng)用)的相關(guān)資料,需要的朋友可以參考下2017-06-06C/C++如何獲取當(dāng)前系統(tǒng)時間的實例詳解
這篇文章主要介紹了 C/C++如何獲取當(dāng)前系統(tǒng)時間的實例詳解的相關(guān)資料,這里提供了幾種實現(xiàn)方法,幫助大家實現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08