HELP!!
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.
Code:
#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);
exit(1);
}
// 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);
fclose(fp);
exit(1);
}
// 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");
exit(1);
}
fclose(fp);
}
// 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;
exit(1);
}
fprintf(out,"P5\n");
fprintf(out,"%d %d\n", w, h);
fprintf(out,"255\n");
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
fclose(out);
return;
}
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
read_pgm("picture.pgm");
//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
write_pgm("picture.pgm");
return 0;
}