-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_features_implementations.py
366 lines (292 loc) · 12.4 KB
/
image_features_implementations.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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# Read README.md to find detailed instructions about the implementation of each feature.
import cv2
import numpy as np
import argparse
from math import exp
def bright(image,beta,output_image_path):
if beta == 0:
factor = -255
elif beta == 0.5:
factor = -127
elif beta == 1.0:
factor = 0
elif beta == 1.5:
factor = 50
elif beta == 2.0:
factor = 100
bright_image = np.zeros(image.shape, image.dtype)
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
bright_image[row, col, cha ] = np.clip(factor + image[row, col, cha],0,255)
cv2.imshow('Bright Image', bright_image)
cv2.imwrite(output_image_path,bright_image)
def contrast(image,alpha,output_image_path):
contrast_image = np.zeros(image.shape, image.dtype)
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
contrast_image[row, col, cha] = alpha * image[row, col, cha]
cv2.imshow('Contrast Image', contrast_image)
cv2.imwrite(output_image_path,contrast_image)
def gaussian(x, sigma):
return exp(-(x**2)/((2*sigma)**2))
def blur(image, sigma,output_image_path):
'''kernel_radius = 1 generates a 3x3 kernel,
kernel_radius = 2 generates a 5x5 kernel, and so on...'''
kernel_radius = 1
kernel_size = 2 * kernel_radius + 1
# compute the actual kernel elements
hkernel = [gaussian(x, sigma) for x in range(kernel_size)]
vkernel = [x for x in hkernel]
kernel2d = [[xh * xv for xh in hkernel] for xv in vkernel]
# normalize the kernel elements
kernelsum = sum([sum(row) for row in kernel2d])
kernel2d = [[x / kernelsum for x in row] for row in kernel2d]
kernel = np.flipud(np.fliplr(kernel2d))
# convolution output
blur_image = np.zeros_like(image)
# Add zero padding to the input image
image_padded = np.zeros((image.shape[0] + 2*kernel_radius, image.shape[1] + 2*kernel_radius, 3))
image_padded[kernel_radius:-kernel_radius, kernel_radius:-kernel_radius,:] = image
# Loop over every pixel of the image
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
blur_image[row, col, cha] = (kernel * image_padded[row: row + kernel_size, col: col + kernel_size, cha]).sum()
cv2.imshow('Blurred Image', blur_image)
cv2.imwrite(output_image_path,blur_image)
def sharpen(image,output_image_path):
sharpen_image = np.zeros(image.shape, image.dtype)
kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]])
image_padded = np.zeros((image.shape[0] + 2 , image.shape[1] + 2 , 3))
image_padded[1:-1, 1:-1, :] = image
# Loop over every pixel of the image
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
sharpen_image[row, col, cha] = (kernel * image_padded[row: row + 3, col: col + 3, cha]).sum()
cv2.imshow('Sharpened Image', sharpen_image)
cv2.imwrite(output_image_path, sharpen_image)
def edge_detection(image,output_image_path):
edge_image = np.zeros(image.shape, image.dtype)
kernel = np.array([[-1, -1, -1], [-1, 8, -1], [-1, -1, -1]])
image_padded = np.zeros((image.shape[0] + 2 , image.shape[1] + 2 , 3))
image_padded[1:-1, 1:-1, :] = image
# Loop over every pixel of the image
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
edge_image[row, col, cha] = (kernel * image_padded[row: row + 3, col: col + 3, cha]).sum()
cv2.imshow('Edge Detection Image', edge_image)
cv2.imwrite(output_image_path, edge_image)
def resize(image,nR,nC,output_image_path):
nrows = image.shape[0]
ncols = image.shape[1]
rescaled_image = np.array([[image[int(nrows * r / nR)][int(ncols * c / nC)] for c in range(nC)] for r in range(nR)])
cv2.imshow('Resized Image', rescaled_image)
cv2.imwrite(output_image_path, rescaled_image)
def scale_point(image,h,w,output_image_path):
height,width,channels =image.shape
new_image=np.zeros((h,w,channels),np.uint8)
sh=h/height
sw=w/width
for i in range(h):
for j in range(w):
x=int(i/sh)
y=int(j/sw)
new_image[i,j]=image[x,y]
cv2.imshow('Point/Nearest Neighbor Rescaled Image', new_image)
cv2.imwrite(output_image_path, new_image)
def scale_bilinear(image,h,w,output_image_path):
height,width,channels =image.shape
new_image=np.zeros((h,w,channels),np.uint8)
value=[0,0,0]
sh=h/height
sw=w/width
for i in range(h):
for j in range(w):
x = i/sh
y = j/sw
p=(i+0.0)/sh-x
q=(j+0.0)/sw-y
x=int(x)-1
y=int(y)-1
for k in range(3):
if x+1<h and y+1<w:
value[k]=int(image[x,y][k]*(1-p)*(1-q)+image[x,y+1][k]*q*(1-p)+image[x+1,y][k]*(1-q)*p+image[x+1,y+1][k]*p*q)
new_image[i, j] = (value[0], value[1], value[2])
cv2.imshow('Bilinear Rescaled Image', new_image)
cv2.imwrite(output_image_path, new_image)
def scale_gaussian(image, kernel_radius,output_image_path):
'''kernel_radius = 1 generates a 3x3 kernel,
kernel_radius = 2 generates a 5x5 kernel, and so on...'''
kernel_size = 2 * kernel_radius + 1
sigma = kernel_radius / 2.
# compute the actual kernel elements
hkernel = [gaussian(x, sigma) for x in range(kernel_size)]
vkernel = [x for x in hkernel]
kernel2d = [[xh * xv for xh in hkernel] for xv in vkernel]
# normalize the kernel elements
kernelsum = sum([sum(row) for row in kernel2d])
kernel2d = [[x / kernelsum for x in row] for row in kernel2d]
kernel = np.flipud(np.fliplr(kernel2d))
# convolution output
gauss_rescaled_image = np.zeros_like(image)
# Add zero padding to the input image
image_padded = np.zeros((image.shape[0] + 2*kernel_radius, image.shape[1] + 2*kernel_radius, 3))
image_padded[kernel_radius:-kernel_radius, kernel_radius:-kernel_radius,:] = image
# Loop over every pixel of the image
for row in range(image.shape[0]):
for col in range(image.shape[1]):
for cha in range(image.shape[2]):
gauss_rescaled_image[row, col, cha] = (kernel * image_padded[row: row + kernel_size, col: col + kernel_size, cha]).sum()
cv2.imshow('Gaussian Rescaled Image', gauss_rescaled_image)
cv2.imwrite(output_image_path,gauss_rescaled_image)
def alpha_composite(foreground, background, alpha,output_image_path):
foreground = foreground.astype(float)
background = background.astype(float)
alpha = alpha.astype(float) / 255
foreground = alpha * foreground
background = (1.0 - alpha) * background
outImage = foreground + background
cv2.imshow('Composite Image', outImage / 255)
cv2.imwrite(output_image_path,outImage)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--image_path',
type=str,
help='Specify the path to the image')
parser.add_argument(
'--feature',
type=str,
help='Specify the feature you want to implement',
choices = ['brightness','contrast','blur','sharpen','edge_detection','resize','scale_point',
'scale_bilinear','scale_gaussian','composite'])
parser.add_argument(
'--output_image_path',
type=str,
help='Specify the path to the output image')
parser.add_argument(
'--alpha',
type=float,
help='Enter the contrast factor',
default = 1.0)
parser.add_argument(
'--beta',
type=float,
help='Enter the brightness factor',
default = 0.0)
parser.add_argument(
'--sigma',
type=float,
help='Enter the value for sigma',
default=0.0)
parser.add_argument(
'--kernel_radius',
type=int,
help='Enter the value for kernel_radius',
default=1)
parser.add_argument(
'--new_image_height',
type=int,
help='Enter the rescale factor',
default=100)
parser.add_argument(
'--new_image_width',
type=int,
help='Enter the rescale factor',
default=200)
parser.add_argument(
'--foreground_image_path',
type=str,
help='Specify the path to the foreground image')
parser.add_argument(
'--background_image_path',
type=str,
help='Specify the path to the background image')
parser.add_argument(
'--mask_image_path',
type=str,
help='Specify the path to the mask image')
args = parser.parse_args()
image = cv2.imread(args.image_path)
feature = args.feature
output_image_path = args.output_image_path
new_image_height = args.new_image_height
new_image_width = args.new_image_width
if feature == 'brightness':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
beta = args.beta
bright(image,beta,output_image_path)
elif feature == 'contrast':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
alpha = args.alpha
contrast(image,alpha,output_image_path)
elif feature == 'blur':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
sigma = args.sigma
blur(image, sigma,output_image_path)
elif feature == 'sharpen':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
sharpen(image,output_image_path)
elif feature == 'edge_detection':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
edge_detection(image,output_image_path)
elif feature == 'resize':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
resize(image, new_image_height , new_image_width,output_image_path)
elif feature == 'scale_point':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
scale_point(image, new_image_height , new_image_width,output_image_path)
elif feature == 'scale_bilinear':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
scale_bilinear(image, new_image_height , new_image_width,output_image_path)
elif feature == 'scale_gaussian':
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
exit(0)
cv2.imshow('Original Image', image)
kernel_radius = args.kernel_radius
scale_gaussian(image, kernel_radius,output_image_path)
elif feature == 'composite':
foreground_image_path = args.foreground_image_path
background_image_path = args.background_image_path
mask_image_path = args.mask_image_path
foreground = cv2.imread(foreground_image_path)
background = cv2.imread(background_image_path)
mask = cv2.imread(mask_image_path)
alpha_composite(foreground,background,mask,output_image_path)
else:
if image is None:
print('Kindly Specify The Correct Image Path. Incorrect Path: ', args.image_path)
print('''Kindly specify the feature you want to implement from the following options: ['brightness','contrast','blur','sharpen','edge_detection','resize','nearest_neighbor_resampling',
'bilinear_resampling','composite']''')
# Wait until user press some key
cv2.waitKey()
# Read README.md to find detailed instructions about the implementation of each feature.