-
Notifications
You must be signed in to change notification settings - Fork 14
/
cppDemo.cpp
179 lines (166 loc) · 5.7 KB
/
cppDemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/stitching.hpp"
#include "superpoint.h"
#include "lightglue.h"
#include <iostream>
#include "common.h"
#include <codecvt>
using namespace std;
using namespace cv;
bool divide_images = false;
Stitcher::Mode mode = Stitcher::PANORAMA;
vector<Mat> imgs;
string stichedImage = "stiching.jpg";
string matchingImage = "matching.jpg";
string superPointPath;//SuperPoint ONNX format model path
string lightGluePath;//LightGlue ONNX format model path
float matchthresh = 0.0f;
void printUsage(char** argv);
int parseCmdArgs(int argc, char** argv);
int main(int argc, char* argv[])
{
int retval = parseCmdArgs(argc, argv);
if (retval) return EXIT_FAILURE;
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
std::wstring sp = converter.from_bytes(superPointPath);
std::wstring lh = converter.from_bytes(lightGluePath);
//![stitching]
Mat pano;
Ptr<Stitcher> stitcher = Stitcher::create(mode);
Ptr<SuperPoint> superpointp = makePtr<SuperPoint>(sp);
Ptr<LightGlue> lightglue = makePtr<LightGlue>(lh, mode, matchthresh);
stitcher->setPanoConfidenceThresh(0.1f);
stitcher->setFeaturesFinder(superpointp);//SpuerPoint feature extraction
stitcher->setFeaturesMatcher(lightglue);//LightGlue feature matching
Stitcher::Status status = stitcher->stitch(imgs, pano);
if (status == Stitcher::OK)
{
imshow(stichedImage, pano);
cv::imwrite(stichedImage,pano);
}
//Draw Matches
std::vector<detail::ImageFeatures> features = lightglue->features();
std::vector<detail::MatchesInfo> matches = lightglue->matchinfo();
for (int i=0;i< matches.size();i++)
{
Mat srcImg = imgs[matches[i].src_img_idx];
Mat dstImg = imgs[matches[i].dst_img_idx];
detail::ImageFeatures srcFeature;
detail::ImageFeatures dstFeature;
for (int j = 0; j < features.size(); j++)
{
if (features[j].img_idx == matches[i].src_img_idx)
srcFeature = features[j];
if (features[j].img_idx == matches[i].dst_img_idx)
dstFeature = features[j];
}
//-- Draw matches
Mat img_matches;
Mat SrcresizedImage;
resize(srcImg, SrcresizedImage, srcFeature.img_size);
Mat DstresizedImage;
resize(dstImg, DstresizedImage, dstFeature.img_size);
drawMatches(SrcresizedImage, srcFeature.keypoints, DstresizedImage, dstFeature.keypoints, matches[i].matches, img_matches);
cv::imwrite(matchingImage, img_matches);
//-- Show detected matches
imshow(matchingImage, img_matches);
cv::waitKey();
}
return EXIT_SUCCESS;
}
void printUsage(char** argv)
{
cout <<
"Images stitcher.\n\n" << "Usage :\n" << argv[0] <<" [Flags] img1 img2 [...imgN]\n\n"
"Flags:\n"
" --d3\n"
" internally creates three chunks of each image to increase stitching success\n"
" --mode (panorama|scans)\n"
" Determines configuration of stitcher. The default is 'panorama',\n"
" mode suitable for creating photo panoramas. Option 'scans' is suitable\n"
" for stitching materials under affine transformation, such as scans.\n"
" --output <result_img>\n"
" The default is 'result.jpg'.\n\n"
" --sp <SuperPoint ONNX format model path>\n"
" --lg <LightGlue ONNX format model path>\n"
"Example usage :\n" << argv[0] << " --d3 --mode scans img1.jpg img2.jpg\n";
}
int parseCmdArgs(int argc, char** argv)
{
if (argc == 1)
{
printUsage(argv);
return EXIT_FAILURE;
}
for (int i = 1; i < argc; ++i)
{
if (string(argv[i]) == "--help" || string(argv[i]) == "/?")
{
printUsage(argv);
return EXIT_FAILURE;
}
else if (string(argv[i]) == "--d3")
{
divide_images = true;
}
else if (string(argv[i]) == "--sp")//SuperPoint ONNX format model path
{
superPointPath = argv[i+1];
i++;
}
else if (string(argv[i]) == "--lg")//LightGlue ONNX format model path
{
lightGluePath = argv[i+1];
i++;
}
else if (string(argv[i]) == "--mthresh")//匹配阈值
{
matchthresh = std::stof(argv[i + 1]); // 将字符串转换为float;
i++;
}
else if (string(argv[i]) == "--d3")
{
divide_images = true;
}
else if (string(argv[i]) == "--output")
{
stichedImage = argv[i + 1];
i++;
}
else if (string(argv[i]) == "--mode")
{
if (string(argv[i + 1]) == "panorama")
mode = Stitcher::PANORAMA;
else if (string(argv[i + 1]) == "scans")
mode = Stitcher::SCANS;
else
{
cout << "Bad --mode flag value\n";
return EXIT_FAILURE;
}
i++;
}
else
{
Mat img = imread(samples::findFile(argv[i]));
if (img.empty())
{
cout << "Can't read image '" << argv[i] << "'\n";
return EXIT_FAILURE;
}
if (divide_images)
{
Rect rect(0, 0, img.cols / 2, img.rows);
imgs.push_back(img(rect).clone());
rect.x = img.cols / 3;
imgs.push_back(img(rect).clone());
rect.x = img.cols / 2;
imgs.push_back(img(rect).clone());
}
else
imgs.push_back(img);
}
}
return EXIT_SUCCESS;
}