着色灰度图像 [英] Colorize a grayscale image

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

问题描述

我有一个灰度图像,需要用5种颜色的渐变值绘制。亮像素应该从渐变中获得较浅的颜色,暗像素应该变得更暗。

I have a grayscale image that needs to be painted with values from a gradient of 5 colors. Light pixels should get a lighter color from the gradient, dark pixels should get a darker one.

如果渐变以黄色开始并以红色结束,则白色像素将是黄色和黑色像素红色,其中一切都在渐变之间。

If the gradient starts with yellow and ends with red, then white pixels would be yellow and black pixels red with everything in between along the gradient.

可以用ColorMatrix完成吗?

Can it be done with ColorMatrix?

推荐答案

你想要的是在最简单的情况下在3d中的5个点之间的线性插值。两种颜色{r1,g1,b1}和{r2,g2,b2}之间的直接颜色渐变,就像你将走到3d中连接点{r1,g1,b1}和{r2,g2, B2}。
当你的灰度值在区间[0,1]中时,如果你有一个灰度级0和{r2,g2,b2},你可能想要颜色{r1,g1,b1}如果你有一个灰度级1.问题是如何计算两者之间的颜色:

What you want is in the simplest case a linear interpolation between 5 points in 3d. A direct color gradient between two colors {r1,g1,b1} and {r2,g2,b2} is, as you would walk onto the line in 3d which connects the points {r1,g1,b1} and {r2,g2,b2}. When your gray-values are in the interval [0,1], then you would want to have the color {r1,g1,b1} if you have a gray-level 0 and {r2,g2,b2} if you have a gray-level 1. The question is how to calculate the colors in between:

想想简单的学校矢量分析。 c1是点{r1,g1,b1},c2是{r2,g2,b2}。从c1开始,沿着向量c2-c1的方向前进:

think of the simple school vector analysis. c1 is the point {r1,g1,b1}, c2 is {r2,g2,b2}. Start at c1 and go in direction of the vector c2-c1:

outColor = c1 + gray *(c2-c1)

outColor = c1 + gray*(c2-c1)

或等价

outColor =(1-grey) c1 + grey c2

outColor = (1-gray)c1 + grayc2

Remenber,灰色必须位于区间[0,1]。否则你必须重新缩放它。这是两点之间的简单线性插值,当然可以根据需要扩展为多种颜色。

Remenber, gray must lie in the interval [0,1]. Otherwise you have to rescale it. This is simple linear interpolation between two points which can of course be extended for as many colors as you want.

对于五种颜色,方法基本相同。这里有5种随机选择的颜色:

For five colors the approach is basically the same. Here for 5 randomly chosen colors:

{{0.273372, 0.112407, 0.0415183}, 
 {0.79436,  0.696305, 0.167884}, 
 {0.235083, 0.999163, 0.848291}, 
 {0.295492, 0.780062, 0.246481}, 
 {0.584883, 0.460118, 0.940826}}

您必须知道的两件重要事情:

Two important things you have to know:


  1. 如果你想拥有对于给定灰度值的颜色,首先需要提取两个周围的rgb点。例如,如果你想得到灰色= 0.1的颜色,它将是第一个和第二个rgb点。

  1. If you want to have the color for a given gray-value, you first need to extract the two surrounding rgb-points. For instance if you want to have the color for gray=0.1 it would be the first and the second rgb-point.

必须根据渐变中使用的颜色数重新缩放灰度值。看看图像,有5种颜色,你有4个不同的两点插值间隔。如果要使上述颜色公式有效,则必须将每个颜色间隔的灰度值重新调整为[0,1]。

You have to rescale your gray-value according to the number of colors which you use in your gradient. Look at the image, with 5 colors, you have 4 different two point interpolation intervals. When you want to make the above mentioned color formula work, you have to rescale your gray-value for each color-interval to [0,1].

备注:这当然不是C#中实现的解决方案,但您的问题建议,创建相同大小的彩色图像不是您的问题。计算每个灰度值的颜色值是着色的关键。

Remark: This is of course not the solution for the implementation in C#, but your question suggested, that creating a color-image of the same size was not your problem. Calculating the color-values for each gray-value is the key for your colorization.

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

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