Skip to content

Commit dd47811

Browse files
committed
Add all files
1 parent 63cf9a8 commit dd47811

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+475
-0
lines changed

Recursos/juego1/ejemplo.png

24.5 KB
Loading

Recursos/juego1/photo0.jpg

56.3 KB
Loading

Recursos/juego1/ref0.jpg

272 KB
Loading

Recursos/juego2/ejemplo.png

233 KB
Loading

Recursos/juego2/photo0.png

165 KB
Loading

Recursos/juego2/ref0.png

17.9 KB
Loading

Recursos/juego2/ref1.png

44 KB
Loading

Recursos/juego2/ref2.png

7.49 KB
Loading

juego1.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from skimage.measure import compare_ssim
2+
import argparse
3+
import imutils
4+
import cv2
5+
import matplotlib.pyplot as plt
6+
import numpy as np
7+
plt.rcParams['image.cmap'] = 'gray'
8+
9+
# Cargar la imagen a analizar
10+
imagen = cv2.imread("./Recursos/juego1/photo0.jpg")
11+
imagenc = imagen.copy()
12+
13+
# Imagen de referencia o patron a comparar
14+
referencia = cv2.imread("./Recursos/juego1/ref0.jpg")
15+
refc = referencia.copy()
16+
17+
# Convertir las imagenes a escala de grises
18+
imagenGris = cv2.cvtColor(imagen, cv2.COLOR_BGR2GRAY)
19+
referenciaGris = cv2.cvtColor(referencia, cv2.COLOR_BGR2GRAY)
20+
21+
# Calcular el indice de similitud estructural (SSIM) entre las dos imagenes
22+
# Devuelve la imagen con las diferencias encontradas
23+
(score, diff) = compare_ssim(imagenGris, referenciaGris, full=True)
24+
diff = (diff * 255).astype("uint8")
25+
26+
# Devuelve la similitud estructural determinada
27+
print("SSIM: {}".format(score))
28+
29+
# Resaltar los contornos y regiones donde difieren ambas imagenes
30+
thresh = cv2.threshold(diff, 0, 255,
31+
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
32+
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
33+
cv2.CHAIN_APPROX_SIMPLE)
34+
cnts = imutils.grab_contours(cnts)
35+
36+
# Aplicamos un loop para cada contorno detectado
37+
for c in cnts:
38+
# Calcular el cuadro delimitador
39+
(x, y, w, h) = cv2.boundingRect(c)
40+
cv2.rectangle(imagen, (x, y), (x + w, y + h), (0, 0, 255), 2)
41+
cv2.rectangle(referencia, (x, y), (x + w, y + h), (0, 0, 255), 2)
42+
43+
# Mostrar resultados
44+
f, axes = plt.subplots(1, 3, figsize=(16, 5))
45+
axes[0].imshow(refc)
46+
axes[0].set_title('Referencia')
47+
axes[1].imshow(imagenc)
48+
axes[1].set_title('Imagen')
49+
axes[2].imshow(diff)
50+
axes[2].set_title('Diferencias')
51+
52+
for ax in axes:
53+
ax.axis('off')
54+
plt.show()
55+
56+
#Guardar imagen
57+
cv2.imwrite("./Recursos/juego1/ejemplo.png",diff)
58+
59+
#Bibliografia: https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/

juego2.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import cv2
2+
import numpy as np
3+
from matplotlib import pyplot as plt
4+
5+
# Imagen a analizar
6+
imagenColor = cv2.imread('./Recursos/juego2/photo0.png')
7+
imagenGris = cv2.cvtColor(imagenColor, cv2.COLOR_BGR2GRAY)
8+
9+
cont=0 #Cantidad de rectangulos
10+
tol=10 #Tolerancia para evitar duplicado en el conteo de rectangulos
11+
12+
# Plantillas a detectar
13+
for i in range(3):
14+
path = './Recursos/juego2/ref' + str(i) +'.png'
15+
template = cv2.imread(path,0)
16+
w, h = template.shape[::-1]
17+
18+
# Plantilla de comparacion
19+
res = cv2.matchTemplate(imagenGris,template,cv2.TM_CCOEFF_NORMED)
20+
threshold = 0.8
21+
loc = np.where( res >= threshold)
22+
23+
listPt=list()
24+
#Detectar y dibujar rectangulos
25+
for pt in zip(*loc[::-1]):
26+
cv2.rectangle(imagenColor, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
27+
listPt.append(([pt[0],pt[1]]))
28+
29+
#Contar rectangulos
30+
listPt.sort()
31+
for i in range (len(listPt)-1):
32+
valor = abs(listPt[i+1][1] - listPt[i][1]) + abs(listPt[i+1][0] - listPt[i][0])
33+
if (valor > tol) or (i==0):
34+
cont+=1
35+
36+
#Mostrar y guardar resultados
37+
cv2.imwrite("./Recursos/juego2/ejemplo.png",imagenColor)
38+
print("Cantidad de rectangulos detectados: ",cont)
39+
40+
#Bibliografia: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html

0 commit comments

Comments
 (0)