我已经编写了一个代码来缝合2幅图像,使用SIFT键盘描述符和单曲矩阵进行透视变换。是否有任何领域的代码需要改进或优化?
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
using namespace xfeatures2d;
void get_Images(Mat &img1, Mat &img2){
img1 = imread("../1.jpg");
img2 = imread("../2.jpg");
}
void get_keyPoints_n_Descriptors(Mat img1, Mat img2, vector &kp1, vector &kp2, Mat &des1, Mat &des2){
Ptr detector = SiftFeatureDetector::create();
detector->detectAndCompute(img1, noArray(), kp1, des1);
detector->detectAndCompute(img2, noArray(), kp2, des2);
}
void get_matches(vector &matches, Mat des1, Mat des2){
FlannBasedMatcher matcher;
matcher.match(des1, des2, matches);
if(matches.size()<4){
cout<<"too few matches"< &good_matches, Mat des1, Mat des2, vector matches){
double min_dist = 100;
double max_dist = 0;
for(int i = 0; i< des1.rows; i++){
double dist = matches[i].distance;
if(dist < min_dist) min_dist = dist;
if(dist>max_dist) max_dist = dist;
}
//Use min distance to find good matches
int minMatch = 8;
//vector good_matches;
for(int i = 0; i< des1.rows; i++){
if(matches[i].distance < 3*min_dist && matches.size()>minMatch){
good_matches.push_back(matches[i]);
}
}
//Use all keypoints if number of good matches is less than min matches
if(good_matches.size() < minMatch){
for(int i = 0; i < des1.rows; i++){
if(i < good_matches.size()){
good_matches[i] = matches[i];
}
else{
good_matches.push_back(matches[i]);
}
}
}
}
void get_homography(Mat &Homography, vector kp1, vector kp2, vector &good_matches){
//use source and destination to find homography between 2 images
vector src, dst;
for(int i = 0; i kp1, kp2;
Mat des1, des2;
get_keyPoints_n_Descriptors(img1, img2, kp1, kp2, des1, des2);
//get all matches
vector matches;
get_matches(matches, des1, des2);
//get good matches
vector good_matches;
find_good_matches(good_matches, des1, des2, matches);
//Find homography
Mat Homography;
get_homography(Homography, kp1, kp2, good_matches);
//get warped and stitched image
Mat final;
final = warp_n_stitch(img1, img2, Homography);
imshow("Stitched Image", final);
waitKey();
return 0;
}发布于 2023-05-25 07:16:10
我不明白为什么我们需要包括或。
我不喜欢这样:
使用名称空间cv;使用命名空间std;使用命名空间xfeatures2d;
您刚刚放弃了名称空间的所有优点:
与直接在get_Images中初始化两个cv::Mat对象相比,曾经调用的D11函数并没有提供多少好处:
int main()
{
auto img1 = cv::imread("../1.jpg");
auto img2 = cv::imread("../2.jpg");
⋮我们需要更有力地检查这些读取是否成功。如果这两个图像都是空Mat,我们应该打印一条消息到std::cerr并返回EXIT_FAILURE (从)。
最好是从函数返回值,而不是要求用户通过引用传递“”参数:
auto const matches = get_matches(des1, des2);
auto const good_matches = find_good_matches(des1, des2, matches);
auto const Homography = get_homography(kp1, kp2, good_matches);可能还包括:
auto const [kp1, kp2, des1, des2] = get_keyPoints_n_Descriptors(img1, img2);https://codereview.stackexchange.com/questions/285153
复制相似问题