在使用C++的OpenCV中是否存在与Matlab的imadjust等效的函数?

Ruc*_*hir 5 c++ matlab opencv image-processing

我习惯于使用Matlab对比增强imadjust.OpenCV中是否有任何等效功能?

谷歌搜索提供了关于亮度和对比度增强的OpenCV文档,但它使用了可能效率低下的for循环.即使我们使用Matrix表达式使其有效,它也不等同于imadjust所做的.

OpenCV中是否有任何内置函数或任何有效的方法?

我看到相关的帖子,但要么链接到我上面提到的OpenCV文档,要么他们建议直方图均衡和阈值.我更喜欢imadjust直方图均衡,而阈值似乎并不像这样执行对比度增强.

对此有任何帮助表示赞赏.

Mik*_*iki 15

OpenCV中没有用于执行直方图拉伸的内置解决方案,但您可以在循环中轻松完成.

imadjust 允许选择上限和下限的容差,或直接选择边界,因此您需要比简单的for循环更多的逻辑.

您可以在实现自己的示例时使用以下示例作为参考:

#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;
}
Run Code Online (Sandbox Code Playgroud)

输入图片:

在此输入图像描述

输出调整图像:

在此输入图像描述