-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
138 lines (105 loc) · 4.08 KB
/
processing.py
File metadata and controls
138 lines (105 loc) · 4.08 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
import cv2 as cv
import numpy as np
import canny
import itertools as IT
from scipy import ndimage
class ImageProcessor:
def __init__(self, frame):
self.frame = frame
"""
Implements OpenCV simple threshold methods
"""
def simple_binary(self, val):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
ret,thresh = cv.threshold(frame, val, 255, cv.THRESH_BINARY)
return thresh
def simple_binary_inv(self, val):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(frame, val, 255, cv.THRESH_BINARY_INV)
return thresh
def simple_trunc(self, val):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(frame, val, 255, cv.THRESH_TRUNC)
return thresh
def simple_tozero(self, val):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(frame, val, 255, cv.THRESH_TOZERO)
return thresh
def simple_tozero_inv(self, val):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(frame, val, 255, cv.THRESH_TOZERO_INV)
return thresh
"""
Implements OpenCV adaptive threshold methods
"""
def adaptive_mean(self):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
thresh = cv.adaptiveThreshold(frame, 255, cv.ADAPTIVE_THRESH_MEAN_C,
cv.THRESH_BINARY, 11, 2)
return thresh
def adaptive_gaussian(self):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
thresh = cv.adaptiveThreshold(frame, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C,
cv.THRESH_BINARY, 11, 2)
return thresh
"""
Implements custom thresholding
"""
def otsu(self):
# TODO: Implement 2D-OTSU for optimisation
# get raw image function
np_img_data_raw = cv.GaussianBlur(self.frame, (5, 5), 0)
cv_histogram = cv.calcHist(np_img_data_raw, [0], None, [256], [0, 256])
cv_histogram_n = cv_histogram.ravel() / cv_histogram.max()
Q = cv_histogram_n.cumsum()
min_func = np.inf
thresh = -1
bins = np.arange(256)
# optimise for max in range
for i in range(0, 256):
w1 = np.split(cv_histogram_n, [i])
p1, p2 = np.hsplit(cv_histogram_n, [i])
s1, s2 = Q[i], Q[255] - Q[i]
w1, w2 = np.hsplit(bins, [i])
m1, m2 = np.dot(p1, w1), np.dot(p2, w2)
v1, v2 = np.dot(((w1 - m1) ** 2), p1) / s1, np.dot(((w2 - m2) ** 2), p2) / s2
within_class_variance = v1 * s1 + v2 * s2
if within_class_variance < min_func:
min_func = within_class_variance;
thresh = i
# Returns the binary frame
return self.simple_binary(thresh)
"""
Performs edge detection on processed image
"""
def sobel_edge(self):
Ix = cv.Sobel(self.frame,cv.CV_64F,1,0,ksize=5)
Iy = cv.Sobel(self.frame,cv.CV_64F,0,1,ksize=5)
frame = np.hypot(Ix, Iy)
frame *= 255.0 / np.max(self.frame)
return self.frame
"""
Canny edge detection on image
"""
def canny(self):
frame = cv.cvtColor(self.frame, cv.COLOR_BGR2GRAY)
result = canny.Canny(frame, 200, 100)
return result.process()
"""
Note: Do not call this method unless input is a binary image
"""
def simple_edge(self):
frame = self.frame
height, width = frame.shape
new_img = np.zeros((height, width), np.uint8)
for x in range(width):
for y in range(height):
try:
if (frame[x + 1, y] - frame[x - 1, y] == 255) or (frame[x + 1, y] - frame[x - 1, y] == 255) or (
frame[x, y + 1] - frame[x, y - 1] == 255) or (frame[x, y + 1] - frame[x, y - 1] == -255):
new_img[x, y] = 255
else:
new_img[x, y] = 0
except IndexError as e:
pass
return new_img