-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinalproject.py
More file actions
273 lines (213 loc) · 6.48 KB
/
finalproject.py
File metadata and controls
273 lines (213 loc) · 6.48 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
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
# ASSIGNMENT 11
# Chris DeJager
""" Final Project
"""
import numpy as np
import scipy as sp
import cv2
import scipy.signal
def fil(width, alpha=0.5):
"""Returns a filter to use
Parameters
----------
width : int
The width of the filter
alpha : float
A parameter
Returns
-------
numpy.ndarray(dtype: np.float)
A widthx1 numpy array representing the filter.
"""
fil = np.zeros(width, dtype=np.float64)
hd = width/2.
for i in xrange(width):
cendst = hd - abs(i-hd)
fil[i] = ((1-alpha) + 2*alpha*(cendst/hd)) / width
return fil
def hueHist(image):
"""This function takes a color image and returns a histogram of the hues in
that image
Parameters
----------
image : numpy.ndarray
An image
Returns
-------
numpy.ndarray(dtype=np.uint32)
a hue hist
"""
outhst = np.zeros(180, dtype=np.uint32)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
for row in hsv:
for pix in row:
outhst[pix[0]] += 1
return outhst
def genHueDiff(image, mask):
"""This function finds differences between the image and the mask
Parameters
----------
image : numpy.ndarray
An image
mask : numpy.ndarray
A mask
Returns
-------
[(int, int), ...]
The image with the hue changed
"""
hsvImg = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
hsvMsk = cv2.cvtColor(mask, cv2.COLOR_BGR2HSV)
hueDiff = []
for i in xrange(len(hsvImg)):
for j in xrange(len(hsvImg[0])):
if (hsvImg[i][j] != hsvMsk[i][j]).any():
hueDiff.append(((hsvImg[i][j][0], hsvImg[i][j][1], hsvImg[i][j][2]), (hsvMsk[i][j][0], hsvMsk[i][j][1], hsvMsk[i][j][2]), (i,j)))
return list(set(hueDiff))
def changeHVPos(hsvImg, imgMark, tp, pos, hwidth, swidth, vwidth, stack):
"""Chanes the hue and value starting at at the given position
Parameters
----------
hsvImg : numpy.ndarray
An image
imgMark : numpy.ndarry (np.bool)
marks the spaces overwritten
trans : translation tuple
pos : The position
"""
# check to see if the spot has been checked or is out of bounds
if pos[0] < 0 or pos[0] >= len(hsvImg) or pos[1] < 0 or pos[1] >= len(hsvImg[0]) or imgMark[pos[0]][pos[1]]:
return
pix = hsvImg[pos[0]][pos[1]]
hdst = abs(int(tp[0][0])-pix[0])
if hdst >= 180:
hdst = 180 - hdst
sdst = abs(int(tp[0][1])-pix[1])
if sdst >= 256:
sdst = 256 - sdst
vdst = abs(int(tp[0][2])-pix[2])
if vdst >= 256:
vdst = 256 - vdst
if hdst < hwidth/2 and sdst < swidth/2 and vdst < vwidth/2:
nh = int(tp[1][0])-tp[0][0] + pix[0]
if nh < 0:
nh += 180
elif nh >= 180:
nh -= 180
pix[0] = nh
#nv = int(tp[1][2])-tp[0][2] + pix[2]
#if nv < 0:
# nv = 0
#elif nv >= 256:
# nv = 255
#pix[2] = nv
imgMark[pos[0]][pos[1]] = True
stack.append((pos[0]+1, pos[1]))
stack.append((pos[0]-1, pos[1]))
stack.append((pos[0], pos[1]+1))
stack.append((pos[0], pos[1]-1))
def changeHV(image, tPairs, hwidth=18, swidth=128, vwidth=128):
"""This function changes the hue and value in the image starting at the given point
Parameters
----------
image : numpy.ndarray
An image
huePairs : (int, int)[]
The converion to due to the hues
width :
The width of the hue
Returns
-------
numpy.ndarray
The image with the hue changed
"""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
im = np.full((len(image), len(image[0])), False, dtype=bool)
for tp in tPairs:
st = []
st.append(tp[2])
while 0 != len(st):
changeHVPos(hsv, im, tp, st.pop(), hwidth, swidth, vwidth, st)
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def changeHue(image, tPairs, hwidth=18, vwidth=512):
"""This function changes the hue in the image
Parameters
----------
image : numpy.ndarray
An image
huePairs : (int, int)[]
The converion to due to the hues
width :
The width of the hue
Returns
-------
numpy.ndarray
The image with the hue changed
"""
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
for row in hsv:
for pix in row:
for tp in tPairs:
hdst = abs(int(tp[0][0])-pix[0])
if hdst >= 180:
hdst = 180 - hdst
vdst = abs(int(tp[0][2])-pix[2])
if vdst >= 256:
vdst = 256 - vdst
if hdst < hwidth/2 and vdst < vwidth/2:
nh = int(tp[1][0])-tp[0][0] + pix[0]
if nh < 0:
nh += 180
elif nh >= 180:
nh -= 180
pix[0] = nh
#nv = int(tp[1][1])-tp[0][1] + pix[2]
#if nv < 0:
# nv = 0
#elif nv >= 256:
# nv = 255
#pix[2] = nv
break
return cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
def histCenters(hueHst, width=18):
"""This function finds the best color centers at the given hue width
Parameters
----------
hueHst : numpy.uint32
A histagram of hues in the image
width : int
The width to find the best hues in the image
Returns
-------
list
A list of the best centers to use
"""
avgHst = np.zeros(180, dtype=np.float64)
f = fil(width)
for i in xrange(180):
for j in xrange(width):
pos = i+j-(width/2)
if pos < 0:
pos += 180
elif pos >= 180:
pos -= 180
avgHst[i] += f[j]*hueHst[pos]
ouths = [(0, avgHst[0])]
for i in xrange(180):
best = True
for ht in ouths:
dst = abs(i-ht[0])
if dst > 90:
dst = 180 - dst
if dst < width/2 and ht[1] >= avgHst[i]:
best = False
if best == True:
for ht in ouths:
dst = abs(i-ht[0])
if dst > 90:
dst = 180 - dst
if dst < width/2 and ht[1] < avgHst[i]:
ouths.remove(ht)
ouths.append((i, avgHst[i]))
ouths = sorted(ouths, key=lambda ht: ht[1], reverse=True)
return ouths