Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Preprocesamiento y segmentación
Algoritmo:
El siguiente algoritmo se encarga de preprocesar la imagen y de conver-
tirla en una matriz de unos y ceros de dimensiones 5x7 de tal manera que
pueda servir como entrada a una red neuronal.
Figura de Prueba
La figura a ser procesada contiene cinco caracteres en color verde.
I
Algoritmo 1 ALGORITMO PARA SEGMENTACIÓN DE IMAGEN
import numpy as np
import cv2
th1=cv2 . r e s i z e ( t h r e s h , ( 1 0 , 1 0 ) )
cv2 . imwrite ( ’ imgth1 . png ’ , th1 )
d i l a t e d = cv2 . d i l a t e ( t h r e s h , k e r n e l , i t e r a t i o n s = 3 )
cv2 . imwrite ( ’ imgth4 . png ’ , d i l a t e d )
# Finds a l l c o n t o u r s i n d i l a t e d , B&W image t o determine c h a r a c t e r l o c a t i o n
imcont , contours , h i e r a r c h y = cv2 . findContours ( t h r e s h , cv2 . RETR_EXTERNAL, cv2 .CHAIN_APPROX_NONE)
i =0
# For each contour item
f o r contour i n c o n t o u r s :
# [ x , y ,w, h ] v a l u e s determined by bounding r e c t around contour
[ x , y ,w, h]= cv2 . boundingRect ( contour )
# I f s i z e i s t o s m a l l i t can be ignored
i f h<40 or w< 2 0 :
continue
# Determine i f contour has max s i z e because max contour s i z e w i l l be c h a r a c t e r
i f (w∗h) > contMaxArea :
xmax=x
ymax=y
wmax=w
hmax=h
# P r i n t s t h e h e i g t h and width o f max contour a r e a t o LX t e r m i n a l f o r c a l i b r a t i o n
p r i n t wmax
p r i n t hmax
# S e t s bounding r e g i o n t o max width and h e i g h t t o ensure c h a r a c t e r i s seen a p p r o p r i a t e l y
i f wmax<Wcal :
wmax = wmax+( Wcal−wmax)
i f hmax<Hcal :
hmax = hmax+( Hcal−hmax )
# Determines ROI based on max width and h e i g h t
r o i = d i l a t e d [ ymax : ymax+hmax , xmax : xmax+wmax]
cv2 . imwrite ( " r o i "+ s t r ( i ) + " . j p g " , r o i )
#Draws ROI on image and s a v e s image with bounding box t o c h a r a c t e r . j p g
cv2 . r e c t a n g l e ( ima , ( xmax , ymax ) , ( xmax+wmax, ymax+hmax ) , np . a r r a y ( [ 0 , 2 5 5 , 0 ] ) , 3 ) ;
cv2 . imwrite ( ’ c h a r a c t e r . jpg ’ , ima )
# R e s i z e s image t o 10 x10 square and s e t a l l v a l u e s >1 t o 1 so a r r a y i s b i n a r y
r o i s m a l l = cv2 . r e s i z e ( r o i , ( 5 , 7 ) )
r o i s m a l l [ r o i s m a l l >1]=1
# P r i n t s 5X7 matrix o f 1 ’ s and 0 ’ s t o LX Terminal f o r v e r i f i c a t i o n
print ( roismall )
# Reshapes 5X7 t o 1 x35 so i t can be used i n n e u r a l network
charAsArray = r o i s m a l l . reshape ( ( 1 , 3 5II ))
i = i +1
Figura convertida a escala de gris
La imagen se ha convertido en gris mediante el comando
IMGRAY = CV 2. CVT C OLOR ( IMA , CV 2.COLOR_RGB2GRAY)
III
Figura con contornos dilatados
IV
Figura segmentada por subfiguras con posibilidad
de contener caracteres