Está en la página 1de 5

Reconocimiento óptico de caracteres con

redes neuronales con Python y OpenCV

JONNY CRISTHIAN OTERO BACA


May 2, 2017

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.

Figure 1: Figura de prueba para detectar caracteres

I
Algoritmo 1 ALGORITMO PARA SEGMENTACIÓN DE IMAGEN
import numpy as np
import cv2

# S e t s max a r e a t o zero t o ensure i t i s c a l c u l a t e d a p p r o p r i a t e l y l a t e r


contMaxArea=0
# S e t s c a l i b r a t e d v a l u e s f o r ensuring minimum h and w a r e a p p r o p r i a t e
Wcal = 100
Hcal = 100

ima = cv2 . imread ( ’ jonny . png ’ )


imgray = cv2 . c v t C o l o r ( ima , 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 , cv2 . THRESH_BINARY_INV)
cv2 . imwrite ( ’ imgblack1 . png ’ , t h r e s h )

th1=cv2 . r e s i z e ( t h r e s h , ( 1 0 , 1 0 ) )
cv2 . imwrite ( ’ imgth1 . png ’ , th1 )

edged = cv2 . Canny ( t h r e s h , 7 5 , 2 0 0 )


cv2 . imwrite ( ’ imgth2 . png ’ , edged )

k e r n e l = cv2 . g e t S t r u c t u r i n g E l e m e n t ( cv2 . MORPH_CROSS, ( 3 , 3 ) )


cv2 . imwrite ( ’ imgth3 . png ’ , k e r n e l )

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)

Figure 2: Imagen con caracteres en escala de grises

Figura binarizada e invertida

Figure 3: Imagen con caracteres en Blanco y negro

III
Figura con contornos dilatados

Figure 4: Imagen binaria con caracteres con los contornos dilatados

Figura segmentada alrededor de cada carácter

Figure 5: Imagen segmentada alrededor de los posibles caracteres

IV
Figura segmentada por subfiguras con posibilidad
de contener caracteres

Figure 6: Figura de prueba para detectar caracteres

También podría gustarte