重新着色图像保持亮度

时间:2017-07-01 19:27:01

标签: opencv image-processing

考虑到输入图像,我正在考虑如何将图像重新着色为单一的新颜色,使图像的亮度保持与之前的相似。
所以我写了一个天真的代码:

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <bits/stdc++.h>
using namespace cv;
using namespace std;

int main() {
    Mat img = imread("test2.png", 1);
    Mat hsv; cvtColor(img, hsv, CV_BGR2HSV);    
    vector<Mat > channels;split(hsv, channels);
    Mat luminance; channels[2].copyTo(luminance);
    Mat res; img.copyTo(res);
    channels.clear(); split(res, channels);
    for (int i = 0; i<res.rows; i++) {
        for (int j = 0; j<res.cols; j++) {
            channels[0].at<uchar>(i, j) = 0;
            channels[1].at<uchar>(i, j) = 0; 
            channels[2].at<uchar>(i, j) = 255;
        }
    }
    merge(channels, res);
    cvtColor(res, hsv, CV_BGR2HSV);
    channels.clear(); split(hsv, channels);
    luminance.copyTo(channels[2]);
    merge(channels, res);
    cvtColor(res, res, CV_HSV2BGR);
    imwrite("result.png", res);
    return 0;
}

我实际上只是提取了原始图像的亮度图,然后使用我想要的颜色创建了一个图像,并用输入图像的亮度图替换了该输出图像的亮度图。登记/> 但结果图像在阴影下看起来更暗。有没有更好的办法呢?
输入图片: This is the input image. 结果图片: This is output which I am getting, which is darker.

2 个答案:

答案 0 :(得分:0)

我认为你正在寻找“tinting”。我没有任何关于如何使用 OpenCV 的参考资料,但是在Anthony Thyssen的优秀 ImageMagick 笔记here中有一个描述 - 搜索单词“不知何故”。如果你想要的效果,也许你可以将它改编为 OpenCV

在命令行中,使用 ImageMagick ,我这样做了:

convert drop.png -fill red -tint 50% result.jpg

enter image description here

答案 1 :(得分:0)

这是Imagemagick的另一种方式。

convert \( input.png -colorspace gray \) \( -clone 0 -fill red -colorize 100 \) \( -clone 0 \) -compose colorize -composite result1.png

grayscale = rec709luma result

convert \( input.png -colorspace lab -channel red -separate \) \( -clone 0 -fill red -colorize 100 \) \( -clone 0 \) -compose colorize -composite result2.png

lab luminance result

convert \( input.png -colorspace hsi -channel blue -separate \) \( -clone 0 -fill red -colorize 100 \) \( -clone 0 \) -compose colorize -composite result3.png

hsi intensity result

选择颜色空间代表您要使用的强度/亮度。请参阅http://www.fmwconcepts.com/imagemagick/color2gray/index.php处的我的脚本color2gray,了解不同的色彩空间强度/亮度显示为灰色。