Automatic Contrast Adjustment

Automatic Contrast Adjustment

Hello All, 

I am trying to perform Auto Contrast in my grey images using Intel IPP functions. I am not sure where I am going wrong but the image becomes too dark. Here is the code. 

#define GrayLevels 256

 int  main()

{

      std::string path("C:\\sharath\\Images\\");

      std::string inputImageFile = path + "DiffFing.bmp";

       IppImage<Ipp8u,1> imgsrc;

      ippiLoadBMP(inputImageFile.c_str(),imgsrc);

      IppImage<Ipp8u,1> imgDst;

      IppiSize imgSize;

      Ipp32s imgLevels[GrayLevels+1], imgHist[GrayLevels];

      Ipp32s imgValues[GrayLevels];

       int w = imgsrc.Width();

      int h = imgsrc.Height();

      int nlevels = GrayLevels;

      float ww=0;

      int min=0;

      int max=GrayLevels-1;

      int slope;

      float cut_low = 0.05f;

      float cut_high = 0.95f;

       imgSize.width = w;

      imgSize.height = h;

 

      ippiHistogramEven(imgsrc, imgSize, imgHist, imgLevels, GrayLevels+1, 0, GrayLevels);

       while(ww<cut_low)

      {

            ww+=imgHist[min++];

      }

      while(ww<cut_high)

      {

            ww+=imgHist[max--];

      }

       slope = (GrayLevels-1)/(max-min);

     for(int i=0;i<GrayLevels;i++)

      {

            if(i<=min)

            {

                  imgValues[i] = 0;

            }

            else if(i>=max)

            {

                  imgValues[i] = GrayLevels-1;

            }

            else

            {

                  imgValues[i] = slope*(i-min);

            }

      }

 

      imgDst.ResizeNE(imgsrc.Width(),imgsrc.Height());

       ippiLUT(imgsrc, imgDst, imgSize, imgValues, imgLevels, GrayLevels+1);

       std::string out4f = path + "DiffFing_Auto.bmp";

      ippiSaveBMP(out4f.c_str(),imgDst);

       return(EXIT_SUCCESS);

}

 Can somebody help me figure out why my image gets darker instead of enhancing the contrast. 

I have also attached the files for reference. DiffFing_AutoContrast.bmp is the image after autocontrast. 

Thanks, 

Sharath

AttachmentSize
Download difffing.bmp11.31 KB
Download difffing-autocontrast.bmp11.31 KB
3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

In the while-statement,

    while(ww<cut_low)
      {
            ww+=imgHist[min++];
      }

If I'm not mistaken, there is an error in datatype; ww is accumulating 32s values from imgHist, but cut_low is expressed in percentile, so a proper way would be to first compute the sum of all imgHist bins as 64u, and then have ww also as 64u, with value computed from sum*percentile.

Speaking about corrected image ( difffing-autocontrast.bmp ) it looks good.

My question is what Contrast value do you set in case of Auto Contrast correction?

For example, if contrast is set to 25% the resulting image will be too pale ( details are lost ), and if contrast is set to 75% the resulting image will be darker and this is what you have.

Leave a Comment

Please sign in to add a comment. Not a member? Join today