Thread: histogram equilization alg.

  1. #1
    Registered User
    Join Date
    Apr 2005

    Unhappy histogram equilization alg.

    Hello, I am writing a code for histogram equilization which will take a gray scale pgm image, read it equilize the picture then write it out. Now I am having a lot of errors specially in the read and write file, and an error in: in the type conversion of Npixels=(wXh)/256 and sum_hist = ((sum*Npixels) + .5; also in the use of buffer to output the image. can someone please help.

    #include <iostream.h>
    #include <math.h>
    // read pgm grey scale image into memory cor[i][j]
    void read_pgm(char * file_name)
      	int	 w, h;
       	char  	 buf[80];
    	FILE	 * fp;
    	// open file for reading 
    	if((fp = fopen(file_name, "r")) == NULL) {
    		fprintf(stderr, "readImage: Can't open %s\n", file_name);
       	// Verify that the image is in PGM format. 
       	fgets(buf, 70, fp);
       	if(strncmp(buf,"P5",2) != 0){
          		fprintf(stderr, "The file %s is not in PGM format", file_name);
    	// skip all comment lines 
       	do { fgets(buf, 70, fp); } while(buf[0] == '#');     
    	sscanf(buf, "%d %d", &w, &h);
       	do { fgets(buf, 70, fp); } while(buf[0] == '#');
            // read intensity values into memory 
    	assign_mem(w, h); // assign a 2-D array cor for the image
    	for (int i = 0; i < w; i++)
    	  for (int j = 0; j < h; j++)
    	    cor[i][j] = getc(fp);
    	if (feof(fp) != 0) {
    	  printf("something is wrong with this picture.\n");
    // writes pgm grey scale image into memory cor[i][j]
    void write_pgm(char * file_name)
    	FILE	*out;
    	if ((out = fopen(file_name, "wb")) == NULL) {
    	  cout << "write_pgm: cannot open the file " 
      	       << file_name 
    	       << " for writing" << endl;
    	fprintf(out,"%d %d\n", w, h);
    	for (int i = 0; i < h; i++)
    	  for (int j = 0; j < w; j++)
    	    putc(int(cor[i][j]+0.5), out);
                // cor saves the image information
    int main(){
    	long i;
    	long w[256];
    	long h[256];
    	long sum;
    	long hist[256];
    	long sum_hist[256];
    	long Npixels = (w*h) / 256;	//error
    	//read image
    	//clear histogram to 0
    	 for (i=0; i < Npixels; i++)
    		hist [i] = 0;
    	//calculate the histogram sum
    	 sum = 0;
    	 for (i=0; i<Npixels; i++)
    	    hist[buffer[i]]++;		//error here 
    	for (i = 0; i< 256; i++){
    		sum += hist[i];
    //error here in conversion "="
    		sum_hist [i]= (sum * Npixels) + 0.5;
    	//transfor image using new sum 
    	for (i = 0; i< Npixels; i++){
    		buffer[i] = sum_hist(buffer[i]);  //error
    	//write image
    return 0;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well something more useful than "error" would help.
    I mean, I assume the compiler gave a longer error message describing the problem right?

    > long Npixels = (w*h) / 256; //error
    But you haven't read the file in yet to set w and h

    Where are the definitions of assign_mem and cor for example?

    > cor[i][j] = getc(fp);
    This will blindly store all the newlines it comes across as well.
    Use fgets() to read each line, then extract all the pixel information in that line and store it in your cor array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hue index Histogram
    By nanang in forum C# Programming
    Replies: 2
    Last Post: 05-17-2009, 09:08 PM
  2. a question on producing a pixel histogram of a picture
    By tracyhaha in forum C Programming
    Replies: 3
    Last Post: 04-07-2007, 08:27 AM
  3. Max & Min value and refine histogram
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-20-2002, 09:04 AM
  4. Still can't print a Histogram from fail input
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 07-11-2002, 01:24 PM
  5. Help Me ! Print a histogram From a file
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 07-10-2002, 10:57 AM
Website Security Test