-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path#17_extract_coins.py
55 lines (44 loc) · 1.71 KB
/
#17_extract_coins.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
import cv2
import numpy as np
img_path = "images/coins.jpg"
image = cv2.imread(img_path)
cv2.imshow("Original", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#! changing the blur kernel size affects the detection
#! like with >>> coins.webp
blurred = cv2.GaussianBlur(gray, (15, 15), 0)
edged = cv2.Canny(blurred, 30, 180)
cv2.imshow("Edged", edged)
#? Finding Contours #
# ! opencv فى قائمة المخرجات وفقا لاصدار مكتبة contours ترتيب ال
if (cv2.__version__)[0] in '24':
contours, _ = cv2.findContours(edged,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
else:
_, contours, _ = cv2.findContours(edged,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
print('=' * 30)
print("{} objects found".format(len(contours)).title())
print('=' * 30)
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
cv2.imshow("Objects Found", image)
#? Extracting Objects One By One #
# * we used the mask in full size of the original image
# * so we can use x and y of the bounding triangele
# * and also so we can draw a circle
mask = np.zeros(image.shape[:2], np.uint8)
for i, c in enumerate(contours):
# ? if taken with PhotoShop
x, y, w, h = cv2.boundingRect(c)
coin = image[y:y+h, x:x+w]
# ? Original Coin
(cX, cY), r = cv2.minEnclosingCircle(c)
cv2.circle(mask, (int(cX), int(cY)), int(r), 255, -1)
cv2.imshow('Mask', mask)
coin_mask = mask[y:y+h, x:x+w]
masked = cv2.bitwise_and(coin, coin, mask=coin_mask)
cv2.imshow("Original & Masked", np.hstack((coin, masked)))
print('Object #{}'.format(i+1))
cv2.waitKey(0)