-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.py
228 lines (208 loc) · 7 KB
/
part2.py
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
from PIL import Image
from itertools import izip
import numpy as np
from scipy import signal as sg
import scipy.stats as st
import pylab as lab
from skimage import data
from skimage import transform as tf
def np_from_image(pic):
return np.asarray(Image.open(pic), dtype=np.float32)
def save_as_image(ar, pic):
Image.fromarray(ar.round().astype(np.uint8)).save(pic)
def Affine_Fit( from_pts, to_pts ):
""" Jarno Elonen, November 2007"""
q = from_pts
p = to_pts
if len(q) != len(p) or len(q)<1:
print "from_pts and to_pts must be of same size."
return false
dim = len(q[0]) # num of dimensions
if len(q) < dim:
print "Too few points => under-determined system."
return false
# Make an empty (dim) x (dim+1) matrix and fill it
c = [[0.0 for a in range(dim)] for i in range(dim+1)]
for j in range(dim):
for k in range(dim+1):
for i in range(len(q)):
qt = list(q[i]) + [1]
c[k][j] += qt[k] * p[i][j]
# Make an empty (dim+1) x (dim+1) matrix and fill it
Q = [[0.0 for a in range(dim)] + [0] for i in range(dim+1)]
for qi in q:
qt = list(qi) + [1]
for i in range(dim+1):
for j in range(dim+1):
Q[i][j] += qt[i] * qt[j]
# Ultra simple linear system solver. Replace this if you need speed.
def gauss_jordan(m, eps = 1.0/(10**10)):
(h, w) = (len(m), len(m[0]))
for y in range(0,h):
maxrow = y
for y2 in range(y+1, h): # Find max pivot
if abs(m[y2][y]) > abs(m[maxrow][y]):
maxrow = y2
(m[y], m[maxrow]) = (m[maxrow], m[y])
if abs(m[y][y]) <= eps: # Singular?
return False
for y2 in range(y+1, h): # Eliminate column y
c = m[y2][y] / m[y][y]
for x in range(y, w):
m[y2][x] -= m[y][x] * c
for y in range(h-1, 0-1, -1): # Backsubstitute
c = m[y][y]
for y2 in range(0,y):
for x in range(w-1, y-1, -1):
m[y2][x] -= m[y][x] * m[y2][y] / c
m[y][y] /= c
for x in range(h, w): # Normalize row y
m[y][x] /= c
return True
# Augement Q with c and solve Q * a' = c by Gauss-Jordan
M = [ Q[i] + c[i] for i in range(dim+1)]
if not gauss_jordan(M):
print "Error: singular matrix. Points are probably coplanar."
return false
# Make a result object
class Transformation:
def To_Str(self):
res = ""
for j in range(dim):
str = "x%d' = " % j
for i in range(dim):
str +="x%d * %f + " % (i, M[i][j+dim+1])
str += "%f" % M[dim][j+dim+1]
res += str + "\n"
return res
def Transform(self, pt):
res = [0.0 for a in range(dim)]
for j in range(dim):
for i in range(dim):
res[j] += pt[i] * M[i][j+dim+1]
res[j] += M[dim][j+dim+1]
return res
def getParamsX(self):
paramArray = []
for i in range(dim):
paramArray.append(M[i][dim+1])
paramArray.append(M[dim][dim+1])
return paramArray
def getParamsY(self):
paramArray = []
for i in range(dim):
paramArray.append(M[i][dim+2])
paramArray.append(M[dim][dim+2])
return paramArray
return Transformation()
def imageGinput(image1, image2):
x1 = Image.open(image1)
x2 = Image.open(image2)
fig2 = lab.figure(1)
fig1 = lab.figure(1)
ax2 = fig2.add_subplot(141)
ax1 = fig1.add_subplot(142)
ax1.imshow(x1)
ax2.imshow(x2)
x = fig1.ginput(3)
xArray = []
for i in range(0,len(x)):
xTuple = (int(x[i][0]), int(x[i][1]))
xArray.append(xTuple)
x22 = fig2.ginput(3)
x22Array = []
for i in range(0,len(x22)):
x22Tuple = (int(x22[i][0]), int(x22[i][1]))
x22Array.append(x22Tuple)
fig1.show()
fig2.show()
newArray = [np.asarray(xArray), np.asarray(x22Array)]
x1.save(image1)
x2.save(image2)
print newArray
return newArray
def performAffineTrans(image1, image2):
correspondance = imageGinput(image1, image2)
print correspondance
trn = Affine_Fit(correspondance[0], correspondance[1])
affineParamsX = trn.getParamsX()
affineParamsY = trn.getParamsY()
print trn.To_Str()
print affineParamsX
print affineParamsY
x1 = Image.open(image1)
x2 = Image.open(image2)
width1, height1 = x1.size
width2, height2 = x2.size
oldImage = np_from_image(image1)
newImage = np.zeros(shape=(height1,width1))
for i in range(0,height1-2):
for j in range(0,width1-2):
newPoint = (int(i * affineParamsX[0] + (j * affineParamsX[1]) + affineParamsX[2]), int(i * affineParamsY[0] + (j * affineParamsY[1]) + affineParamsY[2]))
if (((newPoint[0] > 0) and (newPoint[0] < height1-1)) and ((newPoint[1] > 0) and (newPoint[1] < width1-1))):
newImage[newPoint[0]][newPoint[1]] = oldImage[i][j]
save_as_image(newImage, 'img/affineportal.png')
def performAffineTransColor(image1, image2):
correspondance = imageGinput(image1, image2)
print correspondance
trn = Affine_Fit(correspondance[0], correspondance[1])
affineParamsX = trn.getParamsX()
affineParamsY = trn.getParamsY()
print trn.To_Str()
print affineParamsX
print affineParamsY
x1 = Image.open(image1)
x2 = Image.open(image2)
width1, height1 = x1.size
oldImage = np_from_image(image1)
matrixFiller = np.asarray([0, 0, 0, 255])
newImage = []
for i in range(0,height1):
imagevector = []
for j in range(0,width1):
imagevector.append(matrixFiller)
newImage.append(np.asarray(imagevector))
for i in range(0,height1-2):
for j in range(0,width1-2):
newPoint = (int(i * affineParamsX[0] + (j * affineParamsX[1]) + affineParamsX[2]), int(i * affineParamsY[0] + (j * affineParamsY[1]) + affineParamsY[2]))
if (((newPoint[0] > 0) and (newPoint[0] < height1-1)) and ((newPoint[1] > 0) and (newPoint[1] < width1-1))):
newImage[newPoint[0]][newPoint[1]] = oldImage[i][j]
save_as_image(np.asarray(newImage), 'img/affine.png')
def imageStitch(image1, image2):
correspondance = imageGinput(image1, image2)
print correspondance
trn = Affine_Fit(correspondance[0], correspondance[1])
affineParamsX = trn.getParamsX()
affineParamsY = trn.getParamsY()
print trn.To_Str()
print affineParamsX
print affineParamsY
x1 = Image.open(image1)
x2 = Image.open(image2)
width1, height1 = x1.size
width2, height2 = x2.size
oldImage = np_from_image(image1)
oldImage2 = np_from_image(image2)
matrixFiller = np.asarray([0, 0, 0, 255])
newImage = []
# Determine size constraints
maxwidth = width1 if width1 > width2 else width2
maxheight = height1 if height1 > height2 else height2
# Fill the new image with a buffer
for i in range(0,maxheight*4):
imagevector = []
for j in range(0,maxwidth*4):
imagevector.append(matrixFiller)
newImage.append(np.asarray(imagevector))
# Move image 2 into the new image
for i in range(0,height2):
for j in range(0,width2):
newImage[i+(maxwidth*2)][j+(maxheight*2)] = oldImage2[i][j]
# Perform affine transform of image 1 onto the new image
for i in range(0,height1-2):
for j in range(0,width1-2):
newPoint = (int(i * affineParamsX[0] + (j * affineParamsX[1]) + affineParamsX[2]), int(i * affineParamsY[0] + (j * affineParamsY[1]) + affineParamsY[2]))
newImage[newPoint[0]+(maxwidth*2)][newPoint[1]+(maxheight*2)] = oldImage[i][j]
# print "(np0=" + str(newPoint[0]) + ", np1=" + str(newPoint[1]) + ") ------ maxheight = " + str(maxheight) + ", maxwidth = " + str(maxwidth)
save_as_image(np.asarray(newImage), 'img/affine.png')
imageStitch('img/im2.png', 'img/im1.png')