-
Notifications
You must be signed in to change notification settings - Fork 1
/
BoxFilter.py
133 lines (116 loc) · 4.22 KB
/
BoxFilter.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
"""
Grayscale box filter.
USAGE: BoxFilter.py [--filter_size] [<image name>]
Takes an image filename and filter size (which must be an odd number) and returns blurred image based on filter size.
"""
__author__ = 'hughesj919'
import numpy as np
import cv2
import sys
import getopt
def readImage(filename):
"""
Read in an image file, errors out if we can't find the file
:param filename: The image filename.
:return: The img object in matrix form.
"""
img = cv2.imread(filename, 0)
if img is None:
print('Invalid image:' + filename)
return None
else:
print('Image successfully read...')
return img
def integralImage(img):
"""
Returns the integral image/summed area table. See here: https://en.wikipedia.org/wiki/Summed_area_table
:param img:
:return:
"""
height = img.shape[0]
width = img.shape[1]
int_image = np.zeros((height, width), np.uint64)
for y in range(height):
for x in range(width):
up = 0 if (y-1 < 0) else int_image.item((y-1, x))
left = 0 if (x-1 < 0) else int_image.item((y, x-1))
diagonal = 0 if (x-1 < 0 or y-1 < 0) else int_image.item((y-1, x-1))
val = img.item((y, x)) + int(up) + int(left) - int(diagonal)
int_image.itemset((y, x), val)
return int_image
def adjustEdges(height, width, point):
"""
This handles the edge cases if the box's bounds are outside the image range based on current pixel.
:param height: Height of the image.
:param width: Width of the image.
:param point: The current point.
:return:
"""
newPoint = [point[0], point[1]]
if point[0] >= height:
newPoint[0] = height -1
if point[1] >= width:
newPoint[1] = width -1
return tuple(newPoint)
def findArea(int_img, a, b, c, d):
"""
Finds the area for a particular square using the integral image. See summed area wiki.
:param int_img: The
:param a: Top left corner.
:param b: Top right corner.
:param c: Bottom left corner.
:param d: Bottom right corner.
:return: The integral image.
"""
height = int_img.shape[0]
width = int_img.shape[1]
a = adjustEdges(height, width, a)
b = adjustEdges(height, width, b)
c = adjustEdges(height, width, c)
d = adjustEdges(height, width, d)
a = 0 if (a[0] < 0 or a[0] >= height) or (a[1] < 0 or a[1] >= width) else int_img.item(a[0], a[1])
b = 0 if (b[0] < 0 or b[0] >= height) or (b[1] < 0 or b[1] >= width) else int_img.item(b[0], b[1])
c = 0 if (c[0] < 0 or c[0] >= height) or (c[1] < 0 or c[1] >= width) else int_img.item(c[0], c[1])
d = 0 if (d[0] < 0 or d[0] >= height) or (d[1] < 0 or d[1] >= width) else int_img.item(d[0], d[1])
return a + d - b - c
def boxFilter(img, filterSize):
"""
Runs the subsequent box filtering steps. Prints original image, finds integral image, and then outputs final image
:param img: An image in matrix form.
:param filterSize: The filter size of the matrix
:return: A final image written as finalimage.png
"""
print("Printing original image...")
print(img)
height = img.shape[0]
width = img.shape[1]
intImg = integralImage(img)
finalImg = np.ones((height, width), np.uint64)
print("Printing integral image...")
print(intImg)
cv2.imwrite("integral_image.png", intImg)
loc = filterSize/2
for y in range(height):
for x in range(width):
finalImg.itemset((y, x), findArea(intImg, (y-loc-1, x-loc-1), (y-loc-1, x+loc), (y+loc, x-loc-1), (y+loc, x+loc))/(filterSize**2))
print("Printing final image...")
print(finalImg)
cv2.imwrite("finalimage.png", finalImg)
def main():
"""
Reads in image and handles argument parsing
:return: None
"""
args, img_name = getopt.getopt(sys.argv[1:], '', ['filter_size='])
args = dict(args)
filter_size = args.get('--filter_size')
print("Image Name: " + str(img_name[0]))
print("Filter Size: " + str(filter_size))
img = readImage(img_name[0])
if img is not None:
print "Shape: " + str(img.shape)
print "Size: " + str(img.size)
print "Type: " + str(img.dtype)
boxFilter(img, int(filter_size))
if __name__ == "__main__":
main()