Anda di halaman 1dari 71

/* 

 * Image.h 
 * 
 * Copyright (C) 2008, Dipak Chirmade
 * Student, HD SE. 
 * dipak(dot)chirmade(at)gmail(dot)com  
 * 
 * THE SOFTWARE IS PROVIDED "AS­IS" AND WITHOUT WARRANTY OF ANY KIND, 
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * YOU ARE ALLOWED TO USE THIS CODE FOR PERSONAL OR PROFESSIONAL USE 
 * AND IS TOTALLY FREE!     
 * 
 * This file defines the functions to deal with image processing. 
 * Few selective snapshots of processed images are also added.  
 * 
 */ 

#ifndef _IMAGE_H_ 
#define _IMAGE_H_ 

/* 
  Include files in relevance to image processing functions  
*/ 

#include "version.h" 
#include "Bmp.h" 
#include <GL/glut.h> 
#include "Randomin.h" 
#include <math.h> 
#include "noise.h" 

/* 
  All functions are implemented in generic order 
  as shown in menu options 
*/ 

/* 
  Description: Displays the image pixel by pixel 
  Arguments  : r,g,b attributes of an image 
  Returns    : Nothing     
*/ 

void showImage(image rAttr, image gAttr, image bAttr) 

       if((r!=NULL)&&(g!=NULL)&&(b!=NULL)) 
       {   
UINT iRow,jCol; 
for( iRow=0;iRow<screenHeight;iRow++) 
for(jCol=0;jCol<screenWidth;jCol++) 
                  { 
   glBegin(GL_POINTS); 
      glColor3f(float(rAttr[iRow][jCol]/256.0), 
                                float(gAttr[iRow][jCol]/256.0), 
                                float(bAttr[iRow][jCol]/256.0)); 
      glVertex2i(jCol,screenHeight­iRow­1); 
           glEnd(); 

        } 
     

/* 
  Description: Sets white background and plot black points on it 
               Only one point at a time can be plotted  
  Arguments  : Image in 'image' struct link list 
  Returns    : Nothing     
*/ 

void ShowHistogram(image Gray) 
{  
          UINT Height,Width; 
            
          /* 
            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
          */ 
         
          glPointSize(1.0); 
          glBegin(GL_POINTS); 

         for(Width=0;Width<screenWidth;Width++) 
            for(Height=0;Height<screenHeight;Height++) 
            { 
           if(Gray[Height][Width]!=0) 
              glColor3f(0.0,0.0,0.0); 
           else glColor3f(1.0,1.0,1.0); 
              glVertex2f(Height,Width); 
            }            
          glEnd(); 

/* 
  Description: Colored image to Gray image conversion 
               Formula: 0.5*red+0.3*green+0.2*blue=GrayScaleValueForR=G=B 
  Arguments  : R,G,B values which one want to convert. 
  Returns    : Float value after conversion      
*/ 

float ColorToGrayConversion(float Red,float Green,float Blue) 

  return (0.5*Red)+(0.3*Green)+(0.2*Blue);  

/* 
  Description: Contrast correction either from low to high or vice versa 
  Arguments  : Boolen value to toggle from low to high or vice versa 
  Returns    : Nothing 
*/ 

void GetMinmumGrayValue(void); 
void ContrastCorrection(int IsPlus=0) 

     GetMinmumGrayValue(); 
     if(IsPlus) 
        cout<<endl<<"+"; 
     else 
        cout<<endl<<"­"; 

/* 
  Description: Shifts the histogram to right or left which will change the 
               brightness of the resultant 
  Arguments  : Boolen value to toggle from low to high or vice versa 
  Returns    : Nothing      
*/ 

void BrightnessCorrection(int IsPlus=0) 

     if(IsPlus)   
      for(int HWidth=0;HWidth<screenWidth;HWidth++) 
        for(int HHight=0;HHight<screenHeight;HHight++) 
      {         /*towards white*/ 
        rs[HHight][HWidth]=(float)rs[HHight][HWidth]+1.0; 
        gs[HHight][HWidth]=(float)gs[HHight][HWidth]+1.0; 
        bs[HHight][HWidth]=(float)bs[HHight][HWidth]+1.0; 
      } 
      else 
      for(int HWidth=0;HWidth<screenWidth;HWidth++) 
        for(int HHight=0;HHight<screenHeight;HHight++) 
      {         /*towards black*/ 
        rs[HHight][HWidth]=(float)rs[HHight][HWidth]­1.0; 
        gs[HHight][HWidth]=(float)gs[HHight][HWidth]­1.0; 
        bs[HHight][HWidth]=(float)bs[HHight][HWidth]­1.0; 
      } 
 return ; 

/* 
  Description: Shows you histogram of the image 
  Arguments  : Source and buffer image for raw processing  
  Returns    : Processed image 
*/ 

image CalculateHistrogram(image RawBuffer,image SourceImage) 

 
       MaxHistoHeightCount=0; 
      /* 
        initalization of histogram array 
      */  
      for(int HWidth=0;HWidth<screenWidth;HWidth++) 
        for(int HHight=0;HHight<screenHeight;HHight++) 
            RawBuffer[HHight][HWidth]=0; 
 
           int NormalizationFactor=0; 
     for(int HWidth=0;HWidth<screenWidth;HWidth++) 
         { 
           int HCount=0; 

        for(int Hight=0;Hight<screenHeight;Hight++) 
           for(int Width=0;Width<screenWidth;Width++) 
          { 
            if(HWidth==(float)SourceImage[Hight][Width]) 
               HCount++;     
          } 
           if(MaxHistoHeightCount<=HCount) 
               MaxHistoHeightCount=HCount; 
             /* 
               debug statement 
               cout<<endl<<HWidth<<" ="<<HCount; 
             */ 
         } 
             /* 
               shift the max gray level by 1. 
             */ 
           MaxHistoHeightCount=MaxHistoHeightCount­1; 
             /* 
               calculate max histogram counter 
             */ 
           NormalizationFactor=MaxHistoHeightCount/screenHeight; 
     for(int HWidth=0;HWidth<screenWidth;HWidth++) 
         { 
           int HCount=0; 
        for(int Hight=0;Hight<screenHeight;Hight++) 
           for(int Width=0;Width<screenWidth;Width++) 
          { 
            if(HWidth==(float)SourceImage[Hight][Width]) 
               HCount++;   
          } 
            /* 
              calculate normalized histogram array 
            */ 
           int THistoHightCounter=(int)(HCount/NormalizationFactor); 
           for(int HistoHeight=0;HistoHeight<THistoHightCounter;HistoHeight++) 
                RawBuffer[HWidth][HistoHeight]=1; 
         } 
   return RawBuffer; 

/* 
  Description: Calculates MaxValue of gray level 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void GetMaximumGrayValue(void) 

      for(int Height = 0; Height < screenHeight; Height++) 
         for(int Width = 0; Width < screenWidth; Width++) 
              if((float)rs[Height][Width] > MaxGrayValue) 
                 MaxGrayValue = (float)rs[Height][Width]; 
  return; 

/* 
  Description: Calculates MinValue of gray level 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void GetMinmumGrayValue(void) 

      GetMaximumGrayValue(); 
      int tMin=MaxGrayValue; 
      for(int Height=0;Height<screenHeight;Height++) 
         for(int Width=0;Width<screenWidth;Width++) 
              if((float)rs[Height][Width]<tMin) 
                  tMin=(float)rs[Height][Width]; 
      MinGrayValue=tMin; 
   return;         

/* 
  Description: Performs histogram stretch 
  Arguments  : Nothing 
  Returns    : Nothing 
*/ 

void DoHistrogramStretch(void) 

      for(int Height=0;Height<screenHeight;Height++) 
         for(int Width=0;Width<screenWidth;Width++) 
        { 
          rs[Height][Width]=((((float)rs[Height][Width]­MinGrayValue)/(MaxGrayValue­
MinGrayValue))*((float)255­0))+0; 
          gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 
        } 
   return; 

/* 
  Description: Performs histogram shrink 
  Arguments  : Min and Max width of the image  
  Returns    : Nothing 
*/ 

void DoHistrogramShrink(int MaxWidth,int MinWidth) 

          /* 
           __Have to go step by step conversion 
          */ 
          float diff0=float(MaxWidth­MinWidth); 
          float diff1=(float)(diff0/(MaxGrayValue­MinGrayValue)); 
  
      for(int Height=0;Height<screenHeight;Height++) 
         for(int Width=0;Width<screenWidth;Width++) 
        { 
          /* 
           rs[Height][Width]=(float)(((MaxWidth­MinWidth)/(MaxGrayValue­MinGrayValue)) 
           *((float)rs[Height][Width]­MinGrayValue))+MinWidth; 
           */ 
  
          float diff2=(float)((float)rs[Height][Width]­MinGrayValue); 
          float diff3=(float)(diff1*diff2); 
          float diff4=(float)(diff3+MinWidth); 
          rs[Height][Width] = diff4; 
          gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 
        } 
     return; 

/* 
  Description: Convert int number to binary 
  Arguments  : Int number which need to convert into binary 
  Returns    : Nothing       
*/ 

static int BinaryCounter=0; 
int BinaryString[8]={0,0,0,0,0,0,0,0}; 

void BinaryConversion(int number) { 
        int remainder=0; 
        if(number <= 1) { 
                BinaryString[BinaryCounter]=number; 
                BinaryCounter++; 
                return; 
        } 
        remainder = number%2; 
        BinaryConversion(number >> 1); 
        BinaryString[BinaryCounter]=remainder; 
        BinaryCounter++; 
       return; 

/* 
  Description: Performs histogram eqalization 
  Arguments  : Nothing 
  Returns    : Nothing 
*/ 
void DoHistogramEqualization(void) 

     /* 
       clean array 
     */ 
      int IsZeroGrayLevel[256+4]; 
      for(int CleanConuter=0;CleanConuter<256;CleanConuter++) 
          { 
            ProbDesArray[CleanConuter]=0.0; 
            IsZeroGrayLevel[CleanConuter]=0; 
          } 

      float ProbabilityThreshold=0; 
      for(int NormalizationCounter=0;NormalizationCounter<256;NormalizationCounter++) 
          { 
             int tNk=0; 
      for(int Height=0;Height<screenHeight;Height++) 
         for(int Width=0;Width<screenWidth;Width++) 
            { 
              if(NormalizationCounter==(float)rs[Height][Width]) 
                 tNk++; 
            } 
           int TotalNumberOfPixel=screenWidth*screenHeight; 
           float CurrentCalculatedProbabilty=(float)tNk/TotalNumberOfPixel; 
           
           if(CurrentCalculatedProbabilty==0.0) IsZeroGrayLevel[NormalizationCounter]=1; 
                     
           ProbabilityThreshold+=CurrentCalculatedProbabilty; 
            
           /* 
             store the calulate probablities to an probdesarray 
           */ 
           ProbDesArray[NormalizationCounter]=CurrentCalculatedProbabilty; 
          } 
         
           /* 
            _debug maessage to check prob density array and calculate sum of probability 
           */    
      for(int CleanConuter=0;CleanConuter<256;CleanConuter++) 
          { 
          if(CleanConuter) 
            FinalProbDesArray[CleanConuter]=ProbDesArray[CleanConuter]
         +FinalProbDesArray[CleanConuter­1]; 
          else FinalProbDesArray[CleanConuter]=ProbDesArray[CleanConuter]; 
          } 
         /* 
            Calculate or narmalise gray levels 
         */ 
       for(int GrayCounter=0;GrayCounter<256;GrayCounter++) 
          { 
            ProbDesArray[GrayCounter]=(float)(GrayCounter/(float)(255)); 
          } 
       
       if(ProbabilityThreshold==(float)1) 
           cout<<"_Debug Message level 5: Calculation of probablity density function is 
successful..."; 
       else cout<<"_Debug Message level 5: Calculation of probablity density function is not 
successful..."; 
         
      for(int NormalizationCounter=0;NormalizationCounter<256;NormalizationCounter++) 
        { 
          float tDesValue=FinalProbDesArray[NormalizationCounter]; 
          float ReplaceableValue=0.0; 
        
          if(IsZeroGrayLevel[NormalizationCounter]!=1) 
          for(int TillEnd=0;TillEnd<256;TillEnd++)  
          { 
           if((float)tDesValue<=(float)ProbDesArray[TillEnd]) 
           { 
            ReplaceableValue=TillEnd; 
            break; 
           } 
          } 
          else ReplaceableValue=NormalizationCounter; 
         
       for(int Width=0;Width<screenWidth;Width++) 
        for(int Height=0;Height<screenHeight;Height++) 
          { 
            if((int)rs[Height][Width]==NormalizationCounter) 
                rs[Height][Width]=(float)ReplaceableValue; 
                gs[Height][Width]=bs[Height][Width]=rs[Height][Width];  
          } 
        } 

      /* _Wrong approch 
       rawhistogramSrc=CalculateHistrogram(rawhistogramSrc,rs); 
        { 
          int Height; 
          for(int GrayLevel=0;GrayLevel<8;GrayLevel++) 
          for(Height=0;Height<(32*(GrayLevel+1));Height++) 
          { 
            if(Height>=(32*GrayLevel)) 
            if(rawhistogramSrc[Width][Height]) 
               EqualizationArray[GrayLevel]+=1;       
          } 
        } 

       for(int EqCounter=0;EqCounter<8;EqCounter++) 
           cout<<endl<<"­>>"<<EqualizationArray[EqCounter]<<endl; 
      */ 
  
       return; 

/* 
  Description: Preallocation of row buffer which are used in the rest of 
               the program 
  Arguments  : Nothing 
  Returns    : Nothing 
*/ 

void AllocateSpecificationWinBuffer(void) 

    if(specification_bit_1==NULL)   
      specification_bit_1 = matuc(bh.rows,bh.cols);  
    if(specification_bit_2==NULL)   
      specification_bit_2 = matuc(bh.rows,bh.cols);  
    if(specification_bit_3==NULL)   
      specification_bit_3 = matuc(bh.rows,bh.cols);  
    if(specification_bit_4==NULL)   
      specification_bit_4 = matuc(bh.rows,bh.cols);  
    if(specification_bit_5==NULL)   
      specification_bit_5 = matuc(bh.rows,bh.cols);  
    if(specification_bit_6==NULL)   
      specification_bit_6 = matuc(bh.rows,bh.cols);  
    if(specification_bit_7==NULL)   
      specification_bit_7 = matuc(bh.rows,bh.cols);  
    if(specification_bit_8==NULL)   
      specification_bit_8 = matuc(bh.rows,bh.cols);  

 return; 

/* 
  Description: Computers 8 levels of the buffered image           
  Arguments  : Nothing 
  Returns    : Nothing   
*/ 

void ComputeEightLevelbuffers(void) 

 AllocateSpecificationWinBuffer(); 
  
 for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
 { 
   int GrayLevel=(float)rs[Height][Width]; 
   
   /* 
      BinaryConversion(GrayLevel); 
   */ 
     for(int Bit=0;Bit<8;Bit++) 
       { 
          int IsGray=(GrayLevel>>Bit)&1; 
         switch(Bit) 
        { 
         case 0: 
                 specification_bit_1[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 1: 
                 specification_bit_2[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 2: 
                 specification_bit_3[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 3: 
                 specification_bit_4[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 4: 
                 specification_bit_5[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 5: 
                 specification_bit_6[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 6: 
                 specification_bit_7[Height][Width]=(float)(IsGray*255); 
                 break; 
         case 7: 
                 specification_bit_8[Height][Width]=(float)(IsGray*255); 
                 break; 
       } 
       } 
 } 

 return; 

/* 
  Description: Colored image to Gray image conversion 
               Formula: 0.5*red+0.3*green+0.2*blue=GrayScaleValueForR=G=B 
  Arguments  : R,G,B values which one want to convert. 
  Returns    : Float value after conversion      
*/ 

void DoHistogramSpecification(void) 

 /* 
    BinaryConversion((float)123); 
 */ 

  float Pr[256+4]; 
  for(int NormalizationConuter=0;NormalizationConuter<256;NormalizationConuter++) 
  { 
    Pr[NormalizationConuter]=0; 
      
    int tNk=0; 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
        { 
        if(NormalizationConuter==(float)rs[Height][Width]) 
           tNk++; 
        } 
     int TotalNoOfPix=screenWidth*screenHeight; 
     Pr[NormalizationConuter]=(float)tNk/TotalNoOfPix; 
  } 
 
  float Sk[256+4]; 
  for(int NormalizationConuter=0;NormalizationConuter<256;NormalizationConuter++) 
  { 
    if(NormalizationConuter==0) 
       Sk[NormalizationConuter]=Pr[NormalizationConuter]; 
    else Sk[NormalizationConuter]=Pr[NormalizationConuter]+Sk[NormalizationConuter­1]; 
  
    /* 
      Just for debugging 
      cout<<endl<<"@"<<NormalizationConuter<<" Pr:" 
      <<Pr[NormalizationConuter]<<" Sk:"<<Sk[NormalizationConuter]<<endl; 
    */  
  } 
  
  float Pz[256+4],Vk[256+4]; 
  for(int NormalizationConuter=0;NormalizationConuter<256;NormalizationConuter++) 
  { 
     Pz[NormalizationConuter]=(float)(0.0078125*NormalizationConuter)/(float)255; 
     
     if(NormalizationConuter==0) 
        Vk[NormalizationConuter]=Pz[NormalizationConuter]; 
     else Vk[NormalizationConuter]=Pz[NormalizationConuter]+Vk[NormalizationConuter­1]; 

    /* 
      Just for debugging 
      cout<<endl<<"@"<<NormalizationConuter<<" Pz:"<< 
      Pz[NormalizationConuter]<<" Vk:"<<Vk[NormalizationConuter]<<endl; 
    */ 
  } 

  for(int NormalizationConuter=0;NormalizationConuter<256;NormalizationConuter++) 
      Pr[NormalizationConuter]=(float)(NormalizationConuter/(float)255); 

   
  for(int NormalizationConuter=0;NormalizationConuter<256;NormalizationConuter++) 
 { 
   int ReplaceValue=0;  
   for(int TillEnd=0;TillEnd<256;TillEnd++) 
   { 
        
       float TdesiredValue=(float)Sk[NormalizationConuter]; 
       if(TdesiredValue<=Vk[TillEnd]) 
           { 
             ReplaceValue=TillEnd; 
             break; 
           } 
   } 
   
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
     { 
 
            if((int)rs[Height][Width]==NormalizationConuter) 
                rs[Height][Width]=(float)ReplaceValue; 
                gs[Height][Width]=bs[Height][Width]=rs[Height][Width];  
     } 
 } 

 ComputeEightLevelbuffers();  
 
 return; 

/* 
  Description: To get the mean of the image 
  Arguments  : void 
  Returns    : mean value 
*/ 

float GetMeanOfTheImage(void) 

 float Mean=0; 
 int TotalNoOfPix=screenWidth*screenHeight; 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
      Mean=Mean+(float)rs[Height][Width]; 

  return (float)(Mean/TotalNoOfPix); 

/* 
  Description: To get a mean of the ACE window 
  Arguments  : Nothing  
  Returns    : Float value a mean      
*/ 

float VarianceWindow[3][3]={{0,0,0}, 
                            {0,0,0}, 
                            {0,0,0}}; 

float GetMeanOfTheACEWindow(void) 

 float Mean=0; 
 int TotalNoOfPix=3*3; 
    for(int Height=0;Height<3;Height++) 
      for(int Width=0;Width<3;Width++) 
      Mean=Mean+(float)VarianceWindow[Height][Width]; 

  return (float)(Mean/TotalNoOfPix); 

/* 
  Description: To get the variance of the ACE window 
  Arguments  : Nothing 
  Returns    : Float mean value 
*/ 
float GetVarianceOfTheACEWindow(void) 

  float Mean=GetMeanOfTheACEWindow(),Sigma=0; 
  int TotalNoOfPix=3*3; 
    for(int Height=0;Height<3;Height++) 
      for(int Width=0;Width<3;Width++) 
        Sigma=Sigma+(((float)VarianceWindow[Height][Width]­
Mean)*((float)VarianceWindow[Height][Width]­Mean)); 
  return sqrt((float)(Sigma/(TotalNoOfPix­1))); 

/* 
  Description: Calculates the variance of the whole image 
  Arguments  : Nothing  
  Returns    : Calculated variance 
*/ 

float GetVarianceOfTheWholeImage(void) 

  float Mean=GetMeanOfTheImage(),Sigma=0; 

  int TotalNoOfPix=screenWidth*screenHeight; 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
        Sigma=Sigma+(((float)rs[Height][Width]­Mean)*((float)rs[Height][Width]­Mean)); 
  return sqrt((float)(Sigma/(TotalNoOfPix­1))); 

/* 
  Description: Performs adaptive contrast enhancement 
  Arguments  : Nothing 
  Returns    : Nothing 
*/ 
void DoAdaptiveContrastEnhancement(void) 

    float MeanOftheWholeImage=(float)GetMeanOfTheImage();  
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       /* 
         leave mid pf matrix to be blank during computation 
       */ 
    VarianceWindow[0][0]=(float)rs[Height­1][Width­1];VarianceWindow[0][1]= 
   (float)rs[Height­1][Width];VarianceWindow[0][2]=(float)rs[Height­1][Width+1]; 
    VarianceWindow[1][0]=(float)rs[Height][Width­1];VarianceWindow[1][1]= 
   (float)rs[Height][Width];VarianceWindow[1][2]=(float)rs[Height][Width+1]; 
    VarianceWindow[2][0]=(float)rs[Height+1][Width­1];VarianceWindow[2][1]= 
   (float)rs[Height+1][Width];VarianceWindow[2][2]=(float)rs[Height+1][Width+1]; 
      
       
         /* 
          gs[Height][Width]=(float)((0.1*((float)
(GetMeanOfTheImage()/GetVarianceOfTheACEWindow()) 
          *((float)rs[Height][Width]­GetMeanOfTheACEWindow())))+\ 
          (0.1*GetMeanOfTheACEWindow())); 
         */ 
          if((float)GetVarianceOfTheACEWindow()==(float)0) 
          { 
           ; /*do not do anything*/  
          } 
          else 
          {   
          float temp1=(float) (MeanOftheWholeImage/GetVarianceOfTheACEWindow());     
          float temp2=(float) ((float)rs[Height][Width]­GetMeanOfTheACEWindow()); 
          temp1=(float)(0.1*temp1*temp2);   
          temp2=(float)0.8*GetMeanOfTheACEWindow(); 
          temp1=(float)(temp1+temp2); 
             
          temp1=(float)powf(temp1,float(2)); 
  gs[Height][Width]=(float)sqrt((float)temp1); 
          } 
       /* 
          bd[Height][Width]=gd[Height][Width]=rd[Height][Width];  
       */ 
    } 
    
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  

   return; 

/* 
  Description: Adds Gaussien Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 
void AddGaussienNoise(void) 

   noise_guassian(rori,rs,230,2);   
    
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  
     
 return; 

/* 
  Description: Adds Rayleignh Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void AddRayleighNoise(void) 

  noise_Rayleigh(rori,rs,230); 
      
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  

 return; 

/* 
  Description: Adds Gamma Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void AddGammNoise(void) 

 noise_Gamma(rori,rs,230,2); 
      
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  
  return; 

/* 
  Description: Adds Eponential Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 
void AddExponentialNoise(void) 

 noise_Exponential(rori,rs,230); 
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  
  return; 

/* 
  Description: Adds Uniform Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void AddUniformNoise(void) 

 noise_Uniform(rori,rs,230,2); 
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  
  return; 

/* 
  Description: Adds Impluse Noise to the image 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void AddImpluseNoise(void) 

 noise_imp(rori,rs,0.9,10,100); 
      for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
       bs[Height][Width]=gs[Height][Width]=rs[Height][Width];  
  return; 

/* 
  Description: Image slicing, 1st method 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void SliceingOne(void) 
{          
 /* 
    slicing ____| |____ 
 */ 

 for(int Height=0;Height<screenHeight;Height++) 
   for(int Width=0;Width<screenWidth;Width++) 
    { 
     if(((float)rs[Height][Width]>=180)&&(float)rs[Height][Width]<=210) 
       rs[Height][Width]=(float)210; 
     else rs[Height][Width]=(float)50; 
      
     bs[Height][Width]=gs[Height][Width]=rs[Height][Width]; 
    } 

 return; 

/* 
  Description: Image slicing, 2nd method 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void SliceingTwo(void) 

   for(int Width=0;Width<screenWidth;Width++) 
     for(int Height=0;Height<screenHeight;Height++) 
    { 
        
     if(((float)rs[Height][Width]>=180)&&(float)rs[Height][Width]<=210) 
        ;/*rs[Height][Width]=(float)210;*/ 
     else 
         rs[Height][Width]=(float)Width; 
      
     bs[Height][Width]=gs[Height][Width]=rs[Height][Width]; 
    } 
 return; 

/* 
  Description: Average filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void Average3x3Filter(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
    
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
 
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayValue+=_3x3Window[row][Col]; 

       gs[Height][Width]=(float)(TotalGrayValue/9); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 
    } 
    
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
  return;  

/* 
  Description: Average filter by window 5x5 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void Average5x5Filter(void) 

  float _5x5Window[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}; 
    
   for(int Height=2;Height<screenHeight­2;Height++) 
      for(int Width=2;Width<screenWidth­2;Width++) 
    { 
       _5x5Window[0][0]=(float)rs[Height­2][Width­2];_5x5Window[0][1]=(float)rs[Height­2]
[Width­1];  
                            _5x5Window[0][2]=(float)rs[Height­2][Width];  
       _5x5Window[0][3]=(float)rs[Height­2][Width+1];_5x5Window[0][4]=(float)rs[Height­2]
[Width+2];  
       _5x5Window[1][0]=(float)rs[Height­1][Width­2];_5x5Window[1][1]=(float)rs[Height­1]
[Width­1];  
                            _5x5Window[1][2]=(float)rs[Height­1][Width];  
       _5x5Window[1][3]=(float)rs[Height­1][Width+1];_5x5Window[1][4]=(float)rs[Height­1]
[Width+2];  
       _5x5Window[2][0]=(float)rs[Height][Width­2];_5x5Window[2][1]=(float)rs[Height][Width­
1];  
                            _5x5Window[2][2]=(float)rs[Height][Width]; 
       _5x5Window[2][3]=(float)rs[Height][Width+1];_5x5Window[2][4]=(float)rs[Height]
[Width+2];  
       _5x5Window[3][0]=(float)rs[Height+1][Width­2];_5x5Window[3][1]=(float)rs[Height+1]
[Width­1];  
                            _5x5Window[3][2]=(float)rs[Height+1][Width];  
       _5x5Window[3][3]=(float)rs[Height+1][Width+1];_5x5Window[3][4]=(float)rs[Height+1]
[Width+2];  
       _5x5Window[4][0]=(float)rs[Height+2][Width­2];_5x5Window[4][1]=(float)rs[Height+2]
[Width­1];  
                            _5x5Window[4][2]=(float)rs[Height+2][Width];  
       _5x5Window[4][3]=(float)rs[Height+2][Width+1];_5x5Window[4][4]=(float)rs[Height+2]
[Width+2];  
       

      float TotalGrayValue=0; 
       for(int row=0;row<5;row++) 
        for(int Col=0;Col<5;Col++) 
           TotalGrayValue+=_5x5Window[row][Col]; 

       gs[Height][Width]=(float)(TotalGrayValue/25); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 
    } 
     for(int Height=2;Height<screenHeight­2;Height++) 
      for(int Width=2;Width<screenWidth­2;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
    
  return;  

/* 
  Description: Average filter by window 7x7 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void Average7x7Filter(void) 
{  
   float _7x7Window[7][7]={{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},{0,0,0,0,0,0,0},
{0,0,0,0,0,0,0}, 
                         {0,0,0,0,0,0,0},{0,0,0,0,0,0,0}}; 

   for(int Height=3;Height<screenHeight­3;Height++) 
      for(int Width=3;Width<screenWidth­3;Width++) 
    { 
       _7x7Window[0][0]=(float)rs[Height­3][Width­3];_7x7Window[0][1]=(float)rs[Height­3]
[Width­2];_7x7Window[0][2]=(float)rs[Height­3][Width­1]; 
                                               _7x7Window[0][3]=(float)rs[Height­3][Width]; 
       _7x7Window[0][4]=(float)rs[Height­3][Width+1];_7x7Window[0][5]=(float)rs[Height­3]
[Width+2];_7x7Window[0][6]=(float)rs[Height­3][Width+3]; 
       _7x7Window[1][0]=(float)rs[Height­2][Width­3];_7x7Window[1][1]=(float)rs[Height­2]
[Width­2];_7x7Window[1][2]=(float)rs[Height­2][Width­1]; 
                                               _7x7Window[1][3]=(float)rs[Height­2][Width]; 
       _7x7Window[1][4]=(float)rs[Height­2][Width+1];_7x7Window[1][5]=(float)rs[Height­2]
[Width+2];_7x7Window[1][6]=(float)rs[Height­2][Width+3]; 
       _7x7Window[2][0]=(float)rs[Height­1][Width­3];_7x7Window[2][1]=(float)rs[Height­1]
[Width­2];_7x7Window[2][2]=(float)rs[Height­1][Width­1]; 
                                               _7x7Window[2][3]=(float)rs[Height­1][Width]; 
       _7x7Window[2][4]=(float)rs[Height­1][Width+1];_7x7Window[2][5]=(float)rs[Height­1]
[Width+2];_7x7Window[2][6]=(float)rs[Height­1][Width+3]; 
       _7x7Window[3][0]=(float)rs[Height][Width­3];_7x7Window[3][1]=(float)rs[Height][Width­
2];_7x7Window[3][2]=(float)rs[Height][Width­1]; 
                                               _7x7Window[3][3]=(float)rs[Height][Width]; 
       _7x7Window[3][4]=(float)rs[Height][Width+1];_7x7Window[3][5]=(float)rs[Height]
[Width+2];_7x7Window[3][6]=(float)rs[Height][Width+3]; 
       _7x7Window[4][0]=(float)rs[Height+1][Width­3];_7x7Window[4][1]=(float)rs[Height+1]
[Width­2];_7x7Window[4][2]=(float)rs[Height+1][Width­3]; 
                                               _7x7Window[4][3]=(float)rs[Height+1][Width]; 
       _7x7Window[4][4]=(float)rs[Height+1][Width+1];_7x7Window[4][5]=(float)rs[Height+1]
[Width+2];_7x7Window[4][6]=(float)rs[Height+1][Width+3]; 
       _7x7Window[5][0]=(float)rs[Height+2][Width­3];_7x7Window[5][1]=(float)rs[Height+2]
[Width­2];_7x7Window[5][2]=(float)rs[Height+2][Width­1]; 
                                               _7x7Window[5][3]=(float)rs[Height+2][Width]; 
       _7x7Window[5][4]=(float)rs[Height+2][Width+1];_7x7Window[5][5]=(float)rs[Height+2]
[Width+2];_7x7Window[5][6]=(float)rs[Height+2][Width+3]; 
       _7x7Window[6][0]=(float)rs[Height+3][Width­3];_7x7Window[6][1]=(float)rs[Height+3]
[Width­2];_7x7Window[6][2]=(float)rs[Height+3][Width­1]; 
                                               _7x7Window[6][3]=(float)rs[Height+3][Width]; 
       _7x7Window[6][4]=(float)rs[Height+3][Width+1];_7x7Window[6][5]=(float)rs[Height+3]
[Width+2];_7x7Window[6][6]=(float)rs[Height+3][Width+3]; 
      
       float TotalGrayValue=0; 
       for(int row=0;row<7;row++) 
        for(int Col=0;Col<7;Col++) 
           TotalGrayValue+=_7x7Window[row][Col]; 
       gs[Height][Width]=(float)(TotalGrayValue/49); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 
    } 

     for(int Height=3;Height<screenHeight­3;Height++) 
      for(int Width=3;Width<screenWidth­3;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
      
 return; 

/* 
  Description: Average filter by window 3x3 or arithmatic mean filter 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void ArithmeticMeanFilter(void) 

 Average3x3Filter();  
 return; 

/* 
  Description: Weighted Average filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 
void WeightedAverageFilter(void) 

   
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
    
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)(rs[Height­1][Width­1]/16);_3x3Window[0][1]=(float)
((rs[Height­1][Width]*2)/16); 
          _3x3Window[0][2]=(float)(rs[Height­1][Width+1]/16); 
       _3x3Window[1][0]=(float)((rs[Height][Width­1]*2)/16);_3x3Window[1][1]=(float)
((rs[Height][Width]*4)/16); 
          _3x3Window[1][2]=(float)((rs[Height][Width+1]*2)/16); 
       _3x3Window[2][0]=(float)(rs[Height+1][Width­1]/16);_3x3Window[2][1]=(float)
((rs[Height+1][Width]*2)/16); 
          _3x3Window[2][2]=(float)(rs[Height+1][Width+1]/16); 
 
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayValue+=_3x3Window[row][Col]; 

       gs[Height][Width]=(float)(TotalGrayValue); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 
    } 
    
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
 return; 

/* 
  Description: Geometric mean  filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 
 
void GeometricMeanFilter(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
 
       float TotalGrayValue=1; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayValue=(float)TotalGrayValue*powf((float)_3x3Window[row][Col],
(float)0.11111); 

       gs[Height][Width]=TotalGrayValue; 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 
    } 
    
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
  return;  
 return; 

/* 
  Description: Harmonic filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void HarmonicMeanFilter(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width];' 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
 
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayValue=(float)TotalGrayValue+(float)(1/_3x3Window[row][Col]); 
         
        gs[Height][Width]=(float)(9/TotalGrayValue); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Contra Harmonic filter by window 3x3  
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void ContraHarmonicFilter(float ValueForQ) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
 
       float TotalGrayValueN=0,TotalGrayValueD=0; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           { 
            TotalGrayValueN=(float)TotalGrayValueN+(float)powf((float)_3x3Window[row][Col],
(float)(ValueForQ+1.0)); 
            TotalGrayValueD=(float)TotalGrayValueD+(float)powf((float)_3x3Window[row][Col],
(float)ValueForQ); 
           } 
          
        gs[Height][Width]=(float)(TotalGrayValueN/TotalGrayValueD); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Compare the two float  
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 
int FloatCmp(const void *temp1, const void *temp2) 
 { 
     return (*(int *)temp1 ­ *(int *)temp2); 
 } 

/* 
  Description: Median filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void MedianFilter(void) 

  
  float _3x3Window[3][3]={{0,34,0},{0,21,0},{0,59,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 

     /* 
      sort the array using quick sort function  
     */  
     qsort(_3x3Window, 9, sizeof(int), FloatCmp); 
         
       gs[Height][Width]=(float)_3x3Window[1][1]; 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Max filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void MaxFilter(void) 

  
  float _3x3Window[3][3]={{0,34,0},{0,21,0},{0,59,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 

     /* sort the array using quick sort function */    
     qsort(_3x3Window, 9, sizeof(int), FloatCmp); 
         
       gs[Height][Width]=(float)_3x3Window[2][2]; 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Min filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void MinFilter(void) 

  
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 

     /*   
        sort the array using quick sort function 
     */    
     qsort(_3x3Window, 9, sizeof(int), FloatCmp); 
         
       gs[Height][Width]=(float)_3x3Window[0][0]; 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Mid point filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void MidPointFilter(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 

     /* 
       sort the array using quick sort function  
     */ 
  
     qsort(_3x3Window, 9, sizeof(int), FloatCmp); 
         
       gs[Height][Width]=(float)(((float)_3x3Window[0][0]+(float)_3x3Window[2][2])/2); 
        if((float)gs[Height][Width]>(float)255) gs[Height][Width]=(float)255;     
        if((float)gs[Height][Width]<(float)0) gs[Height][Width]=(float)gs[Height][Width]*­1; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Min mean square error filter by window 3x3 
  Arguments  : Nothing 
  Returns    : Nothing     
*/ 

void MinimumMeanSquareError(void) 
{   

  float WholeImageVariance=GetVarianceOfTheWholeImage(); 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
   
       /* 
        mean calculation 
       */   
       float TotalGrayVablue=0; 
       for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayVablue=TotalGrayVablue+(float)_3x3Window[row][Col]; 
          
        float Mean=(float)(TotalGrayVablue/9.0); 
      
       /* 
        variance calculations 
       */ 
      TotalGrayVablue=0;  
      for(int row=0;row<3;row++) 
        for(int Col=0;Col<3;Col++) 
           TotalGrayVablue=TotalGrayVablue+(((float)_3x3Window[row][Col]­
Mean)*((float)_3x3Window[row][Col]­Mean)); 
        
        float VarLocal=(float)(TotalGrayVablue/(9­1)); 

       float temp1=(float)((WholeImageVariance/(sqrt(VarLocal)))*((float)rs[Height][Width])­
Mean); 
       float TotalGrayValue=(float)rs[Height][Width]­temp1; 
      
 
       if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 

    } 
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 
 return; 

/* 
  Description: Image preprocessing 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void PreProcessforPointDetection(void) 

   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         if((float)rs[Height][Width]<=128) 
             rs[Height][Width]=(float)0; 
         else rs[Height][Width]=(float)255;    
 return; 

  
/* 
  Description: Point detection 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void PointDetection(void) 

 
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
           { 
           gs[Height][Width]=(float)rs[Height][Width+1]+(float)rs[Height][Width­1]­
2.0*(float)rs[Height][Width];      
           /* 
             cout<<endl<<(float)gs[Height][Width]; 
           */  
           } 
   for(int Width=1;Width<screenWidth­1;Width++) 
      for(int Height=1;Height<screenHeight­1;Height++) 
           { 
           bs[Height][Width]=(float)rs[Height+1][Width]+(float)rs[Height­1][Width]­
2.0*(float)rs[Height][Width];      
           /* 
             cout<<endl<<(float)gs[Height][Width]; 
           */ 
           } 
   
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         { 
          if((float)bs[Height][Width]!=0) 
          { 
           /* 
             if((((float)gs[Height][Width­1]+(float)gs[Height][Width+1])*­
1)==(float)gs[Height][Width]) 
           */  
             if((float)bs[Height][Width]>=(float)230) 
              gs[Height][Width]=0.0; 
           else gs[Height][Width]=255.0;     
           }    
         } 
     
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
    
 return; 

/* 
  Description: Horizontal line detection  
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 

void HorizontalDetection(void) 

 
   for(int Width=1;Width<screenWidth­1;Width++) 
      for(int Height=1;Height<screenHeight­1;Height++) 
           { 
           /* 
             gs[Height][Width]=(float)rs[Height+1][Width]+(float)rs[Height­1][Width]­
2.0*(float)rs[Height][Width];     
           */  
             gs[Height][Width]=(float)rs[Height+1][Width]­(float)rs[Height][Width];     
             if((float)gs[Height][Width]==(float)1) 
                gs[Height][Width]=(float)255; 
           } 
   
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         { 
          if((float)gs[Height][Width]>(float)128) 
          { 
           /* 
            cout<<endl<<(float)gs[Height][Width­1]<<" "<<(float)gs[Height][Width]<<" 
"<<(float)gs[Height][Width+1];  
            if((((float)gs[Height][Width­1]+(float)gs[Height][Width+1])*­
1)==(float)gs[Height][Width]) 
           */  
             if((float)gs[Height][Width]==(float)gs[Height][Width+1]) 
                /* 
                 ((float)gs[Height][Width]==(float)gs[Height][Width­1])) 
                */ 

              gs[Height][Width]=255.0; 
             else gs[Height][Width]=0.0;     
           }    
         } 
   
  
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
    
 return; 

/* 
  Description: Vertical line detection 
  Arguments  : Nothing 
  Returns    : Nothing       
*/ 
void VerticalDetection(void) 

 
   for(int Height=1;Height<screenHeight­1;Height++) 
     for(int Width=1;Width<screenWidth­1;Width++) 
           { 
           /* 
             gs[Height][Width]=(float)rs[Height+1][Width]+(float)rs[Height­1][Width]­
2.0*(float)rs[Height][Width]; 
           */       
             gs[Height][Width]=(float)rs[Height][Width+1]­(float)rs[Height][Width];     
             if((float)gs[Height][Width]==(float)1) 
                gs[Height][Width]=(float)255; 
           } 
   
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         { 
          if((float)gs[Height][Width]>(float)128) 
          { 
           /* 
             cout<<endl<<(float)gs[Height][Width­1]<<" "<<(float)gs[Height][Width]<<" 
"<<(float)gs[Height][Width+1]; 
             if((((float)gs[Height][Width­1]+(float)gs[Height][Width+1])*­
1)==(float)gs[Height][Width]) 
           */  
             if((float)gs[Height][Width]==(float)gs[Height+1][Width]) 
                /* ((float)gs[Height][Width]==(float)gs[Height][Width­1])) 
                */  
              gs[Height][Width]=255.0; 
             else gs[Height][Width]=0.0;     
           }    
         } 
   
  
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
    
 return; 

/* 
  Description: Two or more than two point detection 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void AnotherPointDetection(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*­1; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*8; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*­1; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: Two or more than horizontal line detection 
  Arguments  : Nothing  
  Returns    : Nothing     
*/ 

void AnotherHorizontalLineDetection(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*­1; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*2;_3x3Window[1][1]=(float)rs[Height]
[Width]*2; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*2; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*­1; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: Two or more than Vertical line detection 
  Arguments  : Nothing  
  Returns    : Nothing     
*/ 

void AnotherVerticalLineDetection(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*2; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*­1; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*2; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*2; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*­1; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: Line detection which has +45 slop 
  Arguments  : Nothing  
  Returns    : Nothing     
*/ 
void Plus45DegreesDetection(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*2; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*2; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*2;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*­1; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: Line detection which has ­45 slop 
  Arguments  : Nothing  
  Returns    : Nothing     
*/ 

void Minus45DegreesDetection(void) 

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*2;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1;  
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*­1; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*2; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*2; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: line detection using Robert operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void RobertOperatorBasedDetection(void) 
{  

  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
       
        float temp1=(float)_3x3Window[2][2]­(float)_3x3Window[1][1]; 
        temp1=(float)powf(temp1,float(2)); 
        temp1=(float)sqrt(temp1); 
        float temp2=(float)_3x3Window[2][1]­(float)_3x3Window[1][2];    
        temp2=(float)powf(temp2,float(2)); 
temp2=(float)sqrt(temp2); 
         
        float TotalGrayValue=(float)(temp1+temp2);  
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
        /* 
          gs[Height][Width]=(float)(powf((_3x3Window[2][2]­_3x3Window[1][1]),(float)2)+\ 
                                 (powf((_3x3Window[2][1]­_3x3Window[1][2]),(float)2))); 
          gs[Height][Width]=(float)sqrt((float)gs[Height][Width]); 
          if((float)gs[Height][Width]==(float)1) gs[Height][Width]=(float)255;  
          cout<<endl<<(float)gs[Height][Width]; 
        */ 
    } 
    
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: Two or more lines detection using Robert operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void AnotherRobertOperatorBasedDetection(void) 
{  
 float _2x2windowX[2][2]={{0,0},{0,0}}; 
 float _2x2windowY[2][2]={{0,0},{0,0}}; 

   for(int Height=0;Height<screenHeight­1;Height++) 
      for(int Width=0;Width<screenWidth­1;Width++) 
     { 
       _2x2windowX[0][0]=(float)rs[Height][Width]*­1;_2x2windowX[0][1]=(float)rs[Height]
[Width+1]*0;     
       _2x2windowX[1][0]=(float)rs[Height+1][Width]*0;_2x2windowX[1][1]=(float)rs[Height+1]
[Width+1]*1;     
       
       _2x2windowY[0][0]=(float)rs[Height][Width]*0;_2x2windowY[0][1]=(float)rs[Height]
[Width+1]*­1;     
       _2x2windowY[1][0]=(float)rs[Height+1][Width]*1;_2x2windowY[1][1]=(float)rs[Height+1]
[Width+1]*0;     

       float TotalXGray=0,TotalYGray=0; 
       for(int row=0;row<2;row++) 
         for(int col=0;col<2;col++) 
       { 
          TotalXGray+=_2x2windowX[row][col]; 
          TotalYGray+=_2x2windowY[row][col]; 
       } 

        TotalXGray=float(powf(TotalXGray,(float)2)); 
        TotalXGray=(float)sqrt(TotalXGray); 
        
        TotalYGray=float(powf(TotalYGray,(float)2)); 
        TotalYGray=(float)sqrt(TotalYGray); 
       
        float TotalGrayValue=float(TotalXGray+TotalYGray); 
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
     } 
    
     for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: line detection using Prewitt operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 
void PrewittOperatorBasedDetection(void) 
{  
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
       
        float temp1=(float)(_3x3Window[2][0]+_3x3Window[2][1]+_3x3Window[2][2]); 
        float temp2=(float)(_3x3Window[0][0]+_3x3Window[0][1]+_3x3Window[0][2]); 
        float temp3=(float)(_3x3Window[0][2]+_3x3Window[1][2]+_3x3Window[2][2]); 
        float temp4=(float)(_3x3Window[0][0]+_3x3Window[1][0]+_3x3Window[2][0]); 
             
        temp1=float(powf((temp1­temp2),(float)2)); 
        temp1=(float)sqrt(temp1); 
        temp3=float(powf((temp3­temp4),(float)2)); 
        temp3=(float)sqrt(temp3); 
 
        float TotalGrayValue=(float)(temp1+temp3);  
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
        /* 
          cout<<endl<<(float)gs[Height][Width]; 
          gs[Height][Width]=(float)sqrt((float)gs[Height][Width]); 
          if((float)gs[Height][Width]==(float)i1) gs[Height][Width]=(float)255; 
        */   
    } 
    
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: Two or more lines detection using Prewitt operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void AnotherPrewittOperatorBasedDetection(void) 

  float _3x3WindowX[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
  float _3x3WindowY[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 

   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
      _3x3WindowX[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3WindowX[0][1]=(float)rs[Height­
1][Width]*0; 
      _3x3WindowX[0][2]=(float)rs[Height­1][Width+1]*1; 
      _3x3WindowX[1][0]=(float)rs[Height][Width­1]*­1;_3x3WindowX[1][1]=(float)rs[Height]
[Width]*0; 
      _3x3WindowX[1][2]=(float)rs[Height][Width+1]*1; 
      _3x3WindowX[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3WindowX[2]
[1]=(float)rs[Height+1][Width]*0; 
      _3x3WindowX[2][2]=(float)rs[Height+1][Width+1]*1; 
      
      _3x3WindowY[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3WindowY[0][1]=(float)rs[Height­
1][Width]*­1; 
      _3x3WindowY[0][2]=(float)rs[Height­1][Width+1]*­1; 
      _3x3WindowY[1][0]=(float)rs[Height][Width­1]*0;_3x3WindowY[1][1]=(float)rs[Height]
[Width]*0; 
      _3x3WindowY[1][2]=(float)rs[Height][Width+1]*0; 
      _3x3WindowY[2][0]=(float)rs[Height+1][Width­1]*1;_3x3WindowY[2][1]=(float)rs[Height+1]
[Width]*1; 
      _3x3WindowY[2][2]=(float)rs[Height+1][Width+1]*1; 

       float TotalXGray=0,TotalYGray=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
       { 
          TotalXGray+=_3x3WindowX[row][col]; 
          TotalYGray+=_3x3WindowY[row][col]; 
       } 

        TotalXGray=float(powf(TotalXGray,(float)2)); 
        TotalXGray=(float)sqrt(TotalXGray); 
        
        TotalYGray=float(powf(TotalYGray,(float)2)); 
        TotalYGray=(float)sqrt(TotalYGray); 
       
        float TotalGrayValue=float(TotalXGray+TotalYGray); 
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
  
 return; 

/* 
  Description: line detection using Sobel Operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 
void SobelOperatorBaseDetection(void) 

 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1];_3x3Window[0][1]=(float)rs[Height­1]
[Width]; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]; 
       _3x3Window[1][0]=(float)rs[Height][Width­1];_3x3Window[1][1]=(float)rs[Height]
[Width]; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1];_3x3Window[2][1]=(float)rs[Height+1]
[Width]; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]; 
       
        float temp1=(float)(_3x3Window[0][2]+(_3x3Window[1][2]*2)+_3x3Window[2][2]); 
        float temp2=(float)(_3x3Window[0][0]+(_3x3Window[1][0]*2)+_3x3Window[2][0]); 
        float temp3=(float)(_3x3Window[2][0]+(_3x3Window[2][1]*2)+_3x3Window[2][2]); 
        float temp4=(float)(_3x3Window[0][0]+(_3x3Window[0][1]*2)+_3x3Window[0][2]); 
             
        temp1=float(powf((temp1­temp2),(float)2)); 
        temp1=(float)sqrt(temp1); 
        temp3=float(powf((temp3­temp4),(float)2)); 
        temp3=(float)sqrt(temp3); 

        float TotalGrayValue=(float)(temp1+temp3);  
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
        /* 
          cout<<endl<<(float)gs[Height][Width]; 
          gs[Height][Width]=(float)sqrt((float)gs[Height][Width]); 
          if((float)gs[Height][Width]==(float)i1) gs[Height][Width]=(float)255; 
        */  
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: Image preprocessing 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void ImagePreprocess(void) 

   PreProcessforPointDetection();    
 return; 

/* 
  Description: Two or more lines detection using Sobel Operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void AnotherSobelOperatorDetection(void) 
{  
 
  float _3x3WindowX[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
  float _3x3WindowY[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 

   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
      _3x3WindowX[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3WindowX[0][1]=(float)rs[Height­
1][Width]*0; 
      _3x3WindowX[0][2]=(float)rs[Height­1][Width+1]*1; 
      _3x3WindowX[1][0]=(float)rs[Height][Width­1]*­2;_3x3WindowX[1][1]=(float)rs[Height]
[Width]*0; 
      _3x3WindowX[1][2]=(float)rs[Height][Width+1]*2; 
      _3x3WindowX[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3WindowX[2]
[1]=(float)rs[Height+1][Width]*0; 
      _3x3WindowX[2][2]=(float)rs[Height+1][Width+1]*1; 
      
      _3x3WindowY[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3WindowY[0][1]=(float)rs[Height­
1][Width]*­2; 
      _3x3WindowY[0][2]=(float)rs[Height­1][Width+1]*­1; 
      _3x3WindowY[1][0]=(float)rs[Height][Width­1]*0;_3x3WindowY[1][1]=(float)rs[Height]
[Width]*0; 
      _3x3WindowY[1][2]=(float)rs[Height][Width+1]*0; 
      _3x3WindowY[2][0]=(float)rs[Height+1][Width­1]*1;_3x3WindowY[2][1]=(float)rs[Height+1]
[Width]*2; 
      _3x3WindowY[2][2]=(float)rs[Height+1][Width+1]*1; 

       float TotalXGray=0,TotalYGray=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
       { 
          TotalXGray+=_3x3WindowX[row][col]; 
          TotalYGray+=_3x3WindowY[row][col]; 
       } 

        TotalXGray=float(powf(TotalXGray,(float)2)); 
        TotalXGray=(float)sqrt(TotalXGray); 
        
        TotalYGray=float(powf(TotalYGray,(float)2)); 
        TotalYGray=(float)sqrt(TotalYGray); 
       
        float TotalGrayValue=float(TotalXGray+TotalYGray); 
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
  
 return; 

   
/* 
  Description: Line detection using log operator 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 
void LogOperatorBasedDetection(void) 
{  
  float _5x5Window[5][5]={{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}; 
    
   for(int Height=2;Height<screenHeight­2;Height++) 
      for(int Width=2;Width<screenWidth­2;Width++) 
    { 
       _5x5Window[0][0]=(float)rs[Height­2][Width­2]*0;_5x5Window[0][1]=(float)rs[Height­2]
[Width­1]*0;  
                            _5x5Window[0][2]=(float)rs[Height­2][Width]*­1;  
       _5x5Window[0][3]=(float)rs[Height­2][Width+1]*0;_5x5Window[0][4]=(float)rs[Height­2]
[Width+2]*0;  
       _5x5Window[1][0]=(float)rs[Height­1][Width­2]*0;_5x5Window[1][1]=(float)rs[Height­1]
[Width­1]*­1;  
                            _5x5Window[1][2]=(float)rs[Height­1][Width]*­2;  
       _5x5Window[1][3]=(float)rs[Height­1][Width+1]*­1;_5x5Window[1][4]=(float)rs[Height­1]
[Width+2]*0;  
       _5x5Window[2][0]=(float)rs[Height][Width­2]*­1;_5x5Window[2][1]=(float)rs[Height]
[Width­1]*­2;  
                            _5x5Window[2][2]=(float)rs[Height][Width]*16; 
       _5x5Window[2][3]=(float)rs[Height][Width+1]*­2;_5x5Window[2][4]=(float)rs[Height]
[Width+2]*­1;  
       _5x5Window[3][0]=(float)rs[Height+1][Width­2]*0;_5x5Window[3][1]=(float)rs[Height+1]
[Width­1]*­1;  
                            _5x5Window[3][2]=(float)rs[Height+1][Width]*­2;  
       _5x5Window[3][3]=(float)rs[Height+1][Width+1]*­1;_5x5Window[3][4]=(float)rs[Height+1]
[Width+2]*0;  
       _5x5Window[4][0]=(float)rs[Height+2][Width­2]*0;_5x5Window[4][1]=(float)rs[Height+2]
[Width­1]*0;  
                            _5x5Window[4][2]=(float)rs[Height+2][Width]*­1;  
       _5x5Window[4][3]=(float)rs[Height+2][Width+1]*0;_5x5Window[4][4]=(float)rs[Height+2]
[Width+2]*0;  
       

      float TotalGrayValue=0; 
       for(int row=0;row<5;row++) 
        for(int Col=0;Col<5;Col++) 
           TotalGrayValue+=_5x5Window[row][Col]; 

        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
     for(int Height=2;Height<screenHeight­2;Height++) 
      for(int Width=2;Width<screenWidth­2;Width++) 
       rs[Height][Width]=bs[Height][Width]=gs[Height][Width];  
    
 return; 

 
/* 
  Description: To sharp image using 3x3 mask, one method   
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 
void ImageSharpeningMaskOne(void) 
{  
 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*0;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*0; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*5; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*0;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*0; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 

    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 

 return; 

/* 
  Description: To sharp image using 3x3 mask, second method 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void ImageSharpeningMaskTwo(void) 

 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*­1;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*­1; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*9; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*­1;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*­1; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
        
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: To sharp image using 3x3 mask, positive laplacian 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void PositiveLaplacian(void) 

 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*0;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*­1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*0; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*­1;_3x3Window[1][1]=(float)rs[Height]
[Width]*4; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*­1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*0;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*­1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*0; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: To sharp image using 3x3 mask, negative laplacian 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void NegativeLaplacian(void) 

 
  float _3x3Window[3][3]={{0,0,0},{0,0,0},{0,0,0}}; 
     
   for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
    { 
       _3x3Window[0][0]=(float)rs[Height­1][Width­1]*0;_3x3Window[0][1]=(float)rs[Height­1]
[Width]*1; 
       _3x3Window[0][2]=(float)rs[Height­1][Width+1]*0; 
       _3x3Window[1][0]=(float)rs[Height][Width­1]*1;_3x3Window[1][1]=(float)rs[Height]
[Width]*­4; 
       _3x3Window[1][2]=(float)rs[Height][Width+1]*1; 
       _3x3Window[2][0]=(float)rs[Height+1][Width­1]*0;_3x3Window[2][1]=(float)rs[Height+1]
[Width]*1; 
       _3x3Window[2][2]=(float)rs[Height+1][Width+1]*0; 
       
       float TotalGrayValue=0; 
       for(int row=0;row<3;row++) 
         for(int col=0;col<3;col++) 
        TotalGrayValue+=(float)_3x3Window[row][col]; 
        
        if(TotalGrayValue<0) 
           gs[Height][Width]=(float)0; 
        else if(TotalGrayValue>255) 
           gs[Height][Width]=(float)255; 
           else gs[Height][Width]=TotalGrayValue; 
    } 
    for(int Height=1;Height<screenHeight­1;Height++) 
      for(int Width=1;Width<screenWidth­1;Width++) 
         rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
 return; 

/* 
  Description: To draw flat histogram of an image 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void DrawFlatHistogram(void) 
{  
  
 for(int Width=0;Width<screenWidth­1;Width++) 
   for(int Height=0;Height<screenHeight­1;Height++) 
       { 
          gs[Height][Width]=(float)Width; 
          rs[Height][Width]=bs[Height][Width]=gs[Height][Width]; 
        } 
 return; 

/* 
  Description: Preforms customized thresholding 
  Arguments  : Threshold value, default = 128.0 
  Returns    : Nothing      
*/ 

void CustomThersholding(float ThresholdValue=128.0) 

  
   for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         if((float)rs[Height][Width]<=(float)ThresholdValue) 
             rs[Height][Width]=(float)0; 
         else rs[Height][Width]=(float)255;   
 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 
 return; 

 
/* 
  Description: Preforms globle thresholding 
  Arguments  : Threshold value, default = 160.0  
  Returns    : Nothing      
*/ 

void GlobleThresholding(float InitialTValue=160.0) 
{  

 while(true) 
 { 
 int RegionOne=0,PixelsFromOne=0,RegionTwo=0,PixelsFromTwo=0; 
   for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
      { 
         if((int)rs[Height][Width]>=(int)InitialTValue) 
           { 
           RegionOne+=(int)rs[Height][Width]; 
           PixelsFromOne++; 
           } 
         else 
           {  
           RegionTwo+=(int)rs[Height][Width]; 
           PixelsFromTwo++; 
           } 

      } 
  int AvgGray=((RegionOne/PixelsFromOne)+(RegionTwo/PixelsFromTwo))/2; 
 
  if(InitialTValue>AvgGray) InitialTValue­=3;   
  else InitialTValue+=3; 
 
  int terminate=InitialTValue­AvgGray; 
  terminate=pow(terminate,2); 
  terminate=sqrt(terminate); 
 
  cout<<endl<<"Initial T :"<<InitialTValue<<" Average T:"<<AvgGray<<endl; 
  if(terminate<4) break; 
 } 
   
   for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         if((float)rs[Height][Width]<=(float)InitialTValue) 
             rs[Height][Width]=(float)0; 
         else rs[Height][Width]=(float)255;   
 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 
 return; 
}  

/* 
  Description: Preforms threshold using Otsu method 
  Arguments  : Threshold value, default = 200 
  Returns    : Nothing      
*/ 

void OtsuThreshold(float InitialThresholdValue=200.0) 
{  
 
 float PiArray[256],Variance=0.0; 
 int TotoalNoOfPixel=screenWidth*screenHeight; 
  
 for(int CleanCounter=0;CleanCounter<256;CleanCounter++) 
     PiArray[CleanCounter]=(float)0.0; 

  float TotalPk=0; 
   for(int CurrentGrayLevel=0;CurrentGrayLevel<256;CurrentGrayLevel++) 
   { 
     float TotalNoOfCurrentPixel=0; 
   for(int Height=0;Height<screenHeight;Height++) 
     for(int Width=0;Width<screenWidth;Width++) 
     { 
         if((float)CurrentGrayLevel==(float)rs[Height][Width]) 
            TotalNoOfCurrentPixel++; 
     } 
    PiArray[CurrentGrayLevel]=(float)(TotalNoOfCurrentPixel/TotoalNoOfPixel); 
    TotalPk+=PiArray[CurrentGrayLevel]; 
   } 

  while(true) 
 { 
 float Pk=0.0; 
 for(int PkCounter=0;PkCounter<=InitialThresholdValue;PkCounter++) 
    Pk+=PiArray[PkCounter]; 

 float Mk=0.0; 
 for(int MkCounter=0;MkCounter<=InitialThresholdValue;MkCounter++) 
    Mk+=(float)((float)MkCounter*PiArray[MkCounter]);  
 /* 
    Mk=(float)Mk/Pk; 
 */ 

 float Mg=0.0; 
 for(int MgCounter=0;MgCounter<256;MgCounter++) 
    Mg+=(float)((float)MgCounter*PiArray[MgCounter]); 
  /* 
    Mg=(float)Mg/TotalPk; 
  */ 
 
 float temp1=float((Mg*Pk)­Mk); 
 temp1=(float)powf(temp1,2.0); 

 
 temp1=float(temp1/((Pk)*(1­Pk))); 
 if(temp1<Variance) 
   break; 
 else 
   InitialThresholdValue­=3; 
 
 Variance=temp1; 

 cout<<endl<<"Variance: "<<Variance<<"   Threshold value: "<<InitialThresholdValue<<endl; 
 
 if(InitialThresholdValue<=(float)0) break; 

 
 
   for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         if((float)rs[Height][Width]<=(float)InitialThresholdValue) 
             rs[Height][Width]=(float)0; 
         else rs[Height][Width]=(float)255;   
 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 

 return; 

/* 
  Description: Preforms segmentation using connected component of binary image 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 
void ConnectedComponentBinaryImage(void) 
{  
  float _5x1Window[4]={0,0,0,0};  
  int TagValue=0,ChangeHight=0; 
   
    GlobleThresholding(); 
    cout<<endl<<endl; 
 
    for(int Height=1;Height<screenHeight­1;Height++) 
      { 
      for(int Width=1;Width<screenWidth­1;Width++) 
        { 
        if((float)rs[Height][Width]>(float)128) 
           { 
              rs[Height][Width]=(float)1; 
              gs[Height][Width]=(float)1; 
           } 
        else 
             gs[Height][Width]=(float)0; 
        } 
       } 

    /* forward pass */    
    int StartHeight=0,StartWidth=0,HLimit=0,WLimit=0; 
    if(ChangeHight) { 
        StartHeight=2;StartWidth=2;HLimit=screenHeight­2;WLimit=screenWidth­2; 
    } 
    else 
      { 
        StartHeight=1;StartWidth=1;HLimit=screenHeight­1;WLimit=screenWidth­1; 
    }  
        
    for(int Height=StartHeight;Height<HLimit;Height++) 
      { 
      for(int Width=StartWidth;Width<WLimit;Width++) 
    { 
       if((float)rs[Height][Width]==(float)0) 
          ; /*nothing to do for the time being*/ 
       else 
       { 
      
      _5x1Window[0]=(float)gs[Height­1][Width­1];_5x1Window[1]=(float)gs[Height­1][Width]; 
      _5x1Window[2]=(float)gs[Height­1][Width+1]; 
      _5x1Window[3]=(float)gs[Height][Width­1];/*_5x1Window[4]=(float)rs[Height][Width];*/ 

       qsort(_5x1Window, 4, sizeof(int), FloatCmp); 
       
       int ptr=0; 
       float CurrentTagValue=0; 
       while(ptr<4) 
       { 
        if((float)_5x1Window[ptr]==(float)0) 
          ptr++; 
        else 
          { 
            CurrentTagValue=(float)_5x1Window[ptr]; 
            break; 
          } 
       } 
       
       
       if(_5x1Window[3]==(float)0) 
       { 
         TagValue++; 
         gs[Height][Width]=(float)TagValue; 
       } 
       else gs[Height][Width]=(float)CurrentTagValue; 
        } 
        cout<<(float)gs[Height][Width];  
    } 
        cout<<endl; 
      }     
   
     cout<<endl<<endl; 
 
     /*backword pass*/  
    for(int Height=StartHeight;Height<HLimit;Height++) 
      { 
      for(int Width=StartWidth;Width<WLimit;Width++) 
    { 
       if((float)rs[Height][Width]==(float)0) 
          ; /*nothing to do for the time being*/ 
       else 
       { 
      
      _5x1Window[0]=(float)gs[Height+1][Width+1];_5x1Window[1]=(float)gs[Height+1][Width]; 
      _5x1Window[2]=(float)gs[Height+1][Width­1]; 
      _5x1Window[3]=(float)gs[Height][Width+1];/*_5x1Window[4]=(float)gs[Height][Width];*/ 

       qsort(_5x1Window, 4, sizeof(int), FloatCmp); 
 
       /*for(int i=0;i<5;i++) 
          cout<<(float)_5x1Window[i];    
          cout<<endl; 
       */     

       int ptr=0; 
       float CurrentTagValue=0; 
       while(ptr<5) 
       { 
        if((float)_5x1Window[ptr]==(float)0) 
          ptr++; 
        else 
          { 
            CurrentTagValue=(float)_5x1Window[ptr]; 
            /*cout<<"\t­­­­­­­­"<<CurrentTagValue<<"­­­­­­­";*/ 
            break; 
          } 
       } 
       
       
       if(_5x1Window[4]==(float)0) 
       { 
         /*TagValue++; 
           gs[Height][Width]=(float)TagValue; 
         */ 
       } 
       else 
            { 
              gs[Height][Width]=(float)CurrentTagValue; 
            } 
        } 
      cout<<(float)gs[Height][Width];  
    } 
      cout<<endl; 
      }   
     
     TagValue=0; 
     for(int TagNo=1;TagNo<1000;TagNo++) 
       { 
          int flag=0; 
       for(int Height=1;Height<screenHeight­1;Height++) 
         for(int Width=1;Width<screenWidth­1;Width++) 
          { 
          if(TagNo==(int)gs[Height][Width]) 
            flag=1; 
          } 

          if(flag==1)      
            TagValue++; 
       }       
    
  
     cout<<endl<<"Total number of Images: "<<TagValue­2<<endl; 
   
return; 

/* 
  Description: Allocates the momory to hold the image in buffer 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void AllocateHSVMemory(void) 

     if(hs==NULL) 
     {   
     hs = matuc(bh.rows,bh.cols);   
     ss = matuc(bh.rows,bh.cols); 
     vs = matuc(bh.rows,bh.cols); 
     } 
 return; 

/* 
  Description: RGB to HSV conversion 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void RGBToHSV(void) 

 AllocateHSVMemory(); 

 float _3x1Buffer[3]={0,0,0};  
 
 for(int Height=0;Height<screenHeight;Height++) 
    for(int Width=0;Width<screenWidth;Width++) 
   { 
       _3x1Buffer[0]=(float)rs[Height][Width]; 
       _3x1Buffer[1]=(float)gs[Height][Width]; 
       _3x1Buffer[2]=(float)bs[Height][Width]; 
       qsort(_3x1Buffer, 3, sizeof(int), FloatCmp); 
        
        //v component  
        vs[Height][Width]=(float)_3x1Buffer[2]; 
       
        //s component 
        if(_3x1Buffer[2]>(float)0) 
        { 
        float tempBuffer=_3x1Buffer[2]­_3x1Buffer[0]; 
        tempBuffer=(tempBuffer/_3x1Buffer[2]); 

        ss[Height][Width]=(float)tempBuffer*255; 
        } 
        else ss[Height][Width]=(float)0; 
      
       //h component 
       if((float)ss[Height][Width]==(float)0) 
        { 
          hs[Height][Width]=(float)0;    
        } 
       else 
        { 
           float tempBuffer=0; 
           float MaxMin=_3x1Buffer[2]­_3x1Buffer[0]; 
           if(MaxMin!=0) 
           { 
           if((float)rs[Height][Width]==(float)_3x1Buffer[2]) 
             { 
               tempBuffer=(float)gs[Height][Width]­(float)bs[Height][Width]; 
               tempBuffer=tempBuffer/MaxMin; 
                
             } 
           else if((float)gs[Height][Width]==(float)_3x1Buffer[2]) 
             { 
               tempBuffer=(float)bs[Height][Width]­(float)rs[Height][Width]; 
               tempBuffer=tempBuffer/MaxMin; 
               tempBuffer+=2.0; 
             } 
           else if((float)bs[Height][Width]==(float)_3x1Buffer[2]) 
             { 
               tempBuffer=(float)rs[Height][Width]­(float)gs[Height][Width]; 
               tempBuffer=tempBuffer/MaxMin; 
               tempBuffer+=4.0; 
             } 
           } 
          tempBuffer=tempBuffer*60; 
          if(tempBuffer<0) 
             tempBuffer+=360; 
              
         hs[Height][Width]=(float)tempBuffer;    
        }  
   } 
  
 cout<<endl<<endl<<"Hue is multiplied by 255 to represent the pixel in H­Window."<<endl; 
  
 return; 

/* 
  Description: RGB to YIQ conversion 
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void RGBtoYIQ(void) 

 AllocateHSVMemory(); 
 for(int Height=0;Height<screenHeight;Height++) 
    for(int Width=0;Width<screenWidth;Width++) 
   { 
     hs[Height][Width]=(0.299*(float)rs[Height][Width])+(0.587*gs[Height][Width])
+(0.114*bs[Height][Width]);   
     ss[Height][Width]=(0.596*(float)rs[Height][Width])­(0.275*gs[Height][Width])­
(0.321*bs[Height][Width]);   
     vs[Height][Width]=(0.212*(float)rs[Height][Width])­(0.523*gs[Height][Width])­
(0.311*bs[Height][Width]);   
   } 
   
 return; 

/* 
  Description: To do color segmentation 
  Arguments  : Segmentation arguments 
  Returns    : Nothing      
*/ 

void DoColorSegmentation(float Smin=50.0,float Smax=200.0,float Hmin=50.0,float Hmax=200.0) 

 if(cs==NULL) 
     cs = matuc(bh.rows,bh.cols); 
   
     RGBToHSV();  
 
  for(int Height=0;Height<screenHeight;Height++) 
    for(int Width=0;Width<screenWidth;Width++) 
    { 
        if(((float)ss[Height][Width]>=(float)0)&&((float)ss[Height][Width])<(float)Smin) 
            ss[Height][Width]=(float)0; 
        if(((float)ss[Height][Width]>=(float)Smax)&&((float)ss[Height][Width])<(float)256) 
            ss[Height][Width]=(float)255; 
        
         if(((float)hs[Height][Width]>=(float)Hmin)&&((float)hs[Height]
[Width])<=(float)Hmax) 
            hs[Height][Width]=(float)255; 
         else hs[Height][Width]=(float)0; 
    } 
  
  for(int Height=0;Height<screenHeight;Height++) 
    for(int Width=0;Width<screenWidth;Width++) 
     {     
       /* 
         cout<<endl<<(float)ss[Height][Width]<<":"<<(float)hs[Height][Width]<<":" 
             <<(ss[Height][Width]&hs[Height][Width]); 
        */     
       cs[Height][Width]=ss[Height][Width]&hs[Height][Width];   
     } 
   
 return; 

/* 
  Description: Performs region Growing according to image  
  Arguments  : Nothing 
  Returns    : Nothing      
*/ 

void RegoinGrowing(void) 
{  

 for(int Height=0;Height<screenHeight;Height++) 
    for(int Width=0;Width<screenWidth;Width++) 
 { 
    float CurrentGrayLevel=(float)rs[Height][Width]; 
    float WhatToSet=0; 

    if((float)CurrentGrayLevel>=(float)0&&(float)CurrentGrayLevel<(float)25) 
        WhatToSet=0;            /*acts as a seed no 1 */ 
    else if((float)CurrentGrayLevel>=(float)25&&(float)CurrentGrayLevel<(float)50) 
        WhatToSet=25;           /*acts as a seed no 2 */ 
    else if((float)CurrentGrayLevel>=(float)50&&(float)CurrentGrayLevel<(float)75) 
        WhatToSet=50;          /*acts as a seed no 3 */ 
    else if((float)CurrentGrayLevel>=(float)75&&(float)CurrentGrayLevel<(float)100) 
        WhatToSet=75;          /*acts as a seed no 4 */ 
    else if((float)CurrentGrayLevel>=(float)100&&(float)CurrentGrayLevel<(float)125) 
        WhatToSet=100;          /*acts as a seed no 5 */ 
    else if((float)CurrentGrayLevel>=(float)125&&(float)CurrentGrayLevel<(float)150) 
        WhatToSet=125;           /*acts as a seed no 6*/ 
    else if((float)CurrentGrayLevel>=(float)150&&(float)CurrentGrayLevel<(float)175) 
        WhatToSet=150;           /*acts as a seed no 7*/ 
    else if((float)CurrentGrayLevel>=(float)175&&(float)CurrentGrayLevel<(float)200) 
        WhatToSet=175;           /*acts as a seed no 8*/ 
    else if((float)CurrentGrayLevel>=(float)200&&(float)CurrentGrayLevel<(float)225) 
        WhatToSet=200;           /*acts as a seed no 9*/ 
    else if((float)CurrentGrayLevel>=(float)225&&(float)CurrentGrayLevel<(float)256) 
        WhatToSet=225;           /*acts as a seed no 10*/ 
    
   
  /*  if((float)CurrentGrayLevel>=(float)0&&(float)CurrentGrayLevel<(float)50) 
        WhatToSet=0;            //acts as a seed no 1 
    else if((float)CurrentGrayLevel>=(float)50&&(float)CurrentGrayLevel<(float)100) 
        WhatToSet=50;           //acts as a seed no 2 
    else if((float)CurrentGrayLevel>=(float)100&&(float)CurrentGrayLevel<(float)150) 
        WhatToSet=100;          //acts as a seed no 3 
    else if((float)CurrentGrayLevel>=(float)150&&(float)CurrentGrayLevel<(float)200) 
        WhatToSet=150;          //acts as a seed no 4 
    else if((float)CurrentGrayLevel>=(float)200&&(float)CurrentGrayLevel<(float)250) 
        WhatToSet=200;          //acts as a seed no 5 
*/ 
    rs[Height][Width]=(float)WhatToSet;     
 } 
    for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         gs[Height][Width]=bs[Height][Width]=rs[Height][Width]; 
    
      
 return; 

/* 
  Description: Splitting of an image using split hight, width etc 
  Arguments  : Start and stop point for width and hight 
  Returns    : Nothing      
*/ 

int globleWe=screenWidth­1,globleHe=screenHeight­1; 
void Split(int Wstart, int Wend, int Hstart, int Hend) 

   if(Wstart==0||Wend==0||Hstart==0||Hend==0) return; 
  int HoriPix=Wend­Wstart; 
  int VerPix=Hend­Hstart; 
  int TotalNoOfPix=HoriPix*VerPix; 
  float Mean=0; 
    for(int Height=Hstart;Height<=Hend;Height++) 
      for(int Width=Wstart;Width<=Wend;Width++) 
         Mean+=(float)rs[Height][Width];    
    if(TotalNoOfPix<=0) return; 
    else Mean=(float)(Mean/(float)TotalNoOfPix);   
   cout<< endl<< Mean; 
  
   if(Mean>=(float)240&&Mean<=(float)250) 
     { 
       cout<<endl<<"return"; 
       return; 
      } 
   else 
   { 
      cout<<endl<<Wstart<<":"<<Wend<<":"<<Hstart<<":"<<Hend; 
      Split(Wstart,Wend/2,Hstart,Hend/2); 
      Split(Wend/2,Wend,Hstart,Hend/2); 
      Split(Wstart,Wend/2,Hend/2,Hend); 
      if(Wend==(screenWidth­1)&&Hend==(screenHeight­1)) 
        { 
         globleWe=globleWe/2; 
         globleHe=globleHe/2; 
         if(globleHe<=0||globleWe<=0) return; 
         Split(globleWe,Wend,globleHe,Hend); 
         } 
      else Split(Wend/2,Wend,Hend/2,Hend); 
   } 
 return; 

/* 
  Description: Splitting of the image using given size of targeted splitted section 
  Arguments  : Targeted spliting section size 
  Returns    : Nothing      
*/ 

void Split(int Size=2) 

 
  for(int Height=Size;Height<screenHeight­Size;Height+=Size*2) 
     for(int Width=Size;Width<screenWidth­Size;Width+=Size*2) 
    { 
        float temp=0; 
        
      for(int col=­Size;col<Size+1;col++) 
       for(int row=­Size;row<Size+1;row++) 
          temp+=(float)rs[Height+col][Width+row]; 
  
       temp=temp/(((Size*2)+1)*((Size*2)+1));  

      for(int col=­Size;col<Size+1;col++) 
       for(int row=­Size;row<Size+1;row++) 
          rs[Height+col][Width+row]=temp; 
    } 
    
  float temp=0; 
  for(int Height=Size;Height<screenHeight­Size;Height+=Size*2) 
     for(int Width=Size;Width<screenWidth­Size;Width+=Size*2) 
     { 
        float Diff=(float)rs[Height][Width]­temp; 
        Diff=Diff*Diff; 
        Diff=sqrt(Diff); 
        temp=(float)rs[Height][Width]; 
    
    float WhatToSet=0,CurrentGrayLevel=temp; 
                      
    if((float)CurrentGrayLevel>=(float)0&&(float)CurrentGrayLevel<(float)25) 
        WhatToSet=0;            /*acts as a seed no 1*/ 
    else if((float)CurrentGrayLevel>=(float)25&&(float)CurrentGrayLevel<(float)50) 
        WhatToSet=25;           /*acts as a seed no 2*/ 
    else if((float)CurrentGrayLevel>=(float)50&&(float)CurrentGrayLevel<(float)75) 
        WhatToSet=50;          /*acts as a seed no 3 */ 
    else if((float)CurrentGrayLevel>=(float)75&&(float)CurrentGrayLevel<(float)100) 
        WhatToSet=75;          /*acts as a seed no 4 */ 
    else if((float)CurrentGrayLevel>=(float)100&&(float)CurrentGrayLevel<(float)125) 
        WhatToSet=100;          /*acts as a seed no 5*/ 
    else if((float)CurrentGrayLevel>=(float)125&&(float)CurrentGrayLevel<(float)150) 
        WhatToSet=125;           /*acts as a seed no 6*/ 
    else if((float)CurrentGrayLevel>=(float)150&&(float)CurrentGrayLevel<(float)175) 
        WhatToSet=150;           /*acts as a seed no 7 */ 
    else if((float)CurrentGrayLevel>=(float)175&&(float)CurrentGrayLevel<(float)200) 
        WhatToSet=175;           /*acts as a seed no 8 */ 
    else if((float)CurrentGrayLevel>=(float)200&&(float)CurrentGrayLevel<(float)225) 
        WhatToSet=200;           /*acts as a seed no 9 */ 
    else if((float)CurrentGrayLevel>=(float)225&&(float)CurrentGrayLevel<(float)256) 
        WhatToSet=225;           /*acts as a seed no 10 */ 
   
       if(Diff<10)  
      for(int col=­Size;col<Size+1;col++) 
       for(int row=­Size;row<Size+1;row++) 
          rs[Height+col][Width+row]=WhatToSet; 
     } 
    
  for(int Height=0;Height<screenHeight;Height++) 
      for(int Width=0;Width<screenWidth;Width++) 
         bs[Height][Width]=gs[Height][Width]=rs[Height][Width]; 
 return; 

/* 
  Description: Demo for split and merge of giving image 
  Arguments  : Split and merge size 
  Returns    : Nothing      
*/ 

void SplitAndMerge(int Size=3) 

 /*Split(1,(int)screenWidth­1,1,(int)screenHeight­1); */   
 Split(Size);  
 return; 

#endif

Anda mungkin juga menyukai