image-processing - 使用 OpenCV 均衡/标准化彩色图像中的色相饱和度亮度

标签 image-processing opencv

我想对同一主题的两个半脸彩色图像进行均衡,然后将它们合并。它们每个都有不同的色调饱和度和亮度值....使用 opencv 我如何标准化/均衡每个半图像?

我尝试执行 cvEqualizeHist(v, v);在转换后的 HSV 图像的 v 值上,但两幅图像仍然存在显着差异,合并后两半的颜色之间仍然有一条线...谢谢

最佳答案

您是否尝试阅读此链接? http://answers.opencv.org/question/75510/how-to-make-auto-adjustmentsbrightness-and-contrast-for-image-android-opencv-image-correction/

void Utils::BrightnessAndContrastAuto(const cv::Mat &src, cv::Mat &dst, float clipHistPercent)
{

    CV_Assert(clipHistPercent >= 0);
    CV_Assert((src.type() == CV_8UC1) || (src.type() == CV_8UC3) || (src.type() == CV_8UC4));

    int histSize = 256;
    float alpha, beta;
    double minGray = 0, maxGray = 0;

    //to calculate grayscale histogram
    cv::Mat gray;
    if (src.type() == CV_8UC1) gray = src;
    else if (src.type() == CV_8UC3) cvtColor(src, gray, CV_BGR2GRAY);
    else if (src.type() == CV_8UC4) cvtColor(src, gray, CV_BGRA2GRAY);
    if (clipHistPercent == 0)
    {
        // keep full available range
        cv::minMaxLoc(gray, &minGray, &maxGray);
    }
    else
    {
        cv::Mat hist; //the grayscale histogram

        float range[] = { 0, 256 };
        const float* histRange = { range };
        bool uniform = true;
        bool accumulate = false;
        calcHist(&gray, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate);

        // calculate cumulative distribution from the histogram
        std::vector<float> accumulator(histSize);
        accumulator[0] = hist.at<float>(0);
        for (int i = 1; i < histSize; i++)
        {
            accumulator[i] = accumulator[i - 1] + hist.at<float>(i);
        }

        // locate points that cuts at required value
        float max = accumulator.back();
        clipHistPercent *= (max / 100.0); //make percent as absolute
        clipHistPercent /= 2.0; // left and right wings
        // locate left cut
        minGray = 0;
        while (accumulator[minGray] < clipHistPercent)
            minGray++;

        // locate right cut
        maxGray = histSize - 1;
        while (accumulator[maxGray] >= (max - clipHistPercent))
            maxGray--;
    }

    // current range
    float inputRange = maxGray - minGray;

    alpha = (histSize - 1) / inputRange;   // alpha expands current range to histsize range
    beta = -minGray * alpha;             // beta shifts current range so that minGray will go to 0

    // Apply brightness and contrast normalization
    // convertTo operates with saurate_cast
    src.convertTo(dst, -1, alpha, beta);

    // restore alpha channel from source 
    if (dst.type() == CV_8UC4)
    {
        int from_to[] = { 3, 3 };
        cv::mixChannels(&src, 4, &dst, 1, from_to, 1);
    }
    return;
}

关于image-processing - 使用 OpenCV 均衡/标准化彩色图像中的色相饱和度亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7152195/

相关文章:

java - 在Java中计算两个图像直方图之间的卡方距离

c++ - 检测草图像中的植物

arrays - 在 PowerShell 中调整字节数组图像的大小

c# - 在图像文件中定位点的最佳算法是什么?

opencv - 结果中的均值和高斯滤波器之间的差异

c# - 如何匹配两个图像的 EMGU CV SIFT 关键点?

python - opencv-python imshow 在 mac 中给出错误

c++ - 将 libopencv_gpu.so 与 OpenCV 链接时 undefined reference

c++ - 将opencv矩形坐标转换为yolo对象坐标以进行图像标记

python - 在图像文件中将特定 RGB 颜色像素更改为另一种颜色