-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimple_camscanner_LA_project1_9931103.py
More file actions
167 lines (120 loc) · 4.24 KB
/
simple_camscanner_LA_project1_9931103.py
File metadata and controls
167 lines (120 loc) · 4.24 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
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
import numpy as np
import os
from PIL import Image
from matplotlib import pyplot as plt
def get_input(file_name):
img = Image.open(file_name)
img = np.asarray(img)
img = to_mtx(img)
return img
def to_mtx(img):
"""
This method just reverse x and y of an image matrix because of the different order of x and y in PIL and Matplotlib library
"""
H, V, C = img.shape
mtr = np.zeros((V, H, C), dtype='int')
for i in range(img.shape[0]):
mtr[:, i] = img[i]
return mtr
def get_coef(a, b, n):
res = []
b = [b[0], b[1], 1]
dim = 3
for i in range(dim):
curr = [0] * dim * 4
curr[i] = a[0]
curr[dim + i] = a[1]
curr[2 * dim + i] = 1 if i != 2 else 0
curr[3 * dim + n - 1] = -b[i]
res.append(curr)
return res
def getPerspectiveTransform(pts1, pts2):
A = []
plen = len(pts1)
for i in range(plen):
A += get_coef(pts1[i], pts2[i], i)
B = [0, 0, -1] * plen
C = np.linalg.solve(A, B)
res = np.ones(9)
res[:8] = C.flatten()[:8]
return res.reshape(3, -1).T
def showWarpPerspective(dst):
width, height, _ = dst.shape
# This part is for denoising the result matrix . You can use this if at first you have filled matrix with zeros
for i in range(width - 1, -1, -1):
for j in range(height - 1, -1, -1):
if dst[i][j][0] == 0 and dst[i][j][1] == 0 and dst[i][j][2] == 0:
if i + 1 < width and j - 1 >= 0:
dst[i][j] = dst[i + 1][j - 1]
showImage(dst, title='Warp Perspective')
def showImage(image, title, save_file=True):
final_ans = to_mtx(image)
final_ans = final_ans.astype(np.uint8)
plt.title(title)
plt.imshow(final_ans)
if save_file:
try:
os.mkdir('out')
except OSError:
pass
path = os.path.join('out', title + '.jpg')
plt.savefig(path, bbox_inches='tight')
plt.show()
def Filter(img, filter_matrix):
m, n, l = img.shape
res = np.zeros((m, n, l))
for i in range(m):
for j in range(n):
reshaped = np.reshape(img[i, j, :], newshape=(3,))
res[i, j, :] = filter_matrix.dot(reshaped)
return res
def warpPerspective(img, transform_matrix, output_width, output_height):
result = img.warpPerspective([(264,21),(594,180),(259,982),(623,906)], transform_matrix, (output_width, output_height))
return result
def grayScaledFilter(img):
img1 = img.convert("L")
return img1
def crazyFilter(img):
green = np.copy(img)
green[:,:,1]=green[:,:,1]/2
green[:,:,2] , green[:,:,0]=green[:,:,0],green[:,:,0]
avg = 0
for pixel in img:
pixel2 = pixel
pixel.greensize = pixel2.greensize/2
avg += (pixel.getRed() + pixel.getGreen() + pixel.getBlue())/3
"""
TODO : Complete this part based on the description in the manual!
"""
pass
def scaleImg(img, scale_width, scale_height):
resized_image = img.resize(scale_width, scale_height)
return resized_image
def permuteFilter(img):
"""
TODO : Complete this part based on the description in the manual!
"""
pass
if __name__ == "__main__":
image_matrix = get_input('pic.jpg')
# You can change width and height if you want
width, height = 300, 400
showImage(image_matrix, title="Input Image")
"""
TODO : Find coordinates of four corners of your inner Image ( X,Y format)
Order of coordinates: Upper Left, Upper Right, Down Left, Down Right
"""
pts1 = np.float32([[264, 21], [594, 180], [259, 982], [623, 906]])
pts2 = np.float32([[0, 0], [width, 0], [0, height], [width, height]])
m = getPerspectiveTransform(pts1, pts2)
warpedImage = warpPerspective(image_matrix, m, width, height)
showWarpPerspective(warpedImage)
grayScalePic = grayScaledFilter(warpedImage)
showImage(grayScalePic, title="Gray Scaled")
crazyImage, invertedCrazyImage = crazyFilter(warpedImage)
showImage(crazyImage, title="Crazy Filter")
showImage(invertedCrazyImage, title="Inverted Crazy Filter")
scaledImage = scaleImg(warpedImage, 3, 4)
showImage(scaledImage, title="Scaled Image")
permuteImage = permuteFilter(warpedImage)
showImage(permuteImage, title="Permuted Image")