Opencv中图像质量的改善 [英] Image quality improvement in Opencv

查看:41
本文介绍了Opencv中图像质量的改善的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个图像.一种具有更多的绿色,另一种具有更好的质量(具有正确的颜色).如何改善第一个颜色,使其具有与第二个颜色相似的颜色.我使用了对比度增强功能

 //对比度增强for(int y = 0; y< rotation.rows; y ++){for(int x = 0; x< rotation.cols; x ++){对于(int c = 0; c< 3; c ++){//"**输入Alpha值[1.0-3.0]:"//"**输入beta值[0-100]:";Vec3b>(y,x)[c] =saturate_cast uchar(2.5 *(在Vec3b上旋转(y,x)[c])+ 30);}}} 

它使图像变亮.但是我喜欢与第二种颜色相似.要更改为具有第二张图像颜色的RGB值是多少?

解决方案

要增强对比度,您可以使用Matlab的等效工具

这里是完整代码:

  #include< opencv2 \ opencv.hpp>#include< vector>#include< algorithm>使用命名空间std;使用命名空间cv;void imadjust(const Mat1b& src,Mat1b& dst,int tol = 1,Vec2i in = Vec2i(0,255),Vec2i out = Vec2i(0,255)){//src:输入CV_8UC1图片//dst:输出CV_8UC1 imge//tol:公差,从0到100.//in:src图像范围//out:dst图像增强dst = src.clone();tol = max(0,min(100,tol));如果(tol> 0){//计算出入限制//直方图向量< int>hist(256,0);for(int r = 0; r< src.rows; ++ r){for(int c = 0; c< src.cols; ++ c){hist [src(r,c)] ++;}}//累积直方图向量< int>暨=历史;for(int i = 1; i  

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.

解决方案

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

这篇关于Opencv中图像质量的改善的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆