Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将RGB值转换为颜色矩阵_C#_Vb.net_Math_System.drawing_Colormatrix - Fatal编程技术网

C# 将RGB值转换为颜色矩阵

C# 将RGB值转换为颜色矩阵,c#,vb.net,math,system.drawing,colormatrix,C#,Vb.net,Math,System.drawing,Colormatrix,我想给黑白图像上色 为此,我使用了一个颜色矩阵。 例如,当我想将图像涂成红色时,我使用以下颜色矩阵: 1,0,0,0,0, 0,1,0,0,0, 0,0,1,0,0, 0,0,0,1,0, 1,0,0,0,0, 现在,我想从颜色图表中选择一种颜色,例如,从这样的颜色图表中: 我的计划是使用GetPixel检索光标下的颜色,并将此RGB值转换为ColorMatrix 当我在红色上使用GetPixel时,我会 R: 255 G: 0 B: 0 如何将255的红色值转换为ColorMatrix值

我想给黑白图像上色

为此,我使用了一个颜色矩阵。 例如,当我想将图像涂成红色时,我使用以下颜色矩阵:

1,0,0,0,0,
0,1,0,0,0,
0,0,1,0,0,
0,0,0,1,0,
1,0,0,0,0,
现在,我想从颜色图表中选择一种颜色,例如,从这样的颜色图表中:

我的计划是使用GetPixel检索光标下的颜色,并将此RGB值转换为ColorMatrix

当我在红色上使用GetPixel时,我会

R: 255
G: 0
B: 0
如何将255的红色值转换为ColorMatrix值? 应该是1.0。 如果红色值为127,则该值为0.5


我应该简单地使用“三个规则”来确定1.0的值,还是应该如何处理?

首先,我认为您应该阅读本课程,也许可以更新您对矩阵的理解(尽管
ColorMatrix
文档提供了足够的信息,您不需要理解矩阵数学)

让我们看看你的矩阵:

1,0,0,0,0
0,1,0,0,0
0,0,1,0,0
0,0,0,1,0
1,0,0,0,0
该矩阵将按1缩放红色、绿色、蓝色和alpha组件,然后将1添加到红色组件,将0添加到绿色、蓝色和alpha组件

我认为这不是你真正想要的。为了给图像着色,我认为最好将图像的RGB值相乘,而不是相加。否则,如果您的黑白图像的像素是白色的(即RGB 1、1、1),则当您尝试为其着色时,它将保持白色,而此时它应与您应用于它的颜色一致

让我们更全面地了解一下矩阵,看看它如何影响像素颜色:

Sr,  0,  0,  0, 0
 0, Sg,  0,  0, 0
 0,  0, Sb,  0, 0
 0,  0,  0, Sa, 0
Tr, Tg, Tb, Ta, 1
S
代表“比例”,而
T
代表翻译。您的红色分量将乘以
Sr
,然后添加
Tr
,绿色分量将乘以
Sg
,然后添加
Tg
,蓝色分量将乘以
Sb
,然后添加
Tb
,您的alpha分量将乘以
Sa
,然后添加
Ta

因此,要构造一个矩阵使图像着色,您可以执行以下操作:

Color colour = colourDialog.Color; // Or wherever you get your Color instance from
float[][] colourMatrixElements =
{
    new float[] { colour.R / 255.0f, 0, 0, 0, 0 },
    new float[] { 0, colour.G / 255.0f, 0, 0, 0 },
    new float[] { 0, 0, colour.B / 255.0f, 0, 0 },
    new float[] { 0, 0, 0, 1, 0 },
    new float[] { 0, 0, 0, 0, 1 }
};
var colourMatrix = new ColorMatrix(colorMatrixElements);

现在,您可以使用
ColorMatrix
为图像着色。

首先,我认为您应该阅读本课程,或许可以更新您对矩阵的理解(尽管
ColorMatrix
文档提供了足够的信息,您不需要理解矩阵数学)

让我们看看你的矩阵:

1,0,0,0,0
0,1,0,0,0
0,0,1,0,0
0,0,0,1,0
1,0,0,0,0
该矩阵将按1缩放红色、绿色、蓝色和alpha组件,然后将1添加到红色组件,将0添加到绿色、蓝色和alpha组件

我认为这不是你真正想要的。为了给图像着色,我认为最好将图像的RGB值相乘,而不是相加。否则,如果您的黑白图像的像素是白色的(即RGB 1、1、1),则当您尝试为其着色时,它将保持白色,而此时它应与您应用于它的颜色一致

让我们更全面地了解一下矩阵,看看它如何影响像素颜色:

Sr,  0,  0,  0, 0
 0, Sg,  0,  0, 0
 0,  0, Sb,  0, 0
 0,  0,  0, Sa, 0
Tr, Tg, Tb, Ta, 1
S
代表“比例”,而
T
代表翻译。您的红色分量将乘以
Sr
,然后添加
Tr
,绿色分量将乘以
Sg
,然后添加
Tg
,蓝色分量将乘以
Sb
,然后添加
Tb
,您的alpha分量将乘以
Sa
,然后添加
Ta

因此,要构造一个矩阵使图像着色,您可以执行以下操作:

Color colour = colourDialog.Color; // Or wherever you get your Color instance from
float[][] colourMatrixElements =
{
    new float[] { colour.R / 255.0f, 0, 0, 0, 0 },
    new float[] { 0, colour.G / 255.0f, 0, 0, 0 },
    new float[] { 0, 0, colour.B / 255.0f, 0, 0 },
    new float[] { 0, 0, 0, 1, 0 },
    new float[] { 0, 0, 0, 0, 1 }
};
var colourMatrix = new ColorMatrix(colorMatrixElements);

现在,您可以使用
彩色矩阵
为图像着色。

如何获得问题中给出的反向RGB矩阵。4x4如何获得问题中给出的反向RGB矩阵。4x4