画像とテンプレート画像が与えられた場合、画像を照合して、損傷の可能性がある場合はそれを見つけたいと思います。
無傷の画像
破損した画像
テンプレート画像
注: 上の画像は、損傷の例を示しています。損傷のサイズや形状はさまざまです。適切な前処理が行われ、テンプレートと画像の両方が白い背景のバイナリに変換されたとします。
キーポイントを検出して一致させるために、次のアプローチを使用しました。
- ORBを使用して、テンプレートとイメージからすべての
keypoints
とを検索します。そのために、という名前のOpenCVの組み込み関数を使用しました。descriptors
detectAndCompute()
- この後、Brute Force Matcher を使用し、
knnMatch()
. - 次に、 を使用し
Lowe's Ratio Test
て適切な一致を見つけました。
結果:
テンプレートをそれ自体と一致させると、 1751template-template
件の一致が得られます。これは完全一致の理想的な値です。
無傷の画像では、847 個の一致が得られました。
破損した画像では、346の一致が得られました。
試合数で違いは分かりますが、いくつか質問があります。
- 損傷の正確な位置を特定するにはどうすればよいですか?
image-template
と の適切な一致の数を見て、画像に損傷が含まれているとどのように判断できtemplate-template
ますか?
PS:私は OpenCV を初めて使用するので、精巧な回答を期待しています。
編集:参照用のコードは次のとおりです。
#include <iostream>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main() {
Mat image = imread("./Images/PigeonsDamaged.jpg");
Mat temp = imread("./Templates/Pigeons.bmp");
Mat img_gray, temp_gray;
cvtColor(image, img_gray, CV_RGB2GRAY);
cvtColor(temp, temp_gray, CV_RGB2GRAY);
/**** Pre-processing *****/
threshold(temp_gray, temp_gray, 200, 255, THRESH_BINARY);
adaptiveThreshold(img_gray, img_gray, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY_INV, 221, 0);
/*****/
/***** ORB keypoint detector *****/
Mat img_descriptors, temp_descriptors;
vector<KeyPoint> img_keypoints, temp_keypoints;
vector<KeyPoint> &img_kp = img_keypoints;
vector<KeyPoint> &temp_kp = temp_keypoints;
Ptr<ORB> orb = ORB::create(100000, 1.2f, 4, 40, 0, 4, ORB::HARRIS_SCORE, 40, 20);
orb -> detectAndCompute(img_gray, noArray(), img_kp, img_descriptors, false);
orb -> detectAndCompute(temp_gray, noArray(), temp_kp, temp_descriptors, false);
cout << "Temp Keypoints " << temp_kp.size() << endl;
/*****/
vector<vector<DMatch> > featureMatches;
vector<vector<DMatch> > &matches = featureMatches;
Mat & img_desc_ref = img_descriptors;
Mat & temp_desc_ref = temp_descriptors;
BFMatcher bf(NORM_HAMMING2, false); /** Never keep crossCheck true when using knnMatch. Imp: Use NORM_HAMMING2 for WTA_K = 3 or 4 **/
bf.knnMatch(img_descriptors, temp_descriptors, matches, 3);
/*****/
/***** Ratio Test *****/
vector<DMatch> selected;
vector<Point2f> src_pts, temp_pts;
float testRatio = 0.75;
for (int i = 0; i < featureMatches.size(); ++i) {
if (featureMatches[i][0].distance < testRatio * featureMatches[i][1].distance) {
selected.push_back(featureMatches[i][0]);
}
}
cout << "Selected Size: " << selected.size() << endl;
/*****/
/*** Draw the Feature Matches ***/
Mat output;
vector <DMatch> &priorityMatches = selected;
drawMatches(image, img_kp, temp, temp_kp, priorityMatches, output, Scalar(0, 255, 0), Scalar::all(-1));
namedWindow("Output", CV_WINDOW_FREERATIO);
imshow("Output", output);
waitKey();
/******/
return 0;
}