OpenCV C++ 事例演習 8 「Hu モーメントに基づく輪郭マッチング」
序文
この記事では、OpenCV C++ を使用して、Hu モーメントに基づいて輪郭マッチングを実行します。
1. 輪郭を見つける
オリジナル画像
テスト画像
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 は、画像の中心モーメントを計算するモーメント API を提供します。中心モーメントから Hu モーメントを計算するために HuMoments API が使用されます。モーメント 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 モーメントを計算し、次に 2 つの Hu 間の類似性を計算します。瞬間。ここでは、matchShapes API を使用して 2 つの Hu モーメントが計算されます。関数の戻り値は、2 つの Hu モーメントの類似性を表します。まったく同じ場合は値 0 が返されます。つまり、2 つの Hu モーメント間の類似性を計算し、戻り値が最も小さいものを一致として見つけます。
3. ディスプレイ効果
drawContours(src, src_contours, MinIndex, Scalar(0, 255, 0), 2);
Rect rect = boundingRect(src_contours[MinIndex]);
rectangle(src, rect, Scalar(0, 0, 255), 2);
最終的な効果を図に示します。
4. ソースコード
#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;
}
要約する
この記事では、Hu モーメント輪郭マッチングに基づいた OpenCV C++ を使用します。主な手順は次のとおりです。
1. アウトラインを見つけます。ここでは、最も外側の輪郭に基づいてマッチングしています。
2. 輪郭の Hu モーメントを計算し、matchShapes を使用して 2 つの Hu モーメント間の距離を計算し、一致の程度を決定します。