Need help with my function

This is a discussion on Need help with my function within the C Programming forums, part of the General Programming Boards category; I need to turn a gray-scale pgm image into a black and white image, this i did now i need ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Need help with my function

    I need to turn a gray-scale pgm image into a black and white image, this i did now i need to take that image and turn it into a contour image, but something is wrong. It gives me a perfect black and white image but when i put in through the contour function it all goes wrong.

    The function needs to turn each black pixel of the image that is surrounded by 8 black pixels to white The remainder of the pixels keep their original value.

    Here is what i've done

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define INPUT_FILE "mri_1090.pgm"
    #define OUTPUT_FILE "bw1.pmg"
    #define INPUT_FILEBW "bw1.pmg"
    #define OUTPUT_FILEBW "Contour.pmg"
    void contour(FILE* inf, FILE* outf);
    void increaseContrast(FILE* inf, FILE* outf, double fraction);
    int main()
    {
     FILE* infile;
     FILE* outfile;
     FILE* infilecon;
     FILE* outfilecon;
     double fraction=0.35;
    
     infile = fopen(INPUT_FILE, "r" );
     outfile = fopen(OUTPUT_FILE,"w");
     infilecon = fopen(INPUT_FILEBW, "r");
     outfilecon = fopen(OUTPUT_FILEBW, "w");
     
     if(infile==NULL||outfile==NULL)
     {
      printf("File failed to open");
      exit(1);
      }
     increaseContrast(infile,outfile,fraction);
     contour(infilecon, outfilecon);
     return 0;
     }
    void contour(FILE* inf, FILE* outf){
    	char garbage2[5];
    	int width, height, maxIntensity;
    	int i,j;
    	int pixels[256][256];
    	 fscanf(inf,"%s",&garbage2);     
        fscanf(inf,"%d %d",&width,&height);  
        fscanf(inf,"%d",&maxIntensity);
        fprintf(outf, "%s\n", garbage2);
        fprintf(outf, "%d %d\n", width,height);
        fprintf(outf, "%d\n", maxIntensity);
    for(i=0;i<=255;i++){
    	for(j=0;j<=255;j++){
    	fscanf(inf, "%d", &pixels[i][j]);
    	}
    	}
       for(i=1;i<=254; i++){
    	for(j=1;j<=254; j++){
    	  if(pixels[i][j]==0 && pixels[i-1][j-1]==0 && pixels[i-1][j]==0 && 
    	      pixels[i-1][j+1]==0 && pixels[i][j-1]==0 && pixels[i][j+1]==0 && 
    	      pixels[i+1][j-1]==0 && pixels[i+1][j]==0 && pixels[i+1][j+1]==0)
    	  fprintf(outf, "%d", maxIntensity);
    	  else
    	  fprintf(outf, "%d ", pixels[i-1][j-1]);
    	 }
    	 } 
     }
    void increaseContrast(FILE* inf, FILE* outf, double fraction){
     char garbage[5];
     int width,height,maxIntensity;
     int i,j;
     int pixel;
     fraction = 0.35;
     fscanf(inf,"%s",&garbage);     
     fscanf(inf,"%d %d",&width,&height);  
     fscanf(inf,"%d",&maxIntensity);
     fprintf(outf, "%s\n", garbage);
     fprintf(outf, "%d %d\n", width,height);
     fprintf(outf, "%d\n", maxIntensity);
     for(i=0;i<height;i++){
      for(j=0;j<width;j++){
        fscanf(inf,"%d",&pixel);
       if(pixel<maxIntensity*fraction)
        fprintf(outf,"0 ");
       else
        fprintf(outf,"%d ",maxIntensity);
       }
      }
     }
    Last edited by Salem; 03-12-2006 at 10:59 AM. Reason: folding long lines for readability

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    2
    anyone?

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > The function needs to turn each black pixel of the image that is surrounded by 8 black pixels to white
    > fprintf(outf, "%d ", pixels[i-1][j-1]);
    This isn't the pixel which is surrounded.

    > fprintf(outf, "%d", maxIntensity);
    Nor do you write out a space following the digit, so the numbers get mushed together.
    Rather than writing 0 1 say, you end up writing 01

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21