Created
June 15, 2024 08:19
-
-
Save WhiteCat6142/8107c1facb8107b66702777de497a966 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2024 WhiteCat6142 | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <vector> | |
#include <string> | |
#include <opencv2/opencv.hpp> | |
using namespace std; | |
using namespace cv; | |
//以下のコードを参考にした | |
//https://whitewell.sakura.ne.jp/OpenCV/py_tutorials/py_feature2d/py_matcher/py_matcher.html | |
//特徴点のマッチング | |
//https://qiita.com/wakaba130/items/b566f15a4f2cf1ce414f | |
//OpenCVのcv::findHomographyについて | |
int main() | |
{ | |
//画像は以下から | |
//http://www.ess.ic.kanagawa-it.ac.jp/app_images_j.html | |
//標準画像データベース[神奈川工大 信号処理応用研究室] | |
Mat src1 = imread("Mandrill.png"); | |
Mat src2 = imread("Mandrill2.png"); | |
//SIFTについて | |
//https://www.slideshare.net/slideshow/siftsurf/6668292 | |
//画像認識の初歩、SIFT,SURF特徴量 | |
//https://mosapui.hatenablog.com/entry/2009/04/25/193525 | |
//SURF(Speeded Up Robust Features)すごい - もさぷい | |
//http://www.atinfinity.info/blog/archives/108 | |
//OpenCVのCVS版(20081010)で遊んでみた | dandelion's log | |
//https://kesin.hatenablog.com/entry/20120810/1344582180 | |
//OpenCV2でSIFT, SURFによる画像の対応付け | |
vector<KeyPoint> key1; | |
vector<KeyPoint> key2; | |
Mat desc1, desc2; | |
auto detector=SIFT::create(500); | |
detector->detectAndCompute(src1, Mat(), key1, desc1); | |
detector->detectAndCompute(src2, Mat(), key2, desc2); | |
//flannによるマッチングについて | |
//https://speakerdeck.com/matsui_528/jin-si-zui-jin-bang-tan-suo-falsezui-qian-xian | |
//近似最近傍探索の最前線 | |
//https://docs.opencv.org/4.5.2/db/d39/classcv_1_1DescriptorMatcher.html | |
//cv::DescriptorMatcher | |
//https://vovkos.github.io/doxyrest-showcase/opencv/sphinxdoc/struct_cv_flann_SearchParams.html | |
//struct cv::flann::SearchParams | |
//https://stackoverflow.com/questions/44184159/opencv-c-flann-indexparams-and-searchparams-error | |
//OpenCV C++ Flann IndexParams and SearchParams error | |
//https://qiita.com/dandelion1124/items/ab6004058af8fd393674 | |
//OpenCVの初期化処理,終了処理 | |
//https://blog.goo.ne.jp/yoossh/e/a35701fa421903cc78055e3c07c3e1b5 | |
//kd-tree法で点群データ座標を探索(OpenCVとVisual Studio 2019) | |
//https://gist.github.com/kuboyoo/8af6a5eba574a6c9e76af5f39beb5be5 | |
//nearestNeighbor.cpp | |
//https://nokixa.hatenablog.com/entry/2021/08/28/020213 | |
//OpenCVやってみる-20. 特徴点のマッチング(FLANN) | |
vector<vector<DMatch>> knnmatch_points; | |
auto params=cv::makePtr<flann::SearchParams>(50); | |
auto kd=cv::makePtr<flann::KDTreeIndexParams>(5); | |
FlannBasedMatcher match(kd, params); | |
desc1.convertTo(desc1, CV_32F); | |
desc2.convertTo(desc2, CV_32F); | |
match.knnMatch(desc1, desc2, knnmatch_points, 2); | |
//二番目にいい点との比較をする | |
vector<DMatch> goodMatch; | |
vector<Point2f> match_point1; | |
vector<Point2f> match_point2; | |
for (const auto& m: knnmatch_points) { | |
if (m[0].distance <= m[1].distance * 0.75) { | |
goodMatch.push_back(m[0]); | |
match_point1.push_back(key1[m[0].queryIdx].pt); | |
match_point2.push_back(key2[m[0].trainIdx].pt); | |
} | |
} | |
Mat masks; | |
Mat H = findHomography(match_point1, match_point2, masks, RANSAC, 3); | |
//RANSACによるマスク(ロバストなので一部無視する) | |
vector<char> maskx(masks.reshape(1, 1)); | |
//https://docs.opencv.org/4.x//d4/d5d/group__features2d__draw.html | |
//Drawing Function of Keypoints and Matches | |
Mat drawMatch; | |
drawMatches(src1, key1, src2, key2, goodMatch, drawMatch, 1, Scalar::all(-1), Scalar::all(-1), maskx); | |
imwrite("match.jpg", drawMatch); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment