/* Given a pair of images and their keypoints, pick the first keypoint
   from one image and find its closest match in the second set of
   keypoints.  Then write the result to a file.
*/
void FindMatches(Image im1, Keypoint keys1, Image im2, Keypoint keys2)
{
    Keypoint k, match;
    Image result;
    int count = 0;

    /* Create a new image that joins the two images vertically. */
    result = CombineImagesVertically(im1, im2);

    /* Match the keys in list keys1 to their best matches in keys2.
    */
    for (k= keys1; k != NULL; k = k->next) {
      match = CheckForMatch(k, keys2);  

      /* Draw a line on the image from keys1 to match.  Note that we
	 must add row count of first image to row position in second so
	 that line ends at correct location in second image.
      */
      if (match != NULL) {
	count++;
	DrawLine(result, (int) k->row, (int) k->col,
		 (int) (match->row + im1->rows), (int) match->col);
      }
    }

    /* Write result image to standard output. */
    WritePGM(stdout, result);
    fprintf(stderr,"Found %d matches.\n", count);
}
Пример #2
0
void _xvImage(int type,
              unsigned char * red,
              unsigned char * green,
              unsigned char * blue,
              int w, int h)
{
#define mindim 300
    float mag;

    char tmpname[100];
    char command[200];

    FILE *fp;

    tmpnam(tmpname);

    fp = fopen(tmpname,"wb");
    if(!fp) {
        fprintf(stderr,"Failed to open temp file %s\n",tmpname);
        return;
    }

    switch(type) {
      case 1:WritePGM(fp,w,h,red); break;
      case 2:WritePPM(fp,w,h,red,green,blue); break;
      case 3:WritePBM(fp,w,h,red); break;
      default:
        fputs("Unknown type in _xvImage",stderr);
    }


    fclose(fp);

    if(w < mindim && h < mindim) {
        mag = w > h ? w : h;
        mag = mindim/mag;
    } else
        mag = 1.0;

    sprintf(command,"xv -RM -raw -expand %g  %s &",mag, tmpname);
    system(command);

#undef mindim
}
Пример #3
0
int R2Image::
Write(const char *filename) const
{
  // Parse input filename extension
  char *input_extension;
  if (!(input_extension = (char*)strrchr(filename, '.'))) {
    fprintf(stderr, "Output file has no extension (e.g., .jpg).\n");
    return 0;
  }
  
  // Write file of appropriate type
  if (!strncmp(input_extension, ".bmp", 4)) return WriteBMP(filename);
  else if (!strncmp(input_extension, ".png", 4)) return WritePNG(filename);
  else if (!strncmp(input_extension, ".ppm", 4)) return WritePPM(filename, 1);
  else if (!strncmp(input_extension, ".pgm", 4)) return WritePGM(filename, 1);
  else if (!strncmp(input_extension, ".pfm", 4)) return WritePFM(filename);
  else if (!strncmp(input_extension, ".jpg", 5)) return WriteJPEG(filename);
  else if (!strncmp(input_extension, ".jpeg", 5)) return WriteJPEG(filename);

  // Should never get here
  fprintf(stderr, "Unrecognized image file extension");
  return 0;
}
Пример #4
0
main(int argc, char *argv[])
{
  int rows, cols;
  unsigned int x1,x2,y1,y2;
  float t1,t2;
  unsigned char *image;
  unsigned char *output;
  float *edge_mag;
  FILE * fp;
  int i,j,x,y;
  float a,b,c,d;
  

  if (argc != 3){
    printf("Usage: Test <input_filename> <output_filename> \n");
    printf("       <input_filename>:   PGM file \n");
    printf("       <output_filename>:  PGM file \n");
    exit(0);              
  }


  printf("Reading PGM.... \n");
  if ((fp=fopen(argv[1], "rb"))==NULL){
    printf("reading error...\n");
    exit(0); 
  }
  ReadPGM(fp,&image,&rows,&cols);
  

  /* you may replace your own applications here */
  

  edge_mag = (float*)malloc(sizeof(float)*(rows*3+1)*(cols*3+2));
  /*
  printf("begin calculting edge_magnitude.... \n");
  EdgeMag(rows, cols, image, edge_mag);
  */
   for (j=0; j<rows*3; j++)
    for (i=0; i<cols*3; i++)
    { if ((i % 3 == 0)&&(j % 3 == 0))  
         {
          edge_mag[j*cols*3+i]  = image[j/3*cols+i/3];    
          }
    }
    
   
/*
   for (i=0; i<rows*3+1; i++)
     for (j=0; j<2; j++)
     {          
       edge_mag[i*(cols*3+2)+cols*3+j]  = edge_mag[i*(cols*3+2)+(cols*3-3+j)];  
     }
   for (i=0; i<cols*3+1; i++)
     {
       edge_mag[rows*3*(cols*3+2)+i]  = edge_mag[(rows*3-3)*(cols*3+2)+i];   
     }
  */ 
    
  // I am tring to solve it by Bilinear interpolation.
  
  
  for (j=0; j<rows*3; j++)
    for (i=0; i<cols*3; i++)
    { 
        if ((i % 3 == 0)&&(j % 3 == 0))  
        {
        i=i;
        }
        else if ((cols*3-i<4)||(rows*3-j<4))
             edge_mag[j*cols*3+i]  = image[j/3*cols+i/3];   
        else
          
        
            {
            x1=(i/3)*3;
            x2=x1+3;
            y1=(j/3)*3;
            y2=y1+3;
            a = ((float)j-(float)y1)/3;
            b = ((float)y2-(float)j)/3;
            c = ((float)i-(float)x1)/3;
            d = ((float)x2-(float)i)/3;
            t1=a*edge_mag[y1*cols*3+x1]+b*edge_mag[y2*cols*3+x1];
            t2=a*edge_mag[y2*cols*3+x1]+b*edge_mag[y2*cols*3+x2];
            //printf("%d\t%d\t%d\t%d\t%lf\t%lf\n" ,x1,x2,y1,y2,t1,t2);
            //system("pause");  
            edge_mag[j*cols*3+i]=c*t2+d*t1;
            //printf("%d\t%lf\t%lf\t%lf\n",i,c,d,c*t2+d*t1);
            //system("pause");  
               
            
              
            }
        
       /* 
      {
        for (y=0; y<3; y++)
         for (x=0; x<3; x++)
         {
            x1=x/3*3;
            x2=x1+3;
            y1=y-(y%3);
            y2=x/3*3;
            t1=(y-y1)/3*edge_mag[(j+y1)*cols*3+i+x1]+(y2-y)/3*edge_mag[(j+y1)*cols*3+i+x2];
            t2=(y-y1)/3*edge_mag[(j+y2)*cols*3+i+x1]+(y2-y)/3*edge_mag[(j+y2)*cols*3+i+x2];         
            printf("%d\t%d\t%d\t%d\t%d\t%d\t",x1,x2,y1,y2,t1,t2);
            system("pause");  
          edge_mag[(j+y)*cols*3+i+x]=(i-x1)/3*(unsigned char)t2+(x2-i)/3*(unsigned char)t1;
         }
      }
      
      */
    }
  
   
  /* end of your application */


  output = (unsigned char*)malloc(sizeof(unsigned char)*(rows*3)*(cols*3));
  for (j=0; j<rows*3; j++)   
    for (i=0; i<cols*3; i++)
      output[j*cols*3+i] = (unsigned char)edge_mag[j*cols*3+i];

  if ((fp=fopen(argv[2], "wb"))==NULL){
    printf("reading error...\n");
    exit(0); 
  }
  printf("Writing PGM....\n");
  WritePGM(rows*3, cols*3, image, output, fp);
  
}
Пример #5
0
main(int argc, char *argv[])
{
  int rows, cols;
  unsigned char *image;
  unsigned char *output;
  float *edge_mag;
  FILE * fp;
  int i,j;
  

  if (argc != 3){
    printf("Usage: Test <input_filename> <output_filename> \n");
    printf("       <input_filename>:   PGM file \n");
    printf("       <output_filename>:  PGM file \n");
    printf("       <output2_filename>: PGM file \n");
    exit(0);              
  }

  
  printf("Reading PGM.... \n");
  if ((fp=fopen(argv[1], "rb"))==NULL){
    printf("reading error...\n");
    exit(0); 
  }
  ReadPGM(fp,&image,&rows,&cols);
  

  /* you may replace your own applications here */
  
  edge_mag = (float*)malloc(sizeof(float)*rows*cols);
  
  /*
  printf("begin calculting edge_magnitude.... \n");
  EdgeMag(rows, cols, image, edge_mag);
  */
  // copy the data
  
  for (j=0; j<rows; j++)
   {
     for (i=0; i<cols; i++)
     { 
          edge_mag[j*cols+i] = image[j*cols+i];
     }
   }
  
  
    otsu(2,2,cols,rows,edge_mag);  
  
  /* end of your application */
  
  
  
  output = (unsigned char*)malloc(sizeof(unsigned char)*cols*rows);

  for (j=0; j<rows; j++)   
    for (i=0; i<cols; i++)
      output[j*cols+i] = (unsigned char)edge_mag[j*cols+i];
 
  
  if ((fp=fopen(argv[2], "wb"))==NULL){
    printf("reading error...\n");
    exit(0); 
  }
  printf("Writing PGM....\n");
  WritePGM(rows, cols, image, output, fp);
  
  /*
  
  output2 = (unsigned char*)malloc(sizeof(unsigned char)*256*256);
  for (j=0; j<256; j++)   
    for (i=0; i<256; i++)
      output2[j*255+i] = (unsigned char)edge_mag_histo[j*255+i];
  
  if ((fp=fopen(argv[3], "wb"))==NULL){
    printf("reading error...\n");
    exit(0); 
  }
  
  printf("Writing PGM....\n");
  
  WritePGM(rows, cols, image, output2, fp);
  */
}