OpenCV實(shí)戰(zhàn)之基于Hu矩實(shí)現(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)行匹配。
二、計(jì)算Hu矩
OpenCV提供moments API計(jì)算圖像的中心矩;HuMoments API用于中心矩計(jì)算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;
?? ??? ?}
?? ?}上面代碼段大致思路是:首先計(jì)算測試圖的Hu矩;然后使用一個for循環(huán)計(jì)算原圖中所有輪廓的Hu矩,依次計(jì)算兩Hu矩的相似程度。在這里使用matchShapes API計(jì)算兩個Hu矩。函數(shù)返回值代表兩Hu矩的相似程度。完全相同返回值為0。即這里通過計(jì)算兩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、計(jì)算輪廓的Hu矩,然后使用matchShapes計(jì)算兩Hu矩的距離,以此來判斷匹配程度。
到此這篇關(guān)于OpenCV實(shí)戰(zhàn)之基于Hu矩實(shí)現(xiàn)輪廓匹配的文章就介紹到這了,更多相關(guān)OpenCV Hu矩輪廓匹配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++寫時拷貝實(shí)現(xiàn)原理及實(shí)例解析
這篇文章主要介紹了C++寫時拷貝實(shí)現(xiàn)原理及實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
數(shù)據(jù)結(jié)構(gòu) 中數(shù)制轉(zhuǎn)換(棧的應(yīng)用)
這篇文章主要介紹了數(shù)據(jù)結(jié)構(gòu) 中數(shù)制轉(zhuǎn)換(棧的應(yīng)用)的相關(guān)資料,需要的朋友可以參考下2017-06-06
C++設(shè)計(jì)模式之策略模式(Strategy)
這篇文章主要為大家詳細(xì)介紹了C++設(shè)計(jì)模式之策略模式Strategy ,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
C++保存HBITMAP為位圖文件的實(shí)現(xiàn)方法
這篇文章主要介紹了C++保存HBITMAP為位圖文件的實(shí)現(xiàn)方法,幫助大家更好的理解和使用c++,感興趣的朋友可以了解下2021-01-01
Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸詳解
這篇文章主要介紹了如何利用Matlab實(shí)現(xiàn)生成箭頭坐標(biāo)軸,為坐標(biāo)軸增添箭頭,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下2022-03-03
C/C++如何獲取當(dāng)前系統(tǒng)時間的實(shí)例詳解
這篇文章主要介紹了 C/C++如何獲取當(dāng)前系統(tǒng)時間的實(shí)例詳解的相關(guān)資料,這里提供了幾種實(shí)現(xiàn)方法,幫助大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-08-08

