09.05.2013 Views

Introduccion al uso de OpenCV (2) - Cimat

Introduccion al uso de OpenCV (2) - Cimat

Introduccion al uso de OpenCV (2) - Cimat

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Introduccion</strong> <strong>al</strong> <strong>uso</strong> <strong>de</strong> <strong>OpenCV</strong> (2)<br />

Dr. J.B. Hayet<br />

CENTRO DE INVESTIGACIÓN EN MATEMÁTICAS<br />

Agosto 2007<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 1 / 64


Outline<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 2 / 64


Procesamiento <strong>de</strong> imágenes con <strong>OpenCV</strong><br />

<strong>OpenCV</strong> contiene un gran numero <strong>de</strong> funcion<strong>al</strong>ida<strong>de</strong>s entre las mas<br />

clásicas <strong>de</strong> procesamiento <strong>de</strong> imágenes : filtrados, <strong>de</strong>rivación,<br />

operadores morfológicos. . .<br />

Recorrer la documentación para verificar si una función dada esta<br />

implementada o no.<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 3 / 64


Image Processing<br />

Gradients, Edges and Corners<br />

Sampling, Interpolation and Geometric<strong>al</strong> Transforms<br />

Morphologic<strong>al</strong> Operations<br />

Filters and Color Conversion<br />

Pyramids and the Applications<br />

Image Segmentation, Connected Components and Contour<br />

Retriev<strong>al</strong><br />

Image and Contour Moments<br />

Speci<strong>al</strong> Image Transforms<br />

Histograms<br />

Matching<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 4 / 64


Structur<strong>al</strong> An<strong>al</strong>ysis<br />

Contour Processing<br />

Computation<strong>al</strong> Geometry<br />

Planar Subdivisions<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 5 / 64


Motion An<strong>al</strong>ysis and Object Tracking<br />

Accumulation of Background Statistics<br />

Motion Templates<br />

Object Tracking<br />

Optic<strong>al</strong> Flow<br />

Estimators<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 6 / 64


Pattern Recognition<br />

Object Detection<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 7 / 64


Camera C<strong>al</strong>ibration and 3D Reconstruction<br />

Camera C<strong>al</strong>ibration<br />

Pose Estimation<br />

Epipolar Geometry<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 8 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Outline<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 9 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Conversión entre espacios <strong>de</strong> color<br />

void c v C v t C o l o r ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

i n t co<strong>de</strong> ) ;<br />

Con el co<strong>de</strong> <strong>de</strong> la forma siguiente CV ESPA12ESPA2, don<strong>de</strong> ESPA1<br />

y ESPA2 pue<strong>de</strong>n ser entre : BGR, RGB, XYZ, YCrCb, HSV<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 10 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Umbr<strong>al</strong><br />

Umbr<strong>al</strong> :<br />

double c v T h r e s h o l d ( const CvArr ∗ s r c ,<br />

CvArr ∗ dst , double t h r e s h o l d ,<br />

double max v<strong>al</strong> , i n t t h r e s h t y p e ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 11 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Filtros<br />

void c v F i l t e r 2 D ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

const CvMat∗ k e r n e l ,<br />

CvPoint anchor=c v P o i n t ( −1 , −1));<br />

Ejemplo <strong>de</strong> <strong>uso</strong>:<br />

CvMat∗ k e r n e l = cvCreateMat ( 1 1 , 1 1 , CV 32F ) ;<br />

cvSet ( k e r n e l ,<br />

c v S c a l a r ( 1 . / ( k e r n e l −>rows ∗ k e r n e l −>c o l s ) ) ) ;<br />

c v F i l t e r 2 D (A, B, k e r n e l ,<br />

c v P o i n t ( k e r n e l −>c o l s /2 ,<br />

k e r n e l −>rows / 2 ) ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 12 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Filtros : smoothing<br />

Una función <strong>de</strong> smoothing mas especi<strong>al</strong>izada que cvFilter2D :<br />

void cvSmooth ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

i n t smoothtype=CV GAUSSIAN ,<br />

i n t param1=3, i n t param2=0,<br />

double param3=0, double param4=0 ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 13 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Filtros : smoothing<br />

Tipos <strong>de</strong> filtraje con cvSmooth:<br />

# CV BLUR NO SCALE<br />

# CV BLUR ( s i m p l e b l u r )<br />

# CV GAUSSIAN ( g a u s s i a n b l u r )<br />

# CV MEDIAN ( median b l u r )<br />

# CV BILATERAL<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 14 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Derivadas y bordos<br />

Sobel, Canny<br />

void c v S o b e l ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

i n t xor<strong>de</strong>r , i n t yor<strong>de</strong>r ,<br />

i n t a p e r t u r e s i z e =3);<br />

void cvCanny ( const CvArr ∗ image , CvArr ∗ edges ,<br />

double t h r e s h o l d 1 ,<br />

double t h r e s h o l d 2 , i n t a p e r t u r e s i z e =3);<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 15 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Derivadas y bordos<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 16 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Derivadas y bordos<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 17 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Laplaciano<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 18 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Imágenes integr<strong>al</strong>es<br />

Pue<strong>de</strong>n revelarse muy útiles para cálculos muy rápidos <strong>de</strong> sumas<br />

sobre ventanas (las Haar wavelets por ejemplo).<br />

void c v I n t e g r a l ( const CvArr ∗ image , CvArr ∗ sum ,<br />

CvArr ∗ sqsum=NULL , CvArr ∗ t i l t e d s u m=NULL ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 19 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Pirámi<strong>de</strong>s gausianas<br />

Muy útiles para razonamiento multi-esc<strong>al</strong>a :<br />

Down-sampling y filtrado gausiano :<br />

void cvPyrDown ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

i n t f i l t e r =CV GAUSSIAN 5x5 ) ;<br />

Up-sampling :<br />

void cvPyrUp ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

i n t f i l t e r =CV GAUSSIAN 5x5 ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 20 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Pirámi<strong>de</strong>s gausianas<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 21 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Morfología matemática<br />

Flexibilidad en la <strong>de</strong>finición <strong>de</strong>l elemento estructurante<br />

I p l C o n v K e r n e l ∗ c v C r e a t e S t r u c t u r i n g E l e m e n t E x (<br />

i n t c o l s , i n t rows ,<br />

i n t anchx , i n t anchy ,<br />

i n t shape ,<br />

i n t ∗ v a l u e s=NULL ) ;<br />

c v R e l e a s e S t r u c t u r i n g E l e m e n t ( I p l C o n v K e r n e l ∗∗ e l ) ;<br />

Elemento no necesariamente simétrico (anchor)<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 22 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Morfología matemática<br />

Formas soportadas<br />

CV SHAPE RECT<br />

CV SHAPE CROSS<br />

CV SHAPE ELLIPSE<br />

CV SHAPE CUSTOM<br />

En caso <strong>de</strong> CUSTOM, se usan los v<strong>al</strong>ores <strong>de</strong>l arreglo v<strong>al</strong>ues<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 23 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Morfología matemática<br />

Operadores básicos :<br />

void cvEro<strong>de</strong> ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

I p l C o n v K e r n e l ∗ element=NULL , i n t i t e r a t i o n s =1);<br />

void c v D i l a t e ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

I p l C o n v K e r n e l ∗ element=NULL , i n t i t e r a t i o n s =1);<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 24 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Morfología matemática<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 25 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Puntos <strong>de</strong> interés<br />

Todos los métodos implementados son basados en la matriz <strong>de</strong><br />

autocorrelación :<br />

void cvCornerMinEigenV<strong>al</strong> ( const CvArr ∗ img , CvArr ∗ eig ,<br />

i n t b l o c k s i z e , i n t a p s i z e =3);<br />

void c v C o r n e r H a r r i s ( const CvArr ∗ image , CvArr ∗ dst ,<br />

i n t b l o c k s i z e , i n t a p s i z e =3,<br />

double k =0.04 ) ;<br />

void cvFindCornerSubPix ( const CvArr ∗ img ,<br />

CvPoint2D32f ∗ c o r n e r s ,<br />

i n t count ,<br />

CvSize win , CvSize z e r o z o n e ,<br />

C v T e r m C r i t e r i a c r i t e r i a ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 26 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

LK tracking<br />

Método <strong>de</strong> Lucas-Kana<strong>de</strong> para el seguimiento <strong>de</strong> puntos <strong>de</strong> interés<br />

(funciona bien para puntos <strong>de</strong> tipo esquinas, <strong>de</strong>tectados por los<br />

operadores vistos antes)<br />

void cvC<strong>al</strong>cOptic<strong>al</strong>FlowLK ( const CvArr ∗ prev ,<br />

const CvArr ∗ c u r r ,<br />

CvSize w i n s i z e ,<br />

CvArr ∗ v e l x , CvArr ∗ v e l y ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 27 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

LK tracking<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 28 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Histogramas<br />

type<strong>de</strong>f s t r u c t CvHistogram {<br />

i n t type ;<br />

CvArr ∗ b i n s ;<br />

f l o a t t h r e s h [ CV MAX DIM ] [ 2 ] ; /∗ uniform h i s t . ∗/<br />

f l o a t ∗∗ t h r e s h 2 ; /∗ non−uniform h i s t . ∗/<br />

CvMatND mat ; /∗ m a t r i x hea<strong>de</strong>r f o r a r r a y h i s t . ∗/<br />

} CvHistogram ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 29 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Histogramas<br />

CvHistogram ∗ c v C r e a t e H i s t ( i n t dims , i n t ∗ s i z e s ,<br />

i n t type ,<br />

f l o a t ∗∗ r a n g e s=NULL ,<br />

i n t uniform=1 ) ;<br />

void c v R e l e a s e H i s t ( CvHistogram ∗∗ h i s t ) ;<br />

void c v N o r m a l i z e H i s t ( CvHistogram ∗ h i s t , double f a c t o r<br />

void c v C a l c H i s t ( I p l I m a g e ∗∗ image , CvHistogram ∗ h i s t ,<br />

i n t accumulate =0, const CvArr ∗ mask=N<br />

#<strong>de</strong>fine cvQueryHistV<strong>al</strong>ue 1D ( h i s t , i d x 0 ) \<br />

cvGetRe<strong>al</strong>1D ( ( h i s t )−>bins , ( i d x 0 ) )<br />

#<strong>de</strong>fine cvQueryHistV<strong>al</strong>ue 2D ( h i s t , idx0 , i d x 1 ) \<br />

cvGetRe<strong>al</strong>2D ( ( h i s t )−>bins , ( i d x 0 ) , ( i d x 1 ) )<br />

#<strong>de</strong>fine cvQueryHistV<strong>al</strong>ue 3D ( h i s t , idx0 , idx1 , i d x 2 )<br />

cvGetRe<strong>al</strong>3D ( ( h i s t )−>bins , ( i d x 0 ) , ( i d x 1 ) , ( i d x 2 )<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 30 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Histogramas : ejemplo<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 31 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Histogramas : comparación<br />

Varias métricas para histogramas :<br />

double cvCompareHist ( const CvHistogram ∗ h i s t 1 ,<br />

const CvHistogram ∗ h i s t 2 ,<br />

i n t method ) ;<br />

CV COMP CORREL<br />

CV COMP CHISQR<br />

CV COMP INTERSECT<br />

CV COMP BHATTACHARYYA<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 32 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Transformada <strong>de</strong> Hough<br />

Método clásico <strong>de</strong> acumulación para la <strong>de</strong>tección <strong>de</strong> objetos<br />

parametrizados :<br />

CvSeq∗ cvHoughLines2 ( CvArr ∗ img , void ∗ l i n e s t o r a g e ,<br />

i n t method ,<br />

double rho , double theta ,<br />

i n t t h r e s h ,<br />

double par1 =0, double par2 =0);<br />

CvSeq∗ c v H o u g h C i r c l e s ( CvArr ∗ img , void ∗ c i r c s t o r a g e ,<br />

i n t method ,<br />

double dp , double m i n d i s t ,<br />

double par1 =100 ,<br />

double par2 =100 ,<br />

i n t m i n r a d i u s =0,<br />

i n t m a x r a d i u s =0);<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 33 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Transformada <strong>de</strong> Hough : lineas rectas<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 34 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Transformada <strong>de</strong> Hough : círculos<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 35 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Detección <strong>de</strong> caras<br />

Un ejemplo que viene por <strong>de</strong>fault con <strong>OpenCV</strong>:<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 36 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Ada-boost con Haar wavelets<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 37 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

Ada-boost con Haar wavelets<br />

La i<strong>de</strong>a es hacer <strong>de</strong>tección <strong>de</strong> objetos <strong>de</strong> una clase dada usando una<br />

cascada <strong>de</strong> clasificadores débiles (típicamente, por una <strong>de</strong> las Haar<br />

wavelets)<br />

CvSeq∗ c v H a a r D e t e c t O b j e c t s ( const CvArr ∗ image ,<br />

C v H a a r C l a s s i f i e r C a s c a d e ∗ c<br />

CvMemStorage∗ s t o r a g e ,<br />

double s c a l e f a c t o r =1.1 ,<br />

i n t m i n n e i g h b o r s =3,<br />

i n t f l a g s =0,<br />

CvSize m i n s i z e=c v S i z e ( 0 , 0 )<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 38 / 64


Outline<br />

<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 39 / 64


C<strong>al</strong>ibración<br />

<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

Por el momento, viene implementado un metodo clásico basado en<br />

motivo 3D (Tsai) para estimar parametros intrinsecos y extrinsecos.<br />

A<strong>de</strong>mas tiene las herramientas necesarias para usarla muy<br />

simplemente en casos planos (<strong>de</strong>tección <strong>de</strong> motivos <strong>de</strong> tipo ajedrez)<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 40 / 64


C<strong>al</strong>ibración<br />

<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

Función <strong>de</strong> c<strong>al</strong>ibración :<br />

void c v C a l i b r a t e C a m e r a 2 ( const CvMat∗ o b j e c t p o i n t s ,<br />

const CvMat∗ i m a g e p o i n t s ,<br />

const CvMat∗ p o i n t c o u n t s ,<br />

CvSize i m a g e s i z e ,<br />

CvMat∗ i n t r i n s i c m a t r i x ,<br />

CvMat∗ d i s t o r t i o n c o e f f s ,<br />

CvMat∗ r o t v e c t o r s=NULL ,<br />

CvMat∗ t r a n s v e c t o r s=NULL ,<br />

i n t f l a g s =0 ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 41 / 64


C<strong>al</strong>ibración<br />

<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 42 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

C<strong>al</strong>ibración: distorsión radi<strong>al</strong> y tangenci<strong>al</strong><br />

Corrección <strong>de</strong> la distorsión :<br />

void c v U n d i s t o r t 2 ( const CvArr ∗ s r c , CvArr ∗ dst ,<br />

const CvMat∗ i n t r i n s i c m a t r i x ,<br />

const CvMat∗ d i s t o r t i o n c o e f f s ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 43 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

C<strong>al</strong>ibración : distorsión radi<strong>al</strong> y tangenci<strong>al</strong><br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 44 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

C<strong>al</strong>culo <strong>de</strong> transformaciones geométricas<br />

void cvFindHomography ( const CvMat∗ s r c p o i n t s ,<br />

const CvMat∗ d s t p o i n t s ,<br />

CvMat∗ homography ) ;<br />

i n t cvFindFundament<strong>al</strong>Mat ( const CvMat∗ p o i n t s 1 ,<br />

const CvMat∗ p o i n t s 2 ,<br />

CvMat∗ f u n d a m e n t a l m a t r i x ,<br />

i n t method=CV FM RANSAC ,<br />

double param1 =1. ,<br />

double param2 =0.99 ,<br />

CvMat∗ s t a t u s=NULL ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 45 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

Estimación <strong>de</strong> la pose (cámara c<strong>al</strong>ibrada)<br />

void cvPOSIT ( CvPOSITObject∗ p o s i t o b j e c t ,<br />

CvPoint2D32f ∗ i m a g e p o i n t s ,<br />

double f o c a l l e n g t h ,<br />

C v T e r m C r i t e r i a c r i t e r i a ,<br />

CvMatr32f r o t a t i o n m a t r i x ,<br />

CvVect32f t r a n s l a t i o n v e c t o r ) ;<br />

La estructura CvPOSITObject contiene los puntos 3D <strong>de</strong>l objeto<br />

sobre el cu<strong>al</strong> nos loc<strong>al</strong>izamos.<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 46 / 64


<strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

Geometría epipolar<br />

Lineas epipolares correspondientes <strong>de</strong> unos puntos 2D<br />

void c v C o m p u t e C o r r e s p o n d E p i l i n e s (<br />

const CvMat∗ p o i n t s ,<br />

i n t which image ,<br />

const CvMat∗ f u n d a m e n t a l m a t r i x ,<br />

CvMat∗ c o r r e s p o n d e n t l i n e s ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 47 / 64


Outline<br />

<strong>OpenCV</strong> y IPP<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 48 / 64


IPP<br />

<strong>OpenCV</strong> y IPP<br />

IPP (Integrated Performance Primitives) es una parte <strong>de</strong> las<br />

bibliotecas <strong>de</strong> computo <strong>de</strong>sarrolladas por Intel para aprovechar<br />

<strong>de</strong> las características mas avanzadas <strong>de</strong> sus procesadores<br />

(SIMD)<br />

Provee funciones soportando el multi-core, y muy bien<br />

optimizadas<br />

Soporte 64 bits<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 49 / 64


<strong>OpenCV</strong> y IPP<br />

IPP : ¿ dón<strong>de</strong> esta la magia ?<br />

Funcion<strong>al</strong>idad SIMD (single instruction, multiple data) :<br />

registros <strong>de</strong> 64 o 128 bits.<br />

Un registro <strong>de</strong> 128 bits pue<strong>de</strong> contener 4 int, 4 float<br />

Intel: MMX y luego SSE (Streaming SIMD extension)<br />

En gener<strong>al</strong>, adaptado a toda clase <strong>de</strong> problemas vectorizables<br />

(muchos tratamientos loc<strong>al</strong>es in<strong>de</strong>pendientes<br />

Cuidado <strong>al</strong> <strong>al</strong>ineamiento <strong>de</strong> los datos<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 50 / 64


IPP: Scope<br />

<strong>OpenCV</strong> y IPP<br />

Vi<strong>de</strong>o encoding/<strong>de</strong>co- Audio <strong>de</strong>coding/en- Compresión <strong>de</strong> datos<br />

dingcoding<br />

Procesamiento <strong>de</strong> Vision Procesamiento <strong>de</strong><br />

imagenes<br />

señ<strong>al</strong><br />

Vectores y matrices JPEG/JPEG2000 Criptografia<br />

Procesamiento <strong>de</strong>l hablar<br />

Rendido ...<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 51 / 64


Ejemplo<br />

<strong>OpenCV</strong> y IPP<br />

I p p S t a t u s ippiRGBToGray (<br />

const Ipp∗ pSrc , i n t s r c S t e p ,<br />

Ipp∗ pDst , i n t dstStep , I p p i S i z e r o i S i z e ) ;<br />

V<strong>al</strong>ores <strong>de</strong> mod:<br />

8u C3C1R 16u C3C1R 16 s C3C1R 32 f C3C1R<br />

8u AC4C1R 16u AC4C1R 16s AC4C1R 32 f AC4C1R<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 52 / 64


<strong>OpenCV</strong> y IPP<br />

Cuanto se mejoran las performancias<br />

Tipo <strong>de</strong> funcion<strong>al</strong>idad Incremento <strong>de</strong> velocidad<br />

Pirami<strong>de</strong>s gausianas ≈ 3×<br />

Morfología ≈ 3 − 7×<br />

Convolucion linear ≈ 2 − 8×<br />

Conversion color ≈ 1 − 3×<br />

Detección <strong>de</strong> esquinas ≈ 1.8×<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 53 / 64


IPP y <strong>OpenCV</strong><br />

<strong>OpenCV</strong> y IPP<br />

Llamadas a funciones están integradas a una gran parte <strong>de</strong><br />

<strong>OpenCV</strong><br />

Se <strong>de</strong>ci<strong>de</strong> en el tiempo <strong>de</strong> la ejecución si se usa o no IPP<br />

Para que se use, se necesita poner unas variables <strong>de</strong> entorno<br />

(<strong>de</strong> t<strong>al</strong> manera que las librerías <strong>de</strong> IPP estén en el path)<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 54 / 64


IPP y <strong>OpenCV</strong><br />

Verificar que sí se usa IPP:<br />

<strong>OpenCV</strong> y IPP<br />

const char ∗ p l u g i n i n f o= 0 ;<br />

cvGetModuleInfo (0 ,0 ,& p l u g i n i n f o ) ;<br />

i p p i s u s e d= ( p l u g i n i n f o != 0) &&<br />

s t r s t r ( p l u g i n i n f o , ”ipp” )!=0;<br />

Apagar el <strong>uso</strong> <strong>de</strong> IPP y ponerlo otra vez:<br />

cvUseOptimized ( 0 ) ; . . . cvUseOptimized ( 1 ) ;<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 55 / 64


Outline<br />

Usando scripts : los wrappers Python<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 56 / 64


Usando scripts : los wrappers Python<br />

Python y <strong>OpenCV</strong><br />

Uso <strong>de</strong> Python i<strong>de</strong><strong>al</strong> para prototipos, preparar <strong>de</strong>mos<br />

Wrappers generados automáticamente (SWIG)<br />

Inst<strong>al</strong>ado automáticamente (necesita SWIG)<br />

Memoria manejada automáticamente<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 57 / 64


Usando scripts : los wrappers Python<br />

Python y <strong>OpenCV</strong><br />

No hay IplImage acá, solo CvMat<br />

Iteratores<br />

x = cvCreateMat (m, n , type )<br />

f o r row i n x :<br />

# row i s same as t h a t r e t u r n e d by cvGetRow<br />

o :<br />

f o r c o l i n x . c o l r a n g e ( ) :<br />

# c o l i s same as t h a t r e t u r n e d by cvGetCol<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 58 / 64


Usando scripts : los wrappers Python<br />

Python y <strong>OpenCV</strong><br />

Aceso por el operador [], notaciones a la Matlab<br />

c o l = x [ : , i ]<br />

s l i c e = x [ 0 : 1 0 , 0 : 1 0 ]<br />

elem1 = x [ i , j ]<br />

elem2 = x [ i ] [ j ]<br />

x [ : , : ] = 1<br />

x [ 0 : 1 0 , i ] = [ 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ]<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 59 / 64


Usando scripts : los wrappers Python<br />

Python y <strong>OpenCV</strong><br />

Información muy útil en cuanto a la interfase Python :<br />

http://opencvlibrary.sourceforge.net/PythonInterface<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 60 / 64


Outline<br />

Tricks<br />

1 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> procesamiento <strong>de</strong> imágenes<br />

2 <strong>OpenCV</strong> : funcion<strong>al</strong>ida<strong>de</strong>s <strong>de</strong> geometría<br />

3 <strong>OpenCV</strong> y IPP<br />

4 Usando scripts : los wrappers Python<br />

5 Tricks<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 61 / 64


Uso <strong>de</strong> la memoria<br />

Tricks<br />

La memoria esta manejada ”à la C”: cuidado <strong>de</strong> bien liberar los<br />

espacios reservados por las funciones <strong>de</strong> tipo Create o Clone.<br />

c v R e l e a s e I m a g e<br />

cvReleaseMat<br />

. . .<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 62 / 64


Tricks<br />

Soporte <strong>de</strong> funciones en sitio<br />

Checar en la documentacion si la funcion que se usa soporta un<br />

procesamiento en sitio (dst=src). Una mayoría <strong>de</strong> funciones sí lo<br />

soportan.<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 63 / 64


Alineación <strong>de</strong> datos<br />

Tricks<br />

Para optimizar el tratamiento <strong>de</strong> los datos, es mejor <strong>al</strong>inear las lineas<br />

sobre 4 o 8 octetos (es automático con funciones <strong>de</strong> tipo create)<br />

,<br />

J.B. Hayet <strong>OpenCV</strong> parte 2, Agosto 2007 64 / 64

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!