-
Notifications
You must be signed in to change notification settings - Fork 0
/
Opening.py
84 lines (74 loc) · 3.06 KB
/
Opening.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
import ImageUtils as util
import GrayScale as grayScale
import Smoothening as smoothening
from datetime import datetime
import math
import time
def erode(input, w, h, window, ww, wh):
wwHalf = int(math.floor(ww / 2))
whHalf = int(math.floor(wh / 2))
output = [(0 , 0, 0)] * w * h
for row in range(whHalf, h - whHalf): # rows in the image except the border
for col in range(wwHalf, w - wwHalf): # columns in the image except the border
minimum = 255
index = row * w + col
for wRow in range(wh):
for wCol in range(ww):
xi = col - wwHalf - 1 + wCol
yi = row - whHalf - 1 + wRow
indexi = yi * w + xi
wIndex = wRow * ww + wCol
iPixel = list(input[indexi])[0]
wPixel = list(window[wIndex])[0]
diff = iPixel - wPixel
if diff < minimum and diff >= 0:
minimum = diff
output[index] = (minimum, minimum, minimum)
return output
def dilate(input, w, h, window, ww, wh):
wwHalf = int(math.floor(ww / 2))
whHalf = int(math.floor(wh / 2))
output = [(0 , 0, 0)] * w * h
for row in range(whHalf, h - whHalf): # rows in the image except the border
for col in range(wwHalf, w - wwHalf): # columns in the image except the border
maximum = 0
index = row * w + col
for wRow in range(wh):
for wCol in range(ww):
xi = col - wwHalf - 1 + wCol
yi = row - whHalf - 1 + wRow
indexi = yi * w + xi
wIndex = wRow * ww + wCol
iPixel = list(input[indexi])[0]
wPixel = list(window[wIndex])[0]
diff = iPixel + wPixel
if diff >= maximum and diff > 0:
maximum = diff
output[index] = (maximum, maximum, maximum)
return output
def opening(input, w, h, ewindow, eww, ewh, dwindow, dww, dwh):
output = erode(input, w, h, ewindow, eww, ewh)
output = dilate(output, w, h, dwindow, dww, dwh)
return output
def main():
print 'executing...'
im = util.open('in/image1.png')
openingFile = "out/opening-" + datetime.now().strftime('%Y%m%d%H%M%S') + ".png"
arr = util.toBytes(im)
start = time.time()
arr = grayScale.averaging(arr)
print 'grayscale : ' + str(time.time() - start)
start = time.time()
arr = smoothening.median(arr, im.width, im.height, 2)
print 'median : ' + str(time.time() - start)
start = time.time()
arr = util.uniformedThresholding(arr, im.width, im.height, (125, 125, 125))
print 'uniformedThresholding : ' + str(time.time() - start)
start = time.time()
window = [(0, 0, 0)] * 3 * 3
opened = opening(arr, im.width, im.height, window, 3, 3, window, 3, 3)
print 'opening : ' + str(time.time() - start)
util.save(openingFile, opened, im.mode, im.size)
print 'done executing...'
if __name__ == "__main__":
main()