I49 Answer ― Repeated Smoothing


include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "basicImage.c"

int main ( int argc, char* argv[] )
{
  image img, nimg;
  int percent, r, c;

  if ( argc != 4 )
  {
    printf( "saltAndPepper oldImage noisyImage percent\n" );
    exit( EXIT_FAILURE );
  }

  /* read in the image */
  readPGMimage( &img, argv[1] );

  /* create a blank image */
  newImage( &nimg, img.nrows, img.ncols );

  /* get the noize level */
  percent = atoi( argv[3] );

  /* seed the random number generator */
  srand( time(NULL) );

  /* fill in values for the new image */
  for ( r=0; r<img.nrows; r++ )
    for ( c=0; c<img.ncols; c++ )
    {
      /* decide if this pixel gets altered */
      if ( rand()%100 < percent )
      {
        /* if altered, decide if salt or pepper */
        if ( rand()%2 == 0 )
          setPixel( nimg, r, c, 0 );
        else
          setPixel( nimg, r, c, 255 );
       }
       else
         setPixel( nimg, r, c, getPixel( img, r, c ) );
    }

  /* write the image to disk and free memory */
  writePGMimage( nimg, argv[2] );
  freeImage( &img );
  freeImage( &nimg );
}

Comments: