Is there any function equivalent to Matlab imadjust in OpenCV with C ++?

I am using contrast enhancement in Matlab using imadjust. Is there an equivalent function in OpenCV?

A google search provides OpenCV documentation on improving brightness and contrast, but it uses for loops that may be inefficient. Even if we make it efficient using Matrix expressions , this is not equivalent to imadjust .

Is there a built-in function in OpenCV or some efficient method for a task?

I have seen related messages, but they refer to the OpenCV document that I mentioned above, or they offer histogram alignment and threshold value . I prefer to imadjustflatten the histogram, and the threshold value does not seem to perform contrast enhancement as such.

Any help on this is appreciated.

+4
source share
2 answers

OpenCV does not have a built-in solution for performing histogram stretching, but you can do this easily in a loop.

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

You can use the example below as a reference when 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;
}

:

enter image description here

:

enter image description here

+13

: http://opencv-users.1802565.n2.nabble.com/imadjust-matlab-function-with-stretchlim-OpenCV-implementation-td6253242.html

: http://www.mathworks.com/matlabcentral/fileexchange/12191-bilateral-filtering

, , :

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;
}

, . Fab.

0

Source: https://habr.com/ru/post/1599821/


All Articles