In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to achieve contour matching based on Hu moments in OpenCV. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
First, find the outline
Original drawing
Test pattern
VectorfindContour (Mat Image) {Mat gray; cvtColor (Image, gray, COLOR_BGR2GRAY); Mat thresh; threshold (gray, thresh, 0,255, THRESH_BINARY_INV | THRESH_OTSU); vectorcontours; findContours (thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); vectorEffectConts; for (int I = 0; I
< contours.size(); i++) { double area = contourArea(contours[i]); if (area >1000) {EffectConts.push_back (contours [I]);}} return EffectConts;}
As shown in the figure, this is the outermost outline found. Next, we match based on the contour.
Second, calculate the Hu moment
OpenCV provides moments API to calculate the center moment of the image; HuMoments API is used to calculate the Hu moment. Please find out the relevant knowledge about moments HuMoments by yourself.
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#includeusing namespace std;using namespace cv;vectorfindContour(Mat Image){ Mat gray; cvtColor(Image, gray, COLOR_BGR2GRAY); Mat thresh; threshold(gray, thresh, 0, 255, THRESH_BINARY_INV | THRESH_OTSU); vectorcontours; findContours(thresh, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE); vectorEffectConts; 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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.