-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstitch.cpp
More file actions
117 lines (95 loc) · 2.84 KB
/
stitch.cpp
File metadata and controls
117 lines (95 loc) · 2.84 KB
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
/*
*
* Author: Kevin Midkiff
* Version: 0.1
* Description:
* Simple image stitching tool using OpenCV to be used for creating panorama images.
* This was written for my Visual Computer class at Portland State University (CS410).
*
*/
#include <stdio.h>
// OpenCV Includes
#include "opencv2/core/version.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/stitching/detail/blenders.hpp"
// OpenCV 2.4.x has CV_VERSION_EPOCH defined, 3.x does not
#if defined(CV_VERSION_EPOCH)
#include "opencv2/stitching/stitcher.hpp"
#elif CV_VERSION_MAJOR == 3
#include "opencv2/stitching.hpp"
#endif
using namespace std;
using namespace cv;
using namespace cv::detail;
vector<Mat> getImages(int argc, int startIdx, char *argv[]) {
vector<Mat> images;
for(int i = startIdx; i < argc; i++) {
Mat img = imread(argv[i]);
// Verifying the image opened correctly
if (!img.data) {
fprintf(stderr, "Failed to Read Image: %s\n", argv[i]);
exit(-1);
}
images.push_back(img);
}
return images;
}
Blender *getBlender(char *bn) {
Blender *b;
if (strcmp(bn, "Feather") == 0) {
b = new FeatherBlender();
}
else if(strcmp(bn, "MultiBand") == 0) {
b = new MultiBandBlender();
}
else {
fprintf(stderr, "Unknown Blender: %s\n", bn);
exit(-1);
}
return b;
}
void showUsage(char *binaryName) {
printf("Usage: %s [-GPU] <blender> <output> <img1> <img2> ... <imgN>\n\n", binaryName);
printf("Version: 0.1\n");
printf("Author: Kevin Midkiff\n");
printf("Description: Simple image stitching command line tool using OpenCV\n\n");
printf("WARNING: You must pass at least two images\n\n");
printf("Argument Description:\n");
printf("\tGPU - Flag to use the GPU\n");
printf("\tblender - Which blender to use: Default, Feather, MultiBand\n");
printf("\toutput - Output filename\n\n");
}
int main(int argc, char *argv[]) {
if (argc < 4) {
showUsage(argv[0]);
exit(-1);
}
vector<Mat> vImg;
Mat rImg;
bool useGPU = false;
int blenderIdx = 1;
int outNameIdx = 2;
int imgIdxStart = 3;
if (strcmp(argv[1], "-GPU") == 0) {
blenderIdx++;
outNameIdx++;
imgIdxStart++;
useGPU = true;
}
char *outName = argv[outNameIdx];
Stitcher stitcher = Stitcher::createDefault(useGPU);
Blender *b;
if (strcmp(argv[blenderIdx], "Default") != 0) {
b = getBlender(argv[blenderIdx]);
stitcher.setBlender(b);
}
printf("Reading in images... ");
vImg = getImages(argc, imgIdxStart, argv);
printf("Done.\n");
printf("Stitching images together.... ");
stitcher.stitch(vImg, rImg);
imwrite(outName, rImg);
printf("Done.\n");
return 0;
}