int R2Image::
Read(const char *filename)
{
  // Initialize everything
  if (pixels) { delete [] pixels; pixels = NULL; }
  width = height = nchannels = 0;

  // Parse input filename extension
  char *input_extension;
  if (!(input_extension = (char*)strrchr(filename, '.'))) {
    fprintf(stderr, "Input file has no extension (e.g., .jpg).\n");
    return 0;
  }
  
  // Read file of appropriate type
  if (!strncmp(input_extension, ".bmp", 4)) return ReadBMP(filename);
  else if (!strncmp(input_extension, ".png", 4)) return ReadPNG(filename);
  else if (!strncmp(input_extension, ".ppm", 4)) return ReadPPM(filename);
  else if (!strncmp(input_extension, ".pgm", 4)) return ReadPGM(filename);
  else if (!strncmp(input_extension, ".pfm", 4)) return ReadPFM(filename);
  else if (!strncmp(input_extension, ".jpg", 4)) return ReadJPEG(filename);
  else if (!strncmp(input_extension, ".jpeg", 5)) return ReadJPEG(filename);
  
  // Should never get here
  fprintf(stderr, "Unrecognized image file extension");
  return 0;
}
Beispiel #2
0
/* This reads a PGM file from a given filename and returns the image.
 */
Image ReadPGMFile(char *filename)
{
	FILE *file;

	/* The "b" option is for binary input, which is needed if this is
compiled under Windows.  It has no effect in Linux.
	 */
	file = fopen (filename, "rb");
	if (! file)
		FatalError("Could not open file: %s", filename);

	return ReadPGM(file);
}
Beispiel #3
0
/* Read a PGM file from the given file pointer and return it as a
float Image structure with pixels in the range [0,1].  If the file
contains more than one image, then the images will be returned
linked by the "next" field of the Image data structure.  
See "man pgm" for details on PGM file format.  This handles only
the usual 8-bit "raw" PGM format.  Use xv or the PNM tools (such as
pnmdepth) to convert from other formats.
 */
Image ReadPGM(FILE *fp)
{
	int char1, char2, width, height, max, c1, c2, c3, r, c;
	Image image, nextimage;

	char1 = fgetc(fp);
	char2 = fgetc(fp);
	SkipComments(fp);
	c1 = fscanf(fp, "%d", &width);
	SkipComments(fp);
	c2 = fscanf(fp, "%d", &height);
	SkipComments(fp);
	c3 = fscanf(fp, "%d", &max);

	if (char1 != 'P' || char2 != '5' || c1 != 1 || c2 != 1 || c3 != 1 ||
			max > 255)
		FatalError("Input is not a standarddddd raw 8-bit PGM file.\n"
				"Use xv or pnmdepth to convert file to 8-bit PGM format.\n");

	fgetc(fp);  /* Discard exactly one byte after header. */

	/* Create floating point image with pixels in range [0,1]. */
	image = CreateImage(height, width);
	for (r = 0; r < height; r++)
		for (c = 0; c < width; c++)
			image->pixels[r][c] = ((float) fgetc(fp)) / 255.0;

	/* Check if there is another image in this file, as the latest PGM
standard allows for multiple images. */
	SkipComments(fp);
	if (getc(fp) == 'P') {
		ungetc('P', fp);
		nextimage = ReadPGM(fp);
		image->next = nextimage;
	}
	return image;
}
Beispiel #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);
  
}
Beispiel #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);
  */
}