Thread: variable initialization error...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    10

    variable initialization error...


    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;}

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> local variables h, w used wout been initialize(lines in bold)
    I'm just curious. What is your interpretation of these warnings?

    gg

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1)Here is a description of fprintf():
    int fprintf (FILE * stream , const char * format [ , argument , ...] );

    Print formatted data to a stream.
    Prints to the specified stream a sequence of arguments formatted as the format argument specifies.
    You never initialized h or w, nor did you ever assign them any values, yet your are trying to output them to a file:
    Code:
    int cor[256][256],h,w;
    ...
    ...
    fprintf(out,"%d %d\n", w, h);
    What you are doing is equivalent to this:
    Code:
    int w, h;
    cout<<w<<h;
    2) You normally use objects like this:

    Apple myApple;
    myApple.someFunction();

    If you have a pointer to an object:

    Apple* ptr;
    Apple myApple;
    ptr = &Apple;

    you can't use the same notation:

    ptr.someFunction(); //error

    The dot operator is for objects of the class not pointers. If you dereference the pointer: *ptr, then that says to get the object at the address stored in the pointer. Since *ptr is an object, you could use the dot operator like this:

    (*ptr).someFunction();

    but since that notation is so cumbersome, C++ created this notation instead:

    ptr->someFunction();

    I make that mistake myself every so often.
    But, if you are using a compiler like VC++6, and you type the dot after an object name, an autocomplete member list appears, so if you type the dot , and you don't see that members list, then it's a pretty good indication something is wrong.
    Last edited by 7stud; 04-06-2005 at 12:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM