-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stitching.cpp
332 lines (287 loc) · 11.7 KB
/
Stitching.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Panorama creation using Image Stitching
Code is developed for stitching three images. Homographic transform matrix is chosen for specific manually chosen control points.
Users can use other methods to select control points and calculate homographic tranform matrix according to their images
Pass arguments in following order- Input_Image(1).raw Input_Image(2).raw Input_Image(3).raw BytesPerPixel Width Height Output_Image.raw
Author- Chinmay Jog
*/
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
using namespace std;
unsigned char projective_left(unsigned char** plane, int u, int v);
unsigned char projective_right(unsigned char** plane, int u, int v);
unsigned char** create_2D_array(int xSize, int ySize);
int main(int argc, char *argv[])
{
// Define file pointer and variables
FILE *file;
int BytesPerPixel;
int xSize = 512;
int ySize = 512;
// Check for proper syntax
if (argc < 3){
cout << "Syntax Error - Incorrect Parameter Usage:" << endl;
cout << "program_name input_image.raw output_image.raw [BytesPerPixel = 1] [Size = 256]" << endl;
return 0;
}
// Check if image is grayscale or color
if (argc < 4){
BytesPerPixel = 1; // default is grey image
}
else {
BytesPerPixel = atoi(argv[4]);
// Check if size is specified
if (argc >= 5){
xSize = atoi(argv[5]);
ySize = atoi(argv[6]);
}
}
// Allocate image data array
unsigned char* LeftImage = new unsigned char [xSize*ySize*BytesPerPixel];
unsigned char* MiddleImage = new unsigned char [xSize*ySize*BytesPerPixel];
unsigned char* RightImage = new unsigned char [xSize*ySize*BytesPerPixel];
unsigned char* result = new unsigned char [(xSize+900)*(ySize+500)*BytesPerPixel];
unsigned char* rleft = new unsigned char [xSize*ySize];
unsigned char* gleft = new unsigned char [xSize*ySize];
unsigned char* bleft = new unsigned char [xSize*ySize];
unsigned char* rmid = new unsigned char [xSize*ySize];
unsigned char* gmid = new unsigned char [xSize*ySize];
unsigned char* bmid = new unsigned char [xSize*ySize];
unsigned char* rright = new unsigned char [xSize*ySize];
unsigned char* gright = new unsigned char [xSize*ySize];
unsigned char* bright = new unsigned char [xSize*ySize];
unsigned char** rinleft = create_2D_array(xSize, ySize);
unsigned char** binleft = create_2D_array(xSize, ySize);
unsigned char** ginleft = create_2D_array(xSize, ySize);
unsigned char** routleft = create_2D_array(xSize+900, ySize+500);
unsigned char** goutleft = create_2D_array(xSize+900, ySize+500);
unsigned char** boutleft = create_2D_array(xSize+900, ySize+500);
unsigned char** rinmid = create_2D_array(xSize, ySize);
unsigned char** ginmid = create_2D_array(xSize, ySize);
unsigned char** binmid = create_2D_array(xSize, ySize);
unsigned char** rinright = create_2D_array(xSize, ySize);
unsigned char** ginright = create_2D_array(xSize, ySize);
unsigned char** binright = create_2D_array(xSize, ySize);
unsigned char** routright = create_2D_array(xSize+900, ySize+500);
unsigned char** goutright = create_2D_array(xSize+900, ySize+500);
unsigned char** boutright = create_2D_array(xSize+900, ySize+500);
// Read images (filename specified by first 3 arguments) into image data matrix
if (!(file=fopen(argv[1],"rb"))) {
cout << "Cannot open file: " << argv[1] <<endl;
exit(1);
}
fread(LeftImage, sizeof(unsigned char), xSize*ySize*BytesPerPixel, file);
fclose(file);
if (!(file=fopen(argv[2],"rb"))) {
cout << "Cannot open file: " << argv[2] <<endl;
exit(1);
}
fread(MiddleImage, sizeof(unsigned char), xSize*ySize*BytesPerPixel, file);
fclose(file);
if (!(file=fopen(argv[3],"rb"))) {
cout << "Cannot open file: " << argv[3] <<endl;
exit(1);
}
fread(RightImage, sizeof(unsigned char), xSize*ySize*BytesPerPixel, file);
fclose(file);
// Stitching
int count1 = 0;
for (int i = 0; i < xSize*ySize*BytesPerPixel; i+=3)
{
rleft[count1] = LeftImage[i];
gleft[count1] = LeftImage[i+1];
bleft[count1] = LeftImage[i+2];
rmid[count1] = MiddleImage[i];
gmid[count1] = MiddleImage[i+1];
bmid[count1] = MiddleImage[i+2];
rright[count1] = RightImage[i];
gright[count1] = RightImage[i+1];
bright[count1] = RightImage[i+2];
count1++;
}
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
rinleft[i][j] = rleft[i*xSize + j];
ginleft[i][j] = gleft[i*xSize + j];
binleft[i][j] = bleft[i*xSize + j];
rinmid[i][j] = rmid[i*xSize + j];
ginmid[i][j] = gmid[i*xSize + j];
binmid[i][j] = bmid[i*xSize + j];
rinright[i][j] = rright[i*xSize + j];
ginright[i][j] = gright[i*xSize + j];
binright[i][j] = bright[i*xSize + j];
}
}
// Warping function call
cout << "now projection " << endl;
for (int i = 0; i < ySize+500; i++)
{
//cout << "i is " << i << endl;
for (int j = 0; j < xSize+900; j++)
{
routleft[i][j] = projective_left(rinleft, i, j);
goutleft[i][j] = projective_left(ginleft, i, j);
boutleft[i][j] = projective_left(binleft, i, j);
if (result[i*(xSize+900)*3 + (j)*3] == 0)
{
result[i*(xSize+900)*3 + (j)*3] = routleft[i][j];
result[i*(xSize+900)*3 + (j)*3 + 1] = goutleft[i][j];
result[i*(xSize+900)*3 + (j)*3 + 2] = boutleft[i][j];
}
//result[i*(xSize+200)*3+j*3] = routleft[i][j];
}
}
cout << "Now stitching left and middle " << endl;
for (int i = 0; i < ySize; i++)
{
for (int j = 0; j < xSize; j++)
{
if (result[(i+312)*(xSize+900)*3 + (j+515)*3] != 0)
{
result[(i+312)*(xSize+900)*3 + (j+515)*3] = (result[(i+312)*(xSize+900)*3 + (j+515)*3] + rinmid[i][j])/2;
result[(i+312)*(xSize+900)*3 + (j+515)*3 + 1] = (result[(i+312)*(xSize+900)*3 + (j+515)*3 + 1] + ginmid[i][j])/2;
result[(i+312)*(xSize+900)*3 + (j+515)*3 + 2] = (result[(i+312)*(xSize+900)*3 + (j+515)*3 + 2] + binmid[i][j])/2;
}
else
{
result[(i+312)*(xSize+900)*3 + (j+515)*3] = rinmid[i][j];
result[(i+312)*(xSize+900)*3 + (j+515)*3 + 1] = ginmid[i][j];
result[(i+312)*(xSize+900)*3 + (j+515)*3 + 2] = binmid[i][j];
}
//cout << "i is " << i << " j is " << j << endl;
}
}
cout << "now right projection and stitching " << endl;
for (int i = 0; i < ySize+500; i++)
{
for (int j = 0; j < xSize+900; j++)
{
routright[i][j] = projective_right(rinright, i, j);
goutright[i][j] = projective_right(ginright, i, j);
boutright[i][j] = projective_right(binright, i, j);
if (result[i*(xSize+900)*3 + (j)*3] == 0)
{
result[i*(xSize+900)*3 + (j)*3] = routright[i][j];
result[i*(xSize+900)*3 + (j)*3 + 1] = goutright[i][j];
result[i*(xSize+900)*3 + (j)*3 + 2] = boutright[i][j];
}
else
{
if (routright[i][j] != 0)
result[i*(xSize+900)*3 + (j)*3] = (result[i*(xSize+900)*3 + (j)*3] + routright[i][j])/2;
if (goutright[i][j] != 0)
result[i*(xSize+900)*3 + (j)*3 + 1] = (result[i*(xSize+900)*3 + (j)*3 + 1] + goutright[i][j])/2;
if (boutright[i][j] != 0)
result[i*(xSize+900)*3 + (j)*3 + 2] = (result[i*(xSize+900)*3 + (j)*3 + 2] + boutright[i][j])/2;
}
}
}
cout << "Result success " << endl;
// Write image data (filename specified by second argument) from image data matrix
if (!(file=fopen(argv[7],"wb"))) {
cout << "Cannot open file: " << argv[7] << endl;
exit(1);
}
fwrite(result, sizeof(unsigned char), (xSize+900)*(ySize+500)*BytesPerPixel, file);
fclose(file);
delete[] LeftImage;
delete[] MiddleImage;
delete[] RightImage;
delete[] result;
delete[] rleft;
delete[] gleft;
delete[] bleft;
for (int i = 0; i < ySize; i++)
{
delete[] rinleft[i];
delete[] binleft[i];
delete[] ginleft[i];
delete[] routleft[i];
delete[] goutleft[i];
delete[] boutleft[i];
delete[] rinmid[i];
delete[] ginmid[i];
delete[] binmid[i];
delete[] rinright[i];
delete[] ginright[i];
delete[] binright[i];
}
delete[] rinleft;
delete[] binleft;
delete[] ginleft;
delete[] rinmid;
delete[] ginmid;
delete[] binmid;
delete[] rinright;
delete[] ginright;
delete[] binright;
delete[] routleft;
delete[] goutleft;
delete[] boutleft;
delete[] routright;
delete[] goutright;
delete[] boutright;
// delete[] rout;
// delete[] gout;
// delete[] bout;
return 0;
}
unsigned char** create_2D_array(int xSize, int ySize)
{
unsigned char** newArray = new unsigned char*[ySize];
for (int i = 0; i < ySize; i++)
{
newArray[i] = new unsigned char[xSize];
}
return newArray;
}
unsigned char projective_left(unsigned char** plane, int u, int v)
{
unsigned char output = 0;
int left = 0;
int top = 0;
double delX = 0;
double delY = 0;
double x = (0.671641419*u - 0.170916775*v - 104.239976)/(0.0000141046254*u + -0.000554889381*v + 1);
double y = (-0.00330654332*u + 0.460786062*v + -36.5581096)/(0.0000141046254*u + -0.000554889381*v + 1);
if (x >= 0 && x < 639 && y>= 0 && y < 479)
{
left = floor(x);
top = floor(y);
delX = x - left;
delY = y - top;
//cout << "now bilinear " << endl;
output = ((1-delX)*(1-delY)*plane[left][top]) + ((delX)*(1-delY)*plane[left][top+1])
+ ((1-delX)*(delY)*plane[left+1][top]) + ((delX)*(delY)*plane[left+1][top+1]);
}
return output;
}
unsigned char projective_right(unsigned char** plane, int u, int v)
{
//cout << "function entered " << endl;
unsigned char output = 0;
int left = 0;
int top = 0;
double delX = 0;
double delY = 0;
//double x = (1.04679568376569*u + 0.172255438495553*v + -469.954179500672)/(-2.23469834773348e-05*u + 0.000554403618039415*v + 0.605275885383875);
//double y = (0.0210196538928225*u + 1.12691370038267*v + -824.239392638731)/(-2.23469834773348e-05*u + 0.000554403618039415*v + 0.605275885383875);
//double x = (1.01152832158642*u + 0.198786437246042*v + -473.471412178908)/(-9.66690802237395e-05*u + 0.000639607929172788*v + 0.568407667192168);
//double y = (0.0104684050596606*u + 1.12947597549977*v + -821.168980298455)/(-9.66690802237395e-05*u + 0.000639607929172788*v + 0.568407667192168);
double x = (1.06476193210222*u + 0.201497622260042*v + -499.800313250537)/(-1.68472256851966e-05*u + 0.000661254908071017*v + 0.527036925843002);
double y = (0.00855047884459119*u + 1.16468564013660*v + -845.749556553356)/(-1.68472256851966e-05*u + 0.000661254908071017*v + 0.527036925843002);
if (x >= 0 && x < 639 && y >= 0 && y < 479)
{
left = floor(x);
top = floor(y);
delX = x - left;
delY = y - top;
//cout << "now bilinear " << endl;
output = ((1-delX)*(1-delY)*plane[left][top]) + ((delX)*(1-delY)*plane[left][top+1])
+ ((1-delX)*(delY)*plane[left+1][top]) + ((delX)*(delY)*plane[left+1][top+1]);
}
return output;
}