//------------------------------------------------------ // module : Tp2-IFT2425.c // author : // date : // version : 1.0 // language: C++ // note : //------------------------------------------------------ // //------------------------------------------------ // FICHIERS INCLUS ------------------------------- //------------------------------------------------ #include #include #include #include #include //------------------------- //--- Windows ------------- //------------------------- #include Display *display; int screen_num; int depth; Window root; Visual* visual; GC gc; /************************************************************************/ /* OPEN_DISPLAY() */ /************************************************************************/ int open_display() { if ((display=XOpenDisplay(NULL))==NULL) { printf("Connection impossible\n"); return(-1); } else { screen_num=DefaultScreen(display); visual=DefaultVisual(display,screen_num); depth=DefaultDepth(display,screen_num); root=RootWindow(display,screen_num); return 0; } } /************************************************************************/ /* FABRIQUE_WINDOW() */ /* Cette fonction crée une fenetre X et l'affiche à l'écran. */ /************************************************************************/ Window fabrique_window(char *nom_fen,int x,int y,int width,int height,int zoom) { Window win; XSizeHints size_hints; XWMHints wm_hints; XClassHint class_hints; XTextProperty windowName, iconName; char *name=nom_fen; if(zoom<0) { width/=-zoom; height/=-zoom; } if(zoom>0) { width*=zoom; height*=zoom; } win=XCreateSimpleWindow(display,root,x,y,width,height,1,0,255); size_hints.flags=PPosition|PSize|PMinSize; size_hints.min_width=width; size_hints.min_height=height; XStringListToTextProperty(&name,1,&windowName); XStringListToTextProperty(&name,1,&iconName); wm_hints.initial_state=NormalState; wm_hints.input=True; wm_hints.flags=StateHint|InputHint; class_hints.res_name=nom_fen; class_hints.res_class=nom_fen; XSetWMProperties(display,win,&windowName,&iconName, NULL,0,&size_hints,&wm_hints,&class_hints); gc=XCreateGC(display,win,0,NULL); XSelectInput(display,win,ExposureMask|KeyPressMask|ButtonPressMask| ButtonReleaseMask|ButtonMotionMask|PointerMotionHintMask| StructureNotifyMask); XMapWindow(display,win); return(win); } /****************************************************************************/ /* CREE_XIMAGE() */ /* Crée une XImage à partir d'un tableau de float (zoom possible) */ /****************************************************************************/ XImage* cree_Ximage(float** mat,int z,int length,int width) { int lgth,wdth,lig,col,zoom_col,zoom_lig; float somme; unsigned char pix; unsigned char* dat; XImage* imageX; /*Zoom positiv*/ /*------------*/ if (z>0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig0) { lgth=length*z; wdth=width*z; dat=(unsigned char*)malloc(lgth*(wdth*4)*sizeof(unsigned char)); if (dat==NULL) { printf("Impossible d'allouer de la memoire."); exit(-1); } for(lig=0;lig (en pgm) //---------------------------------------------------------- float** LoadImagePgm(char* name,int *length,int *width) { int i,j,k; unsigned char var; float** mat; int ta1,ta2,ta3; FILE *fic; char buff[100],stringTmp1[100],stringTmp2[100]; //>Ouverture_Fichier strcpy(buff,name); printf("\n > Ouverture de %s",buff); fic=fopen(buff,"r"); if (fic==NULL) { printf("\n- Grave erreur a l'ouverture de %s -\n",buff); exit(-1); } //>Recuperation_Entete fgets(stringTmp1,100,fic); for(;;) { fread(&var,1,1,fic); if (var==35) fgets(stringTmp2,100,fic); else break; } fseek(fic,-1,SEEK_CUR); fscanf(fic,"%d %d",&ta1,&ta2); fscanf(fic,"%d\n",&ta3); //>Enregistrement_AllocationMemoire *length=ta2; *width=ta1; mat=fmatrix_allocate_2d(*length,*width); //>Chargement Image for(i=0;i<*length;i++) for(j=0;j<*width;j++) { fread(&var,1,1,fic); mat[i][j]=var; } //>End fclose(fic); return(mat); } //---------------------------------------------------------- // Sauvegarde de l'image de nom au format pgm //---------------------------------------------------------- void SaveImagePgm(char* name,float** mat,int lgth,int wdth) { int i,j; char buff[300]; FILE* fic; //>Extension strcpy(buff,name); strcat(buff,".pgm"); //>Ouverture fichier fic=fopen(buff,"wb"); if (fic==NULL) { printf("Probleme dans la sauvegarde de %s",buff); exit(-1); } printf("\n Sauvegarde [%s] (format pgm)",buff); //>Sauvegarde de l'entete fprintf(fic,"P5"); fprintf(fic,"\n# IMG Module"); fprintf(fic,"\n%d %d",wdth,lgth); fprintf(fic,"\n255\n"); //>Enregistrement-- for(i=0;iFermeture fichier fclose(fic); } //---------------------------------------------------------- // Sauvegarde de l'image de nom au format ppm //---------------------------------------------------------- void SaveImagePpm(char* name,float** img,float** vct,int lgth,int wdth) { int i,j; char buff[300]; FILE* fic; //>Extension strcpy(buff,name); strcat(buff,".ppm"); //>Ouverture fichier fic=fopen(buff,"wb"); if (fic==NULL) { printf("Probleme dans la sauvegarde de %s",buff); exit(-1); } printf("\n Sauvegarde [%s] (format ppm)",buff); //>Sauvegarde de l'entete fprintf(fic,"P6"); fprintf(fic,"\n# IMG Module"); fprintf(fic,"\n%d %d",wdth,lgth); fprintf(fic,"\n255\n"); //>Enregistrement-- for(i=0;iFermeture fichier fclose(fic); } //---------------------------------------------------------- // DrawLine[Img] (rowbeg,colbeg)-->(rowend,colend) //---------------------------------------------------------- void DrawLine(float** Img,int colbeg,int rowbeg,int colend,int rowend,int wdth,int lgth) { int k; float cord,pente; float incrow,inccol; float diffrow=(float)(rowend-rowbeg); float diffcol=(float)(colend-colbeg); //Boucles if (fabs(diffrow)>=fabs(diffcol)) { pente=(float)(diffcol)/(float)(diffrow); incrow=diffrow/fabs(diffrow); cord=colbeg; if (rowend>=rowbeg) for(k=rowbeg;k<=rowend;k++) { if ((k>0)&&(cord>0)&&(krowend) for(k=rowbeg;k>=rowend;k--) { if ((k>0)&&(cord>0)&&(k0)&&(cord>0)&&(kfabs(diffrow)) { pente=(float)(diffrow)/(float)(diffcol); inccol=diffcol/fabs(diffcol); cord=rowbeg; if (colend>=colbeg) for(k=colbeg;k<=colend;k++) { if ((k>0)&&(cord>0)&&(kcolend) for(k=colbeg;k>=colend;k--) { if ((k>0)&&(cord>0)&&(k0)&&(cord>0)&&(kLGTH_MIN) { DrawLine(Img,PosX,PosY,NxtPosX,NxtPosY,wdth,lgth); if (LgthArrow>LGTH_MIN_ARROW) { Theta=atan2(PosY-NxtPosY,PosX-NxtPosX); PX=(int)(NxtPosX + Sz * cos(Theta + PI/4.0)); PY=(int)(NxtPosY + Sz * sin(Theta + PI/4.0)); DrawLine(Img,PX,PY,NxtPosX,NxtPosY,wdth,lgth); PX=(int)(NxtPosX + Sz * cos(Theta - PI/4.0)); PY=(int)(NxtPosY + Sz * sin(Theta - PI/4.0)); DrawLine(Img,PX,PY,NxtPosX,NxtPosY,wdth,lgth); } } } //---------------------------------------------------------- // ConvertVelocityFieldInAroowField //---------------------------------------------------------- void ConvertVelocityFieldInAroowField(float*** SeqImgOptFlot,float*** Vx,float*** Vy,int lgth,int wdth,int nbit,int INC) { int i,j,k; for(k=0;kConstante const int NBITER=1000; //>Pour Xwindow XEvent ev; Window win_ppicture1,win_ppicture2,win_ppicture3; XImage *x_ppicture1,*x_ppicture2,*x_ppicture3; char nomfen_ppicture1[100],nomfen_ppicture2[100],nomfen_ppicture3[100]; //>Lis Arguments if(argc<4) { printf("\n\t Usage: %s image1 image2 alpha",argv[0]); printf("\n\t Exemple: %s rubikseq0.pgm rubikseq1.pgm 500",argv[0]); printf("\n\t Exemple: %s salesman0.pgm salesman1.pgm 200",argv[0]); printf("\n\t Exemple: %s carfront0.pgm carfront1.pgm 400",argv[0]); printf("\n\n\n"); return 0; } int zoom=2; //>Load Images_&_Parametre Img1=LoadImagePgm(argv[1],&length,&width); Img2=LoadImagePgm(argv[2],&length,&width); alpha=atof(argv[3]); printf("\n > Alpha=[%.2f]",alpha); fflush(stdout); //>Allocation Memoire SeqImgOptFlot=fmatrix_allocate_3d(NBITER,length,width); OptFl_Vx=fmatrix_allocate_3d(NBITER,length,width); OptFl_Vy=fmatrix_allocate_3d(NBITER,length,width); Ix=fmatrix_allocate_2d(length,width); Iy=fmatrix_allocate_2d(length,width); It=fmatrix_allocate_2d(length,width); VxM=fmatrix_allocate_2d(length,width); VyM=fmatrix_allocate_2d(length,width); //Init for(k=0;k Rempli OptFlotSequence (apr une seq. de flot optique aleatoire) //> les septs lignes suivantes sont a mettre en commentaire for(k=0;k Iteration > [%d/%d] ",k,NBITER); float posx=0.3*cos(3.0*3.14*k/NBITER); float posy=0.3*sin(3.0*3.14*k/NBITER); for(i=5;i {Array Of Vector} ConvertVelocityFieldInAroowField(SeqImgOptFlot,OptFl_Vx,OptFl_Vy,length,width,NBITER,7); //Sauvegarde printf("\n"); SaveImagePpm("Optical_Flot_WithImg",Img2,SeqImgOptFlot[NBITER-1],length,width); SaveImagePgm("Optical_Flot",SeqImgOptFlot[NBITER-1],length,width); //-------------------------------------------------------------------------------- //---------------- Visu sous XWINDOW --------------------------------------------- //-------------------------------------------------------------------------------- //Info printf("\n\n Evolution du Flot Optique estime: "); //>Window creation if (open_display()<0) printf("Graphical Session Error"); sprintf(nomfen_ppicture1,"Image Sequence"); sprintf(nomfen_ppicture2,"Optical Flow"); sprintf(nomfen_ppicture3,"Image + Optical Flow"); win_ppicture1=fabrique_window(nomfen_ppicture1,10,10,width,length,zoom); win_ppicture2=fabrique_window(nomfen_ppicture2,12+(zoom*width),10,width,length,zoom); win_ppicture3=fabrique_window(nomfen_ppicture3,12+(zoom*width),10,width,length,zoom); //>Visu Sequences printf("\n"); for(k=0;kwidth,x_ppicture1->height); XPutImage(display,win_ppicture2,gc,x_ppicture2,0,0,0,0, x_ppicture2->width,x_ppicture2->height); XPutImage(display,win_ppicture3,gc,x_ppicture3,0,0,0,0, x_ppicture3->width,x_ppicture3->height); usleep(10); //si votre machine est lente mettre un nombre moins grand if (k==(NBITER-1)); { XDestroyImage(x_ppicture1); XDestroyImage(x_ppicture2); XDestroyImage(x_ppicture3); } } //--------------- End Graphical Session -------------------- //---------------------------------------------------------- //Liberation Memoire if (SeqImgOptFlot) free_fmatrix_3d(SeqImgOptFlot,NBITER); if (OptFl_Vx) free_fmatrix_3d(OptFl_Vx,NBITER); if (OptFl_Vy) free_fmatrix_3d(OptFl_Vy,NBITER); if (Ix) free_fmatrix_2d(Ix); if (Iy) free_fmatrix_2d(Iy); if (It) free_fmatrix_2d(It); if (VxM) free_fmatrix_2d(VxM); if (VyM) free_fmatrix_2d(VyM); //Return printf("\n C'est fini... \n"); return 0; }