重新着色图像保持亮度 [英] Re-coloring image preserving luminance

查看:201
本文介绍了重新着色图像保持亮度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到输入图像,我正在考虑如何将图像重新着色为单一的新颜色,使图像的亮度保持与之前的相似。

所以我写了一个天真的代码:

Given an input image, I was thinking about how the image could be re-colored to a single new color keeping the luminance of the image similar to what it was earlier.
So I wrote a naive code:

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

我实际做的只是提取原始图像的亮度图,然后使用我想要的颜色创建一个图像,并用输入图像的亮度图替换此输出图像的亮度图。

但是结果图像在阴影中看起来更暗。有没有更好的方法呢?

输入图片:

结果图片:

What I actually did is just extracted the luminance map of the original image, then created an image with the color I want it to be in, and replaced the luminance map of this output image with the luminance map of input image.
But resultant image seems to be darker in shade. Is there any better way to do this?
Input image: Resulting image:

推荐答案

这是Imagemagick的另一种方式。

Here is another way in Imagemagick.

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

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

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

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

Choose what colorspace represents the intensity/luminance you want to use. See my script, color2gray at http://www.fmwconcepts.com/imagemagick/color2gray/index.php to see what different colorspace intensity/luminance show as gray.

这篇关于重新着色图像保持亮度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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