7

I'm used to contrast enhancement in Matlab using imadjust. Is there any equivalent function in OpenCV?

A google search gives the OpenCV documentation on brightness and contrast enhancement but it uses for loops which might be inefficient. Even if we make it efficient by using Matrix expressions, it is not equivalent to what imadjust does.

Is there any in-built function in OpenCV or any efficient method for the task?

I saw related posts but either they link to the OpenCV doc I mentioned above or they suggest Histogram Equalization and thresholding. I prefer imadjust to histogram equalization and thresholding doesn't seem to perform contrast enhancement as such.

Any help on this is appreciated.

Community
  • 1
  • 1
Ruchir
  • 845
  • 2
  • 10
  • 24
  • Try to post some sample code and your expected output so that we can help. –  Jul 27 '15 at 08:02
  • @Derman, thanks for the reply. I can't give sample code in OpenCV because that's what I'm looking for. However, you can find sample results on this page http://www.mathworks.com/help/images/ref/imadjust.html which is the reference for imadjust function. I'm expecting to do the same thing as what imadjust does in Matlab. Hope that clarifies. – Ruchir Jul 27 '15 at 08:21
  • Have a look at [equalizeHist](http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_equalization/histogram_equalization.html) – Miki Jul 27 '15 at 08:50
  • 1
    @Miki, equalizeHist uses histogram equalization which is different from what imadjust does. Please see this link (https://www.mathworks.com/matlabcentral/newsreader/view_thread/253503) for more information on the difference. It discourages using histeq for processing and even I've observed better results using imadjust. – Ruchir Jul 27 '15 at 09:00
  • Thanks @Miki for the guideline. I will try to code myself so that I learn something. In case I still fail, I'll comment here. Thanks anyway for offering the code :) – Ruchir Jul 27 '15 at 09:58
  • opencv contrib includes the class [`cv::xphoto::SimpleWB`](http://docs.opencv.org/3.2.0/d1/d8b/classcv_1_1xphoto_1_1SimpleWB.html), which performs histogram stretching similar to `imadjust`. Here's the implementation: https://github.com/opencv/opencv_contrib/blob/3.2.0/modules/xphoto/src/simple_color_balance.cpp – Amro May 31 '17 at 02:12

3 Answers3

17

There's no builtin solution in OpenCV to perform histogram stretching, but you can do it easily in a loop.

imadjust allows to select a tolerance for upper and lower bounds, or the bounds directly, so you need a little more logic than a simple for loop.

You can use the example below as a reference while implementing your own:

#include <opencv2\opencv.hpp>
#include <vector>
#include <algorithm>

using namespace std;
using namespace cv;

void imadjust(const Mat1b& src, Mat1b& dst, int tol = 1, Vec2i in = Vec2i(0, 255), Vec2i out = Vec2i(0, 255))
{
    // src : input CV_8UC1 image
    // dst : output CV_8UC1 imge
    // tol : tolerance, from 0 to 100.
    // in  : src image bounds
    // out : dst image buonds

    dst = src.clone();

    tol = max(0, min(100, tol));

    if (tol > 0)
    {
        // Compute in and out limits

        // Histogram
        vector<int> hist(256, 0);
        for (int r = 0; r < src.rows; ++r) {
            for (int c = 0; c < src.cols; ++c) {
                hist[src(r,c)]++;
            }
        }

        // Cumulative histogram
        vector<int> cum = hist;
        for (int i = 1; i < hist.size(); ++i) {
            cum[i] = cum[i - 1] + hist[i];
        }

        // Compute bounds
        int total = src.rows * src.cols;
        int low_bound = total * tol / 100;
        int upp_bound = total * (100-tol) / 100;
        in[0] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), low_bound));
        in[1] = distance(cum.begin(), lower_bound(cum.begin(), cum.end(), upp_bound));

    }

    // Stretching
    float scale = float(out[1] - out[0]) / float(in[1] - in[0]);
    for (int r = 0; r < dst.rows; ++r)
    {
        for (int c = 0; c < dst.cols; ++c)
        {
            int vs = max(src(r, c) - in[0], 0);
            int vd = min(int(vs * scale + 0.5f) + out[0], out[1]);
            dst(r, c) = saturate_cast<uchar>(vd);
        }
    }
}

int main()
{
    Mat3b img = imread("path_to_image");

    Mat1b gray;
    cvtColor(img, gray, COLOR_RGB2GRAY);

    Mat1b adjusted;
    imadjust(gray, adjusted);

    // int low_in, high_in, low_out, high_out
    // imadjust(gray, adjusted, 0, Vec2i(low_in, high_in), Vec2i(low_out, high_out));

    return 0;
}

Input image:

enter image description here

Output adjusted image:

enter image description here

Miki
  • 40,887
  • 13
  • 123
  • 202
  • 1
    Your code is impressive - your histogram is about 8 times faster than STL algorithm std::nth_element. That is, when I removed all mention to openCV. I run it on 3000 images at 950K clocks, instead of STL 7750K clocks. – mousomer Nov 18 '15 at 14:18
  • This is nice. A bit cleaner would be to use `cv::calcHist` to calculate the histogram, then use `std::vector` to compute the CDF and find the upper and lower bounds, and finally use `cv::threshold` to clamp the source between upper and lower bounds, then `cv::normalize` with `cv::NORM_MINMAX` to do the scaling. The advantage of this approach is that you're reusing CV primitives, you get support for all supported data types, and all of this can be pushed to GPU. – WillY Apr 20 '21 at 20:55
1

There is a implementation of imadjust and stretchlim here:

https://github.com/joshdoe/opencv/blob/1d319f683f6b9a8b0c7cbe2abdc9664f0dac919f/modules/imgproc/src/imadjust.cpp

Freeman
  • 5,810
  • 3
  • 47
  • 48
0

You could try asking to the guy here: http://opencv-users.1802565.n2.nabble.com/imadjust-matlab-function-with-stretchlim-OpenCV-implementation-td6253242.html

which based his implementation on this: http://www.mathworks.com/matlabcentral/fileexchange/12191-bilateral-filtering

The file should be the following but I am not completely sure it works:

void
getOptimalImgAdjustParamsFromHist (IplImage* p_img,unsigned int* p_optminmaxidx, int p_count)
{
  int numBins = 256;
  CvMat* bins = cvCreateMat(1,numBins,CV_8UC1);
  calcHistogram(p_img,bins,numBins);
  int sumlow = 0, sumhigh = 0;
  int low_idx = 0, high_idx = 0;
  for (unsigned int i = 0; i < numBins; i++) {
    float curval = (float) cvGetReal1D (bins, (i));
    sumlow += curval;
    if (sumlow >= p_count) {
      low_idx = i;
      break;
    }
  }
  for (unsigned int i = numBins - 1 ; i >= 0; i--) {
    float curval = (float) cvGetReal1D (bins, (i));
    sumhigh += curval;
    if (sumhigh >= p_count) {
      high_idx = i;
      break;
    }
  }
  cvReleaseMat(&bins);
  p_optminmaxidx[OPTMINIDX] = low_idx;
  p_optminmaxidx[OPTMAXIDX] = high_idx;
}

IplImage *
imageAdjust (IplImage * p_img)
{
  CvSize framesize = cvGetSize (p_img);
  int low_count = round (framesize.width * framesize.height * 0.01);
  unsigned int *optminmaxidx = new unsigned int [2];
  getOptimalImgAdjustParamsFromHist (p_img, optminmaxidx,low_count);
  int range = optminmaxidx[OPTMAXIDX] - optminmaxidx[OPTMINIDX];
  IplImage *adjustedImg = p_img;
  for (int i = 0; i < framesize.height; i++)
    for (int j = 0; j < framesize.width; j++) {
      unsigned int val = (unsigned int) getData (p_img, i, j);
      unsigned int newval = 0;
      if (val <= optminmaxidx[OPTMINIDX]) {
        newval = 0;
        setData (adjustedImg, i, j, (uchar) newval);
      } else if (val >= optminmaxidx[OPTMAXIDX]) {
        newval = 255;
        setData (adjustedImg, i, j, (uchar) newval);
      } else {
        newval =
            (unsigned int) round ((double) (((double) val -
                    (double) optminmaxidx[OPTMINIDX]) * (double) (255.0 /
                    (double) range)));
        setData (adjustedImg, i, j, (uchar) newval);
      }
    }
  delete[]optminmaxidx;
  return adjustedImg;
}

I hope it helps you. Fab.

desmond13
  • 2,913
  • 3
  • 29
  • 46