I am working on two different programs that use pgm pictures and make changes to each picture using histogram equilization and connected componet labeling. Now I am having 3 errors which eventhough i tried different things I can fix. Here there are:
histogram equilization
error: local variables h, w used wout been initialize(lines in bold)
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
using namespace std;
void read_pgm(char * file_name)
{ int w, h;
char buf[80];
FILE * fp;
int cor [256][256];
// 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)
{
//int h,w;
FILE *out;
int cor[256][256],h,w;
if ((out = fopen(file_name, "wb")) == NULL) {
cout << "write_pgm: cannot open the file "<<file_name<<" for writing";
//cout << file_name ;
//cout << " for writing";
//cout << 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(){
//read image
read_pgm("lena.pgm");
int h,w;
char buf[80];
long i;
long sum;
long hist[256];
long sum_hist[256];
long Npixels = (w*h);
float formula = Npixels / 255;
//clear histogram to 0
for (i=0; i < 256; i++)
hist [i] = 0;
//calculate the histogram sum
for (i=0; i<Npixels; i++)
hist[buf[i]]++;
sum = 0;
for (i = 0; i< 256; i++){
sum += hist[i];
sum_hist [i]= (sum * formula) + 0.5;
}
//transfor image using new sum
for (i = 0; i< Npixels; i++){
buf[i] = (char) sum_hist[buf[i]];
}
//write image
write_pgm("newlena.pgm");
return 0;
}
component labeling
using same read and write function as above program
error=Left of .getValue must have class/structure/union type(bold)
Code:
int main(){
FILE *fp;
read_pgm("lena.pgm");
int i,j;
int w, h;
int cor[256][256];
int pixels[256];
int thresh;
/******************** For Connected Components Only ********************
Applying the Component Labeling algorithm on the input image.
*/
//Step 1- Fill the Intensity array backward ..That is, 255,254,253 with increasing index
//These value will work as label for us. Maximum label range can be 255
for ( i=0;i<256;i++) //error message name lookup of 'i' change for new ISO 'for' scope
{ pixels[i]= 255-i; }
/***************For Connected Components Labeling only ******************
//Step 2- Apply the Thresholding on input image*/
for( i=0; i<w; i++)
{
for(j=0; j<h; j++)
{ if(cor[i][j] < thresh) {
cor[i][j] = 255; }
else {
cor[i][j] = 0; }
}
}
// Now we will label the components here
for ( i=0; i<w;i++)
{
for(j=0;j<h;j++)
{ if(fp.getValue(i,j)==0) {
//found a component by its neighbors... }
}
}
//Step 2-Map the image data with the Intensity array
for ( i=0;i<w;i++)
{ for (int j=0;j<h;j++){
cor[i][j] = pixels[cor[i][j]];
}
}
return 0;}