-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBoxFilter.py
195 lines (171 loc) · 7 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
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
# coding=utf-8
"""
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__ = 'hdmjdp'
import numpy as np
import cv2
from math import sqrt
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.zeros((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)
return finalImg
def boxFilterStd(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
"""
height = img.shape[0]
width = img.shape[1]
intImg_sum = integralImage(img)
finalImg = np.zeros((height, width), np.uint64)
img_square = np.square(img.astype(np.uint64))
intImg_square = integralImage(img_square)
loc = filterSize // 2
for y in range(height):
for x in range(width):
s1 = findArea(intImg_square, (y - loc - 1, x - loc - 1), (y - loc - 1, x + loc),
(y + loc, x - loc - 1), (y + loc, x + loc))
s2_sum = findArea(intImg_sum, (y - loc - 1, x - loc - 1), (y - loc - 1, x + loc),
(y + loc, x - loc - 1), (y + loc, x + loc))
s2 = s2_sum*s2_sum/(filterSize**2)
variance = s1 - s2
std_deviation = sqrt(variance/(filterSize**2))
finalImg.itemset((y, x), std_deviation)
# cv2.imwrite("finalimage11.png", finalImg)
return finalImg
def boxFilter_MeanStd(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
"""
height = img.shape[0]
width = img.shape[1]
intImg_sum = integralImage(img)
finalImg_mean = np.zeros((height, width), np.uint64)
finalImg_stdv = np.zeros((height, width), np.uint64)
img_square = np.square(img.astype(np.uint64))
intImg_square = integralImage(img_square)
loc = filterSize // 2
for y in range(height):
for x in range(width):
s1 = findArea(intImg_square, (y - loc - 1, x - loc - 1), (y - loc - 1, x + loc),
(y + loc, x - loc - 1), (y + loc, x + loc))
s2_sum = findArea(intImg_sum, (y - loc - 1, x - loc - 1), (y - loc - 1, x + loc),
(y + loc, x - loc - 1), (y + loc, x + loc))
mean = s2_sum / (filterSize ** 2)
s2 = s2_sum * mean # s2_sum*s2_sum/(filterSize**2)
variance = s1 - s2
std_deviation = sqrt(variance/(filterSize**2))
finalImg_stdv.itemset((y, x), std_deviation)
finalImg_mean.itemset((y, x), mean)
# cv2.imwrite("finalImg_mean.png", finalImg_mean)
# cv2.imwrite("finalImg_stdv.png", finalImg_stdv)
return finalImg_mean, finalImg_stdv
def main():
"""
Reads in image and handles argument parsing
:return: None
"""
img = readImage("/Volumes/Transcend/高内涵/master/MDA-MB-231-20171212-4x-4D-M2.jpg")
src = cv2.resize(img, (400, 300), interpolation=cv2.INTER_AREA)
# finalImg = boxFilter(src, 201)
# finalImg = boxFilterStd(src, 201)
finalImg_mean, finalImg_stdv = boxFilter_MeanStd(src, 201)
cv2.imshow("finalImg", finalImg_mean)
cv2.waitKey()
if __name__ == "__main__":
main()