-
Notifications
You must be signed in to change notification settings - Fork 0
/
Panorama.cpp
175 lines (156 loc) · 5.26 KB
/
Panorama.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
// Imagine++ project
// Project: Panorama
// Author: Pascal Monasse
// Date: 2013/10/08
#include <Imagine/Graphics.h>
#include <Imagine/Images.h>
#include <Imagine/LinAlg.h>
#include <vector>
#include <sstream>
using namespace Imagine;
using namespace std;
// Record clicks in two images, until right button click
void getClicks(Window w1, Window w2,
vector<IntPoint2>& pts1, vector<IntPoint2>& pts2) {
// ------------- TODO/A completer ----------
IntPoint2 p ;
Window w ;
int subWin=0 ;
int button=anyGetMouse(p,w, subWin) ;
while (button!=3) {
if (w==w1){
pts1.push_back(p);}
else if (w==w2){
pts2.push_back(p);}
button = anyGetMouse(p,w, subWin);
}
}
// Return homography compatible with point matches
Matrix<float> getHomography(const vector<IntPoint2>& pts1,
const vector<IntPoint2>& pts2) {
size_t n = min(pts1.size(), pts2.size());
if(n<4) {
cout << "Not enough correspondences: " << n << endl;
return Matrix<float>::Identity(3);
}
Matrix<double> A(2*n,8);
Vector<double> B(2*n);
// ------------- TODO/A completer ----------
int i;
for(i = 0; i < n; i++){
double xi = (double)pts1.at(i)[0];
double yi = (double)pts1.at(i)[1];
double xi2 = (double)pts2.at(i)[0];
double yi2 = (double)pts2.at(i)[1];
A.setRow(2*i, Imagine::Array<double>({xi, yi, 1, 0, 0, 0, -xi2*xi, -xi2*yi}));
A.setRow(2*i+1, Imagine::Array<double>({0, 0, 0, xi, yi, 1, -yi2*xi, -yi2*yi}));
B[2*i] = xi2;
B[2*i+1] = yi2;
}
B = linSolve(A, B);
Matrix<float> H(3, 3);
H(0,0)=B[0]; H(0,1)=B[1]; H(0,2)=B[2];
H(1,0)=B[3]; H(1,1)=B[4]; H(1,2)=B[5];
H(2,0)=B[6]; H(2,1)=B[7]; H(2,2)=1;
// Sanity check
for(size_t i=0; i<n; i++) {
float v1[]={(float)pts1[i].x(), (float)pts1[i].y(), 1.0f};
float v2[]={(float)pts2[i].x(), (float)pts2[i].y(), 1.0f};
Vector<float> x1(v1,3);
Vector<float> x2(v2,3);
x1 = H*x1;
cout << x1[1]*x2[2]-x1[2]*x2[1] << ' '
<< x1[2]*x2[0]-x1[0]*x2[2] << ' '
<< x1[0]*x2[1]-x1[1]*x2[0] << endl;
}
return H;
}
// Grow rectangle of corners (x0,y0) and (x1,y1) to include (x,y)
void growTo(float& x0, float& y0, float& x1, float& y1, float x, float y) {
if(x<x0) x0=x;
if(x>x1) x1=x;
if(y<y0) y0=y;
if(y>y1) y1=y;
}
// Panorama construction
void panorama(const Image<Color,2>& I1, const Image<Color,2>& I2,
Matrix<float> H) {
Vector<float> v(3);
float x0=0, y0=0, x1=I2.width(), y1=I2.height();
v[0]=0; v[1]=0; v[2]=1;
v=H*v; v/=v[2];
growTo(x0, y0, x1, y1, v[0], v[1]);
v[0]=I1.width(); v[1]=0; v[2]=1;
v=H*v; v/=v[2];
growTo(x0, y0, x1, y1, v[0], v[1]);
v[0]=I1.width(); v[1]=I1.height(); v[2]=1;
v=H*v; v/=v[2];
growTo(x0, y0, x1, y1, v[0], v[1]);
v[0]=0; v[1]=I1.height(); v[2]=1;
v=H*v; v/=v[2];
growTo(x0, y0, x1, y1, v[0], v[1]);
cout << "x0 x1 y0 y1=" << x0 << ' ' << x1 << ' ' << y0 << ' ' << y1<<endl;
Image<Color> I(int(x1-x0), int(y1-y0));
setActiveWindow( openWindow(I.width(), I.height()) );
I.fill(WHITE);
// ------------- TODO/A completer ----------
//first, put the second image by translation
for (int xi=0; xi<I2.width(); xi++) {
for (int yi=0; yi<I2.height(); yi++) {
int x = xi - x0;
int y = yi - y0;
I(x, y) = I2(xi, yi);
}
}
//second, put the first image where it is white, stitch the images together where both images intersect
for (int xi=0; xi<I1.width(); xi++) {
for (int yi=0; yi<I1.height(); yi++) {
v[0]=xi; v[1]=yi; v[2]=1;
v=H*v;
v/=v[2];
int x = v[0] - x0;
int y = v[1] - y0;
if (I(x, y) == WHITE) {
I(x, y) = I1(xi, yi);
}
else {
I(x, y) = (I1(xi, yi)+I(x, y))/2;
}
}
}
display(I,0,0);
}
// Main function
int main(int argc, char* argv[]) {
const char* s1 = argc>1? argv[1]: srcPath("image0006.jpg");
const char* s2 = argc>2? argv[2]: srcPath("image0007.jpg");
// Load and display images
Image<Color> I1, I2;
if( ! load(I1, s1) ||
! load(I2, s2) ) {
cerr<< "Unable to load the images" << endl;
return 1;
}
Window w1 = openWindow(I1.width(), I1.height(), s1);
display(I1,0,0);
Window w2 = openWindow(I2.width(), I2.height(), s2);
setActiveWindow(w2);
display(I2,0,0);
// Get user's clicks in images
vector<IntPoint2> pts1, pts2;
getClicks(w1, w2, pts1, pts2);
vector<IntPoint2>::const_iterator it;
cout << "pts1="<<endl;
for(it=pts1.begin(); it != pts1.end(); it++)
cout << *it << endl;
cout << "pts2="<<endl;
for(it=pts2.begin(); it != pts2.end(); it++)
cout << *it << endl;
// Compute homography
Matrix<float> H = getHomography(pts1, pts2);
cout << "H=" << H/H(2,2);
// Apply homography
panorama(I1, I2, H);
endGraphics();
return 0;
}