Image quality improvement in Opencv

倖福魔咒の 提交于 2021-01-28 00:00:31

问题


I have two images. One has more green color and another one has better quality (it has right color). How can I improve the first one to have the similar color as the second one.I used the contrast enhancement as

//Contrast enhancement
    for (int y = 0; y < rotated.rows; y++)
    {
        for (int x = 0; x < rotated.cols; x++)
        {
            for (int c = 0; c < 3; c++)
            {
                //"* Enter the alpha value [1.0-3.0]: "
                //"* Enter the beta value [0-100]: ";
                rotated.at<Vec3b>(y, x)[c] =
                    saturate_cast<uchar>(2.5*(rotated.at<Vec3b>(y, x)[c]) + 30);
            }
        }
    }

It brightens the image. But I like to have similar color as the second one. What are the RGB values to change to have the second image's color.


回答1:


For contrast enhancement you can use the equivalent of Matlab imadjust. You can find an OpenCV implementation here.

Applying imadjust with default parameters on each separate channel you get:

Here the full code:

#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");

    vector<Mat1b> planes;
    split(img, planes);

    for (int i = 0; i < 3; ++i)
    {
        imadjust(planes[i], planes[i]);
    }

    Mat3b result;
    merge(planes, result);

    return 0;
}


来源:https://stackoverflow.com/questions/32848301/image-quality-improvement-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!