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

Ruchir

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.

Miki

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Equivalent function of Matlab's filter2 in OpenCV

From Dev

what is the equivalent function of matlab 'fix' in opencv?

From Dev

What is the opencv equivalent to matlab's fliplr()?

From Dev

OpenCV equivalent for thresholding in MATLAB

From Dev

OpenCV equivalent for thresholding in MATLAB

From Dev

What is the MATLAB equivalent of Excel's NORMSDIST function?

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

Is there a Python equivalent to MATLAB's pearsrnd function?

From Dev

Is there an equivalent to matlab's rcond() function in Julia?

From Dev

Torch tensor equivalent function to matlab's "find"?

From Dev

Python equivalent to Matlab's set function

From Dev

What is the MATLAB equivalent of Excel's NORMSDIST function?

From Dev

Matlab equivalent of Python's 'reduce' function

From Dev

matlab function "m = size(X,dim)" equivalent in opencv

From Dev

Objective-C equivalent to Swift's "Any"

From Dev

Objective-C equivalent to Swift's "Any"

From Dev

matlab to C++/openCV normalization function

From Dev

Equivalent R function for Matlab

From Dev

Equivalent R function for Matlab

From Dev

Is any alias equivalent to a function?

From Dev

What is the equivalent of matlab's smooth3 function in python?

From Dev

SQL Server - any equivalent of Excel's CHOOSE function?

From Dev

Is there any function in jQuery that is equivalent to PHP's array_column()?

From Dev

Equivalent of MATLAB's hold on

From Dev

Equivalent of MATLAB's hold on

From Dev

Are there any eigenvalue decomposition methods in C++ faster than MATLAB's eig function?

From Dev

Is there a C# equivalent to python's type() function?

From Dev

C# equivalent of Excel's TINV function

Related Related

  1. 1

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

  2. 2

    Equivalent function of Matlab's filter2 in OpenCV

  3. 3

    what is the equivalent function of matlab 'fix' in opencv?

  4. 4

    What is the opencv equivalent to matlab's fliplr()?

  5. 5

    OpenCV equivalent for thresholding in MATLAB

  6. 6

    OpenCV equivalent for thresholding in MATLAB

  7. 7

    What is the MATLAB equivalent of Excel's NORMSDIST function?

  8. 8

    Matlab equivalent of Python's 'reduce' function

  9. 9

    Is there a Python equivalent to MATLAB's pearsrnd function?

  10. 10

    Is there an equivalent to matlab's rcond() function in Julia?

  11. 11

    Torch tensor equivalent function to matlab's "find"?

  12. 12

    Python equivalent to Matlab's set function

  13. 13

    What is the MATLAB equivalent of Excel's NORMSDIST function?

  14. 14

    Matlab equivalent of Python's 'reduce' function

  15. 15

    matlab function "m = size(X,dim)" equivalent in opencv

  16. 16

    Objective-C equivalent to Swift's "Any"

  17. 17

    Objective-C equivalent to Swift's "Any"

  18. 18

    matlab to C++/openCV normalization function

  19. 19

    Equivalent R function for Matlab

  20. 20

    Equivalent R function for Matlab

  21. 21

    Is any alias equivalent to a function?

  22. 22

    What is the equivalent of matlab's smooth3 function in python?

  23. 23

    SQL Server - any equivalent of Excel's CHOOSE function?

  24. 24

    Is there any function in jQuery that is equivalent to PHP's array_column()?

  25. 25

    Equivalent of MATLAB's hold on

  26. 26

    Equivalent of MATLAB's hold on

  27. 27

    Are there any eigenvalue decomposition methods in C++ faster than MATLAB's eig function?

  28. 28

    Is there a C# equivalent to python's type() function?

  29. 29

    C# equivalent of Excel's TINV function

HotTag

Archive