Intel® Integrated Performance Primitives
Deliberate problems developing high-performance vision, signal, security, and storage applications.
Announcements
FPGA community forums and blogs have moved to the Altera Community. Existing Intel Community members can sign in with their current credentials.

Automatic Contrast Adjustment

SharathKum_D_Intel
1,049 Views

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 = 0;

            }

            else if(i>=max)

            {

                  imgValues = GrayLevels-1;

            }

            else

            {

                  imgValues = 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

0 Kudos
2 Replies
Thomas_Jensen1
Beginner
1,049 Views

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.

0 Kudos
SergeyKostrov
Valued Contributor II
1,049 Views
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.
0 Kudos
Reply