La présentation est en train de télécharger. S'il vous plaît, attendez

La présentation est en train de télécharger. S'il vous plaît, attendez

MATLAB - VECTORISATION

Présentations similaires


Présentation au sujet: "MATLAB - VECTORISATION"— Transcription de la présentation:

1 MATLAB - VECTORISATION
OPTIMISATION DES FONCTIONS MATLAB Moyenne 3x3 dans une image - images de 300x300 : ImS  ImD - fonction Matlab ( m ), 4 méthodes - traduction en C du code Matlab ( mex ) via MCC - fonction C compatible Matlab ( mex ) via MEX Tests sur 2 systèmes, avec Matlab 6.0 (R12) et Visual C/C : - P 233 MHz 64Mo Win 98se - PIII 1 GHz 512Mo Win XP Pro

2 MATLAB – VECTORISATION ( 1 )
% Méthode 1  4 boucles imbriquées ( type langage C ) tic; for L=2:Nl-1, for C=2:Nc-1, Somme=0; for Ls=L-1:L+1, for Cs=C-1:C+1 Somme=Somme+ImS(Ls,Cs); end ImD(L,C)=Somme/9; end; Res(1)=toc;

3 MATLAB – VECTORISATION ( 2 )
% Méthode 2  2 boucles imbriquées, fonction somme vectorielle tic; for L=2:Nl-1, for C=2:Nc-1, Zone3x3=ImS(L-1:L+1,C-1:C+1); ImD(L,C)=sum(Zone3x3(:))/9; end end; Res(2)=toc; SUM

4 MATLAB – VECTORISATION ( 3 )
% Méthode 3  vectorisation : arrangement 3x3 en colonne % sur 9 lignes tic; for L=2:Nl-1, Bloc3xN=ImS(L-1:L+1,:); Col_9=[ [zeros(3,2) Bloc3xN];... [zeros(3,1) Bloc3xN zeros(3,1)];... [Bloc3xN zeros(3,2)] ]; ImD(L,2:Nc-1)=sum(Col_9(:,3:Nc)); end; ImD=ImD/9; Res(3)=toc; A B C A B C

5 MATLAB – VECTORISATION ( 4 )
% Méthode 4  vectorisation précédente et pré-allocations tic; Col_9=zeros(9,Nc+2); Bloc3xN=zeros(3,Nc); for L=2:Nl-1, Bloc3xN=ImS(L-1:L+1,:); Col_9(1:3,3:Nc+2)=Bloc3xN; Col_9(4:6,2:Nc+1)=Bloc3xN; Col_9(7:9,1:Nc)=Bloc3xN; ImD(L,2:Nc-1)=sum(Col_9(:,3:Nc)); end; ImD=ImD/9; Res(4)=toc;

6 MATLAB : C - MEX Appel de fonction C compatible Matlab ( mex )
ImDst = vecteur_c ( ImSrc ); #include "mex.h" #include <stdlib.h> #define INV9 (1./9.) void mexFunction(int Nres,mxArray *Res[],int Narg,const mxArray *Arg[]) { double *Ipo,*Mpo,*Ip,*Mp,Som,Div=INV9; int Nx,Ny,Txy,Nv8,Npix,V8[8]; const int *Dim; Dim=mxGetDimensions(Arg[0]); // dim.image Ny=Dim[0]; Nx=Dim[1]; Txy=Nx*Ny; Ipo=(double *)mxGetData(Arg[0]); // adresse V8[0]=-Ny-1; V8[1]=-Ny; V8[2]=-Ny+1; V8[3]=-1; // zone 3x3 V8[4]=1; V8[5]=Ny-1; V8[6]=Ny; V8[7]=Ny+1; Res[0]=mxCreateDoubleMatrix(Ny,Nx,mxREAL); // creation moyenne Mpo=(double *)mxGetData(Res[0]);

7 MATLAB : C - MEX ( suite ) Mp=Mpo+Ny; Ip=Ipo+Ny; for (Npix=Txy-2*Ny-2; Npix; Npix--) // calcul hors bordures { for (Som=*(++Ip),Nv8=8; Nv8; ) Som+=*(Ip+V8[--Nv8]); *(++Mp)=Som*Div; } Mp=Mpo; Ip=Ipo; for (Txy-=Ny,Npix=Ny; Npix; Npix--) // recopie bordures EW *(Mp+Txy)=*(Ip+Txy); *(Mp++)=*(Ip++); for (Txy=Ny-1,Npix=Nx-2; Npix; Npix--) // recopie bordures NS *(Mp+Txy)=*(Ip+Txy); *(Mp)=*(Ip); Mp+=Ny; Ip+=Ny;

8 MATLAB - CHONOMETRIE Chronométrie des méthodes de codage ( en secondes ) : 4 boucles 2 boucles vector. vector. + pré-alloc. P 233 m 64.7 22.5 2.6 1.6 P 233 mex 4.9 12.1 2.9 2.1 P 1G m 8.0 3.3 0.14 0.08 P 1G mex 0.9 1.9 0.12 0.13 C  mex ( * ) 0.1 0.01 ( * ) : avec appel DLL, création de l’image destination et gestion des bords

9 FIN DE PRESENTATION FIN DE PRESENTATION
RETOUR AU PLAN FIN DE PRESENTATION


Télécharger ppt "MATLAB - VECTORISATION"

Présentations similaires


Annonces Google