Tuesday, 22 May 2012

Histogram Equalization

//______________________________________________________________________________________
//
//OpenCV Histograms of Images
//
//______________________________________________________________________________________
//______________________________________________________________________________________
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>

int main( int argc, char** argv )
{
    IplImage *im_gray;
    int height, width,i,j,c=0,size,counter=0,temp,cnt=0;

    int arr[500]={0};
    int s[256]={0};
    CvScalar v;
    int a=0;
    double size1;
   
    im_gray = cvLoadImage("C:\\Users\\admin\\Desktop\\imag2.jpg");
    cvShowImage("Original Image",im_gray);
    if (!im_gray)
    {
        printf("Cannot find the image");
    }
   
    height = im_gray->height;
    width = im_gray->width;

    printf("Height: %d\n",height);
    printf("Width: %d\n",width);
   
   
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
       
          v = cvGet2D(im_gray,i,j);
          temp =  v.val[-1];   
          arr[temp] = arr[temp]+1;

        }
    }

    size = width*height;
    size1=255.0/size;

    for(i=0;i<256;i++)
    {
      cnt = cnt+arr[i];
      s[i] = size1*cnt;
    }
   
    for(i=0;i<width;i++)
    {
        for(j=0;j<height;j++)
        {
            v = cvGet2D(im_gray,i,j);
            temp =  v.val[-1];   
            v.val[-1]=s[temp];
            cvSet2D(im_gray,i,j,v);
        }
    }
   
      printf("Histogram Sucess");
      cvShowImage("Output Image",im_gray);
      cvWaitKey(0);
      return 0;
    }



No comments:

Post a Comment