Está en la página 1de 8

Trabajo de Procesamiento digital de

señales
Jonny Cristhian Otero Baca

April 22, 2017

Objetivo:
• Medir perímetros en figuras geométricas usando un software de proce-
samiento de imágenes

Procedimiento
1. Tomar una fotografía de una imagen con figuras geométricas de dis-
tintos colores dibujadas sobre un papel, la imagen deberá contener
un trazo con medidas conocidas que servira como patrón.

2. Convertir la imagen a escala de grises

3. Binarizar la imagen aplicando un valor de umbral adecuado, que


permita distinguir todas las imágenes con claridad.

4. Sobre la imagen anterior, aplicar detección de contornos

5. Identificar las figuras

6. Medir perímetros.

Paso 1:
Fotografía conteniendo las figuras geométricas y la linea de referencia:

I
Figure 1: Imagen con figuras geométricas y figura patrón

Paso 2:
Fotografía convertida a escala de grises

Algoritmo 1 Código con python y OpenCV para convertir a escala de gris


import numpy as np
import cv2
im = cv2 . imread ( FG1 . j p g )
imgray = cv2 . c v t C o l o r ( im , cv2 . COLOR_RGB2GRAY)
cv2 . imwrite ( imgray . png , imgray )

II
Figure 2: Figura 1 convertida a escala de grises

Paso 3:
Binarizar la imagen

Algoritmo 2 Código con python y OpenCV para binarizar imagen


import numpy as np
import cv2
im = cv2 . imread ( FG1 . j p g )
imgray = cv2 . c v t C o l o r ( im , cv2 . COLOR_RGB2GRAY)
cv2 . imwrite ( imgray . png , imgray )
r e t , t h r e s h = cv2 . t h r e s h o l d ( imgray , 1 9 5 , 2 5 5 , 0 )
cv2 . imwrite ( imgblack . png , t h r e s h )

III
Figure 3: Figura 2 convertida en binaria

Paso 4:
Detección de contornos
Binarizar la imagen

Algoritmo 3 Código con Python y OpenCV para detectar y dibujar con-


tornos
import numpy as np
import cv2
im = cv2 . imread ( FG1 . j p g )
imgray = cv2 . c v t C o l o r ( im , cv2 . COLOR_RGB2GRAY)
cv2 . imwrite ( imgray . png , imgray )
r e t , t h r e s h = cv2 . t h r e s h o l d ( imgray , 1 9 5 , 2 5 5 , 0 )
cv2 . imwrite ( imgblack . png , t h r e s h )
im2 , contours , h i e r a r c h y =
cv2 . findContours ( t h r e s h , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE)
cv2 . imwrite ( imgcnt . png , im2 )

IV
Figure 4: Figura 3 aplicando trazado de contornos

Paso 5:
Identificación de figuras

Algoritmo 4 Código con Python y OpenCV para trazar contornos 1 a 1


import numpy as np
import cv2
im = cv2 . imread ( FG1 . j p g )
imgray = cv2 . c v t C o l o r ( im , cv2 . COLOR_RGB2GRAY)
cv2 . imwrite ( imgray . png , imgray )
r e t , t h r e s h = cv2 . t h r e s h o l d ( imgray , 1 9 5 , 2 5 5 , 0 )
cv2 . imwrite ( imgblack . png , t h r e s h )
im2 , contours , h i e r a r c h y = cv2 . findContours (
t h r e s h , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE)
cv2 . imwrite ( imgcnt . png , im2 )
f o r i i n range ( 0 , l e n ( c o n t o u r s ) ) :
f i g c o n t =cv2 . drawContours ( im2 , contours , i , np . a r r a y ( [ 2 5 5 , 0 , 0 ] ) , 2 )
cv2 . imwrite ( cn+ s t r ( i ) + . png , f i g c o n t )

V
Figure 5: Figura 3 aplicando trazado de contornos

De acuerdo a orden de trazado, hacemos una lista con el orden en que


se han detectado las figuras:
1. contorno 0: Contorno Total
2. contorno 1: Cuadrado
3. contorno 2: Octágono
4. contorno 3: Rombo
5. contorno 4: Triangulo rectángulo
6. contorno 5: Pentágono
7. contorno 6: Hexágono
8. contorno 7: Linea patrón
9. contorno 8: Equilátero
10. contorno 9: Rectángulo

Medición de perímetro:

Paso 6:
Medición de perímetros:

VI
Algoritmo 5 Algoritmo competo para detección de figuras y medición de
perímetro
import numpy as np
import cv2
im = cv2 . imread ( ’ FG1 . jpg ’ )
imgray = cv2 . c v t C o l o r ( im , cv2 . COLOR_RGB2GRAY)
cv2 . imwrite ( ’ imgray . png ’ , imgray )
r e t , t h r e s h = cv2 . t h r e s h o l d ( imgray , 1 9 5 , 2 5 5 , 0 )
cv2 . imwrite ( ’ imgblack . png ’ , t h r e s h )
im2 , contours , h i e r a r c h y = cv2 . findContours (
t h r e s h , cv2 . RETR_TREE , cv2 . CHAIN_APPROX_SIMPLE)
cv2 . imwrite ( ’ imgcnt . png ’ , im2 )
f o r i i n range ( 0 , l e n ( c o n t o u r s ) ) :
f i g c o n t =cv2 . drawContours ( im2 , contours , i , np . a r r a y ( [ 2 5 5 , 0 , 0 ] ) , 2 )
cv2 . imwrite ( ’ cn ’ + s t r ( i ) + ’ . png ’ , f i g c o n t )
p r i n t ’ contorno 0 : Contorno T o t a l ’
p r i n t ’ contorno 1 : Cuadrado ’
p r i n t ’ contorno 2 : Octagono ’
p r i n t ’ contorno 3 : Rombo ’
p r i n t ’ contorno 4 : T r i a n g u l o r e c t a n g u l o ’
p r i n t ’ contorno 5 : Pentagono ’
p r i n t ’ contorno 6 : Hexagono ’
p r i n t ’ contorno 7 : Linea patron ’
p r i n t ’ contorno 8 : E q u i l a t e r o ’
p r i n t ’ contorno 9 : Rectangulo ’
p r i n t ’ contorno 1 0 : C i r c u l o ’
f o r i i n range ( 0 , l e n ( c o n t o u r s ) ) :
cnt = contours [ i ]
p e r i m e t e r = 2 . 0 5 ∗ cv2 . arcLength ( cnt , True )/ cv2 . arcLength ( c o n t o u r s [ 7 ] , True )
p r i n t ’ P e r i m e t r o d e l contorno ’ s t r ( i ) + ’ : ’ + s t r ( p e r i m e t e r )
dc2 = cv2 . drawContours ( im2 , contours , − 1,
np . a r r a y ( [ 2 5 5 , 0 , 0 ] ) , 1 )
cv2 . imwrite ( ’ cn . png ’ , dc2 )

VII
' $
Contorno 0: Contorno Total
Contorno 1: Cuadrado
Contorno 2: Octágono
Contorno 3: Rombo
Contorno 4: Triangulo rectángulo
Contorno 5: Pentágono
Contorno 6: Hexágono
Contorno 7: Linea patrón
Contorno 8: Equilátero
Contorno 9: Rectángulo
Contorno 10: Circulo
Perímetro del contorno 0: 64.7228405490356
Perímetro del contorno 1: 15.4555230450189
Perímetro del contorno 2: 13.1190336161656
Perímetro del contorno 3: 11.2843175036067
Perímetro del contorno 4: 11.2425491762919
Perímetro del contorno 5: 12.2011061612950
Perímetro del contorno 6: 12.4426061206995
Perímetro del contorno 7: 2.05000000000000
Perímetro del contorno 8: 9.89633152420338
Perímetro del contorno 9: 14.4715552568856
Perímetro del contorno 10: 11.0963884686110
& %

Conclusiones
• El presente trabajo, muestra la potencia de procesamiento de imá-
genes para la medición de variable físicas.

• La medición de longitudes por medio de camaras, permite obtener


datos de procesos físicos a distancia y con la mínima intervención.

• Se mostró por medio de este ejemplo el uso de OpenCV y Python


como alternativa a Matlab.

VIII

También podría gustarte