網站首頁 編程語言 正文
前言
本文將使用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 = 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矩的距離,以此來判斷匹配程度。
原文鏈接:https://blog.csdn.net/Zero___Chen/article/details/122008672
相關推薦
- 2023-01-11 解讀時間序列分析之ADF檢驗_python
- 2022-08-28 前端實現加載完畢之前loading提示效果
- 2022-09-21 redis緩存數據庫中數據的方法_Redis
- 2022-06-12 C語言經典順序表真題演練講解_C 語言
- 2022-02-28 nuxt 打包后運行 DOMException: Failed to execute ‘append
- 2022-05-20 HTML常用標簽超詳細整理_CSS/HTML
- 2022-07-17 用Android?studio實現簡易計算器功能_Android
- 2022-07-29 解決Vmware虛擬機安裝centos8報錯“Section?%Packages?Does?Not?
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支