-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefault_Dot_Detector.py
202 lines (146 loc) · 6.18 KB
/
Default_Dot_Detector.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
import cv2
import numpy as np
import os
# current image data - mat
currentImg = None
imgHeightWidth = 540
# default color values - HSV
color_dict_HSV = {
'red1': [[123, 30, 110], [180, 255, 250]],
'red2': [[9, 255, 255], [0, 50, 70]],
'background': [[45, 20, 0], [85, 255, 255]]
}
# image processing parameters
binaryThreshold = 178
kernel1 = np.ones((2, 2), np.uint8)
kernel2 = np.ones((5, 5), np.uint8)
kernel3 = np.ones((3, 3), np.uint8)
kernel4 = np.ones((1, 1), np.uint8)
kernel5 = np.ones((7, 7), np.uint8)
objectAreaMin = 17
objectAreaMax = 250
WhiteRed_Distance = 10
# function 1 : black pixel to white pixel
def convertBlackToWhite(_img):
for y in range(imgHeightWidth):
for x in range(imgHeightWidth):
if _img[y, x] == 0:
_img[y, x] = 255
return _img
# function 2 : detect white object
def WhiteCalculateDefault():
original_img = currentImg.copy()
original_img = cv2.cvtColor(original_img, cv2.COLOR_BGR2HSV)
# 1. convert to image : BGR to HSV
hsv = cv2.cvtColor(original_img, cv2.COLOR_BGR2HSV)
# 2. remove background
mask = cv2.inRange(hsv, np.array(color_dict_HSV['background'][0]), np.array(color_dict_HSV['background'][1]))
masking_result = cv2.bitwise_and(original_img, original_img, mask=~mask)
# 3. convert to image : BGR to Gray
img_gray = cv2.cvtColor(masking_result, cv2.COLOR_BGR2GRAY)
# 4. convert pixel
img_gray = convertBlackToWhite(img_gray.copy())
# 5. image processing
img_gray = cv2.GaussianBlur(img_gray, (3, 3), 0)
(ret, img_binary) = cv2.threshold(img_gray, binaryThreshold, 255, cv2.THRESH_BINARY_INV)
img_binary = cv2.erode(img_binary, kernel1, iterations=1)
img_binary = cv2.dilate(img_binary, kernel2, iterations=1)
img_binary = cv2.erode(img_binary, kernel3, iterations=1)
return img_binary
# function 3 : detect red object
def RedCalculateDefault():
original_img = currentImg.copy()
# 1. convert to image : BGR to HSV
hsv = cv2.cvtColor(original_img, cv2.COLOR_BGR2HSV)
# 2. extract object
mask1 = cv2.inRange(hsv, np.array(color_dict_HSV['red1'][0]), np.array(color_dict_HSV['red1'][1]))
mask2 = cv2.inRange(hsv, np.array(color_dict_HSV['red2'][0]), np.array(color_dict_HSV['red2'][1]))
# 3. image processing
img_binary1 = cv2.erode(mask1, kernel1, iterations=1)
img_binary1 = cv2.dilate(img_binary1, kernel2, iterations=1)
img_binary1 = cv2.erode(img_binary1, kernel3, iterations=1)
img_binary2 = cv2.erode(mask2, kernel1, iterations=1)
img_binary2 = cv2.dilate(img_binary2, kernel2, iterations=1)
img_binary2 = cv2.erode(img_binary2, kernel3, iterations=1)
return img_binary1 + img_binary2
# function 4 : find group area in the input image
def findObjectArea(_img):
cnt, labels, stats, centroids = cv2.connectedComponentsWithStats(_img)
detectedObject = []
for i in range(1, cnt):
(x, y, w, h, area) = stats[i]
if area > objectAreaMin and area < objectAreaMax:
centerX, centerY = int(x + (w / 2)), int(y + (h / 2))
radius = int((w + h) / 2)
data = [centerX, centerY, radius, area]
detectedObject.append(data)
return detectedObject
# function 5 : draw result of white object detection
def drawDetectedWhiteObjectPositions(_whitePos):
original_image = currentImg.copy()
# draw white pos
areaData_white = []
for white in _whitePos :
cv2.circle(original_image, (white[0], white[1]), white[2], (0, 0, 255), 1)
areaData_white.append(white[3])
return original_image, areaData_white
# function 6 : draw result of red object detection
def drawDetectedRedObjectPositions(_redPos):
original_image = currentImg.copy()
# draw red pos
areaData_red = []
for red in _redPos:
cv2.circle(original_image, (red[0], red[1]), red[2], (0, 255, 255), 1)
areaData_red.append(red[3])
return original_image, areaData_red
# function 7 : filtering
def FinalObjectDetection(_whiteImg, _redImg):
# detected Data = [centerX, centerY, radius, area]
whiteData = findObjectArea(_whiteImg)
redData = findObjectArea(_redImg)
# calculate distance between white and red
deleteIndex = []
indexW = 0
for white in whiteData:
for red in redData:
distance = ((white[0] - red[0]) ** 2 + (white[1] - red[1]) ** 2) ** 0.5
if distance < WhiteRed_Distance:
deleteIndex.append(indexW)
indexW += 1
# delete duplicated data
delCount = 0
for i in deleteIndex:
whiteData.pop(i - delCount)
delCount += 1
# draw detected data
# white
finalImageWhite, areaW = drawDetectedWhiteObjectPositions(whiteData)
# red
finalImageRed, areaR = drawDetectedRedObjectPositions(redData)
return finalImageWhite, finalImageRed, areaW, areaR
# server function - ** 변형 필요 **
def excute(input_image) :
# white Object Detection
detectionWhiteResult = WhiteCalculateDefault()
# red Object Detection
detectionRedResult = RedCalculateDefault()
# draw result into image
finalImageWhite, finalImageRed, areaW, areaR = FinalObjectDetection(detectionWhiteResult, detectionRedResult)
if len(areaW) != 0:
whiteInfo = "MIN : " + str(min(areaW)) + " | MAX : " + str(max(areaW)) + " | AVG = " + str(sum(areaW, 0.0) / len(areaW))
print("WHITE INFO = " + whiteInfo)
if len(areaR) != 0:
redInfo = "MIN : " + str(min(areaR)) + " | MAX : " + str(max(areaR)) + " | AVG = " + str(sum(areaR, 0.0) / len(areaR))
print("RED INFO = " + redInfo)
# out images - ** 변형 필요 **
finalImageWhite = cv2.resize(finalImageWhite, (imgHeightWidth * 2, imgHeightWidth * 2))
finalImageRed = cv2.resize(finalImageRed, (imgHeightWidth * 2, imgHeightWidth * 2))
cv2.imwrite("outputWhite.jpg", finalImageWhite)
cv2.imwrite("outputRed.jpg", finalImageRed)
if __name__ == '__main__':
# image path that want to detect
input_imagePath = os.getcwd() + "/HairImages/0881a9e0-ff1a-4a35-93e0-2c946d28cd03.jpg"
currentImg = cv2.imread(input_imagePath)
currentImg = cv2.resize(currentImg, (imgHeightWidth, imgHeightWidth))
# server function
excute(currentImg)