網(wǎng)站首頁 編程語言 正文
前言
本文將使用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矩;然后使用一個(gè)for循環(huán)計(jì)算原圖中所有輪廓的Hu矩,依次計(jì)算兩Hu矩的相似程度。在這里使用matchShapes API計(jì)算兩個(gè)Hu矩。函數(shù)返回值代表兩Hu矩的相似程度。完全相同返回值為0。即這里通過計(jì)算兩Hu矩的相似程度,找到返回值最小的那個(gè)作為成功匹配。
三、顯示效果
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矩的距離,以此來判斷匹配程度。
原文鏈接:https://blog.csdn.net/Zero___Chen/article/details/122008672
相關(guān)推薦
- 2022-04-24 如何利用Python實(shí)現(xiàn)簡單C++程序范圍分析_python
- 2022-11-08 PostgreSQL查看帶有綁定變量SQL的通用方法詳解_PostgreSQL
- 2022-12-26 matplotlib畫圖之修改坐標(biāo)軸刻度問題_python
- 2022-09-14 Redis核心原理詳細(xì)解說_Redis
- 2022-12-23 loadavg數(shù)據(jù)異常引發(fā)問題起源分析_Android
- 2022-02-28 el-dialog 的關(guān)閉事件執(zhí)行兩次
- 2022-11-16 C++實(shí)現(xiàn)中綴轉(zhuǎn)后綴的示例詳解_C 語言
- 2022-03-03 react引入antd按需加載警告“You are using a whole package of
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支