-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinCounter.py
More file actions
357 lines (292 loc) · 9.89 KB
/
Copy pathCoinCounter.py
File metadata and controls
357 lines (292 loc) · 9.89 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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# -*- coding: utf-8 -*-
"""
Created on Friday Oct 2 2020
@author: Eduardo Carvalho Nunes
e-mail: eduardocarvnunes@gmail.com
"""
#import libraries
import cv2
import numpy as np
import yaml
# load parameter.yaml
def yaml_load():
with open("parameter.yaml") as stream:
param = yaml.safe_load(stream)
return param
def read_image(image_file):
"""
Read an image
Parameters
----------
image_file : String
Image file path
Returns
-------
image : Array of unit8 or None
Returns a color image or return None (if there is an error).
"""
try:
image = cv2.imread(image_file)
return image
except:
print('[ERROR]: could not read image')
return None
def bgr_gray(image):
"""
Convert BGR to Gray using the OpenCV
Parameters
----------
image : Array of uint8
A color image
Returns
-------
image_gray : Array of uint8 or None
Return the grayscale image or return None (if there is an error)
"""
try:
image_gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
return image_gray
except:
print('[ERROR]: could not read image ')
return None
def blur_image(image_gray):
"""
Smoothing Images using Gaussian Blur from the OpenCV library
Parameters
----------
image_gray : Array of uint8
Grayscale image
Returns
-------
blurred : Array of uint8 or None
Returns a grayscale image with a Gaussian Blur Filter
or returns None (if there is an error)
"""
try:
#kernel(17,17)
blurred = cv2.GaussianBlur(image_gray, (17, 17), 0)
if len(blurred.shape) > 2:
print('[ERROR]: Dimension > 2. Is an image gray?')
return None
else:
return blurred
except:
print('[ERROR]: could not convert image')
return None
def detect_circles(image):
"""
Detect cicle using the HoughCircles fuction from OpenCV
Parameters
----------
image : Array of uint8
A grayscale image with a Gaussian Blur Filter
Returns
-------
circles : Array of float32 or None
Returns the detector circles with coordinates and radius
or returns None (if there is an error)
"""
try:
if len(image.shape) > 2:
print('[ERROR]: Dimension > 2. Is an image gray?')
return None
circles = cv2.HoughCircles(image, cv2.HOUGH_GRADIENT,1,
image.shape[0]/8, param1=100,
param2=50,minRadius=0,maxRadius=0)
if len(circles) == 0:
print('[ERROR]: not possible to detect circles')
return None
else:
return circles
except:
print('[ERROR]: could not detect circles')
return None
def segmentation_bin(image_gray, th):
"""
Segments the image using the threshold function of the OpenCV library
Parameters
----------
image_gray : Array of uint8
Grayscale image
th : int
Threshold for segmentation
Returns
-------
image_bin : Array of uint8
Returns a segmented image with a value of 0 (black pixels) and
255 (white pixels) or returns None (if there is an error)
"""
try:
if len(image_gray.shape) > 2:
print('[ERROR]: Dimension > 2. Is an image gray?')
return None
ret, image_bin = cv2.threshold(image_gray, th, 255, cv2.THRESH_BINARY_INV)
return image_bin
except:
print('[ERROR]: could not segmentation image')
return None
def morphological_transformation(image_gray):
"""
apply morphological transformation (CLOSING) of the OpenCV library
reference: https://docs.opencv.org/master/d9/d61/tutorial_py_morphological_ops.html
Parameters
----------
image_gray : Array of uint8
Grayscale image
Returns
-------
image_closing : Array of uint8
Returns an image with morphological transformation (CLOSING)
or returns None (if there is an error)
"""
try:
if len(image_gray.shape) > 2:
print('[ERROR]: Dimension > 2. Is an image gray?')
return None
#kernel 5x5
kernel = np.ones((5,5),np.uint8)
#closing : dilatation followed by Erosion
image_closing = cv2.morphologyEx(image_gray, cv2.MORPH_CLOSE, kernel, iterations=3)
return image_closing
except:
print('[ERROR]: could not detect circles')
return None
def simple_blob_detector(image_morpho):
"""
It uses the SimpleBlobDetector method to detect the coins in an image.
This method is implemented in the OpenCV library
params reference: https://www.youtube.com/watch?v=3UjNRJ8jbXE
Parameters
----------
image_morpho : Array of uint8
image with morphological transformation.
Returns
-------
detector : SimpleBlobDetector or None
Returns keypoints of coins or returns None (if there is an error)
"""
try:
if len(image_morpho.shape) > 2:
print('[ERROR]: Dimension > 2. Is an image gray?')
return None
# params
params = cv2.SimpleBlobDetector_Params()
# load parameter.yaml
param = yaml_load()
# change thresholds
params.minThreshold = param['low_color']
params.maxThreshold = param['max_color']
params.filterByColor = param['threshold']['filterByColor']
params.blobColor = param['threshold']['blobColor']
params.minDistBetweenBlobs = param['threshold']['minDistBetweenBlobs']
params.thresholdStep = param['threshold']['thresholdStep']
params.minRepeatability = param['threshold']['minRepeatability']
# filter by area
params.filterByArea = param['area']['filterByArea']
params.minArea = param['low_area']
params.maxArea = param['area']['maxArea']
# Filter by Circularity
params.filterByCircularity = param['circularity']['filterByCircularity']
params.minCircularity = param['low_circularity'] / 100
# Filter by Convexity
params.filterByConvexity = param['convexity']['filterByConvexity']
params.minConvexity = param['low_convexity'] / 100
# Filter by Inertia
params.filterByInertia = param['inertia']['filterByInertia']
params.minInertiaRatio = param['low_inertia'] / 100
# Create a detector with the parameters
detector = cv2.SimpleBlobDetector_create(params)
# detect coins
keypoints = detector.detect(image_morpho)
if len(keypoints) == 0:
print('[ERROR]: not possible detect coins')
return None
return keypoints
except:
print('[ERROR]: could not detect circles')
return None
def draw_circles_hough(image, circles):
"""
draws the border and center of the detected coins
Parameters
----------
image : Array of uint8
a color image
circles : Array of float32
circle coordinates
Returns
-------
image_final : Array of uint8
image with the border and center drawn
"""
try:
# Convert the circle parameters a, b and r to integers.
detected_circles = np.uint16(np.around(circles))
for pt in detected_circles[0, :]:
a, b, r = pt[0], pt[1], pt[2]
# Draw the circumference of the circle.
image = cv2.circle(image, (a, b), r, (0, 255, 0), 2)
# Draw a small circle (of radius 1) to show the center.
image_final = cv2.circle(image, (a, b), 1, (0, 0, 255), 3)
return image_final
except:
print('[ERROR]: could not draw image')
return None
def draw_key_pts(image, keypoints):
"""
draws the edges of the detected currencies using the drawKeypoints
function of the OpenCV library
reference: https://stackoverflow.com/questions/19748020/visualizing-opencv-keypoints
Parameters
----------
image : Array of uint8
a color image
circles : Array of float32
circle coordinates
Returns
-------
image_final : Array of uint8
image with the border and center drawn
"""
# Draw blobs on our image as green circles
blank = np.zeros((1, 1))
image = cv2.drawKeypoints(image, keypoints, blank, (0, 255, 0),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
for curKey in keypoints:
x=np.int(curKey.pt[0])
y=np.int(curKey.pt[1])
#size = np.int(curKey.size)
image_final = cv2.circle(image,(x,y),2,(255, 0, 0), 3)
return image_final
"""
#test 1
image = read_image('real_original.jpg')
image_gray = bgr_gray(image)
image_seg = blur_image(image_gray)
detected_circles = detect_circles(image_seg)
image_final_real = draw_circles_hough(image, detected_circles)
print('[RESULT] Number of coins detected = ' + str(len(detected_circles[0])))
cv2.imshow("1", image)
cv2.imshow('2', image_gray)
cv2.imshow('3', image_seg)
cv2.imshow('4', image_final_real)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""
"""
#test 2
image = read_image('dolar_original.png')
image_gray = bgr_gray(image)
image_seg = segmentation_bin(image_gray, 25)
image_morpho = morphological_transformation(image_seg)
keypoints = simple_blob_detector(image_morpho)
image_final = draw_key_pts(image, keypoints)
print('[RESULT] Number of coins detected = ' + str(len(keypoints)))
cv2.imshow("1", image)
cv2.imshow('2', image_gray)
cv2.imshow('3', image_seg)
cv2.imshow('4', image_morpho)
cv2.imshow('5', image_final)
cv2.waitKey(0)
cv2.destroyAllWindows()
"""