-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopencv_12.py
87 lines (59 loc) · 2.18 KB
/
opencv_12.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
import numpy as np
import cv2
import os
def CargarImagen():
ruta = os.getcwd()
nombreArchivo = r'recursos\monedas.jpg'
rutaAbrir = os.path.join(ruta, nombreArchivo)
imagen = cv2.imread(rutaAbrir)
h, w, c = imagen.shape
factor = 0.85
nAlto, nAncho = int(factor*h), int(factor*w)
imagenT = cv2.resize(imagen, (nAncho, nAlto))
cv2.imshow('img', imagenT)
cv2.moveWindow('img', 0, 0)
return imagenT
def DetectorBordeMoneda(imagen, mostrar=False):
img = imagen.copy()
h, w, c = img.shape
imgGris = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGris, (5, 5), 0)
_, imgThres = cv2.threshold(imgBlur, 210, 255, cv2.THRESH_BINARY_INV)
cv2.circle(imgThres, (375, 240), 65, 255, -1)
imgCanny = cv2.Canny(imgThres, 30, 150)
if mostrar:
cv2.imshow('img gris', imgGris)
cv2.moveWindow('img gris', w, 0)
cv2.imshow('img blur', imgBlur)
cv2.moveWindow('img blur', 2*w, 0)
cv2.imshow('img thres', imgThres)
cv2.moveWindow('img thres', w, h)
cv2.imshow('img canny', imgCanny)
cv2.moveWindow('img canny', 2*w, h)
return imgCanny
def DetectarContorno(imagen, imgCanny):
img = imagen.copy()
h, w, c = img.shape
cnts, _ = cv2.findContours(imgCanny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print("Se han encontrado {} monedas en esta imagen".format(len(cnts)))
cv2.drawContours(img, cnts, -1, (255, 0, 0), 2)
cv2.imshow('img canny', imgCanny)
cv2.moveWindow('img canny', w, 0)
cv2.imshow('img contornos', img)
cv2.moveWindow('img contornos', w, h)
return cnts
def ContarMonedas(imagen, cnts):
img = imagen.copy()
# h, w, c = img.shape
for i, c in enumerate(cnts):
x, y, w, h = cv2.boundingRect(c)
moneda = img[y:y+h, x:x+w]
cv2.imshow('moneda {}'.format(i), moneda)
cv2.waitKey(0)
return None
if __name__ == '__main__':
img = CargarImagen()
imgCanny = DetectorBordeMoneda(img)
cnts = DetectarContorno(img, imgCanny)
ContarMonedas(img, cnts)
cv2.waitKey(0)