关于 ColorMatrix 转换如何工作的说明
我正在图像处理应用程序上做一些工作(为了好玩),并且正在努力完全理解 ColorMatrix 转换的工作原理。我了解了线性/仿射变换的基础知识,并且可以通过在线复制示例来很好地完成,但我想完全掌握为什么某些东西有效,而不是仅仅满足于它有效。
例如,对图像进行简单的变换以产生其负片(每种颜色都转换为其各自的补色)使用以下矩阵:
[-1, 0, 0, 0, 0]
[0, -1, 0, 0, 0]
[0, 0, -1, 0, 0]
[0, 0, 0, 1, 0]
[1, 1, 1, 0, 1]
我知道 -1 是 180 度的余弦,这是“翻转”a 所需的旋转颜色与其互补,但我不明白的是如何将颜色向量与上述矩阵相乘并产生正确的互补向量。
例如,如果像素的颜色向量为 [247, 255, 0, 255, 1](使用 RGBAW 空间),则对上述矩阵执行乘法会产生 [-247, -255, 0, 255, 1] ,但这并不正确,因为上面的真正补色是 [8, 0, 255, 255, 1]。
我在这里遗漏了一些明显的东西,并且很高兴地承认我不完全确定我在做什么:)正在转换的颜色矢量是否在其他坐标系中表示? (例如不是0-255)
如果有人可以帮助提供我理解的“缺失环节”,我将非常感激。
编辑
我刚刚发现以下矩阵也有效,并且实际上在数学上很直观(它产生正确的向量)。
-1 0 0 0 0
0 -1 0 0 0
0 0 -1 0 0
1 1 1 1 0
0 0 0 0 1
所以我的新问题是:为什么这两个矩阵都有效?后一个为我提供了更令人满意的解决方案,因为我可以从代数的角度理解它的工作原理。四行是用来缩放的吗?如果是这样,为什么缩放会增加 255?它从哪里获得这个价值?
抱歉,如果这些问题真的很愚蠢,我正在努力解决这个问题。
I'm doing some work on an image processing app (for fun) and am struggling to fully understand how ColorMatrix transformations work. I get the basics of linear/affine transformations, and can get by just fine by replicating examples online, but I'd like to fully grasp why something works instead of just being satisfied that it works.
For example, doing a simple transformation on an image to produce its negative (each color is converted to its respective complimentary) uses the following matrix:
[-1, 0, 0, 0, 0]
[0, -1, 0, 0, 0]
[0, 0, -1, 0, 0]
[0, 0, 0, 1, 0]
[1, 1, 1, 0, 1]
I understand that -1 is the cosine of 180degrees, which is the rotation needed to "flip" a color to it's complementary, but what I don't understand is how a color vector can be multiplied against the above matrix and produce the correct complementary vector.
For instance, if a pixel has the color vector of [247, 255, 0, 255, 1] (using the RGBAW space), performing the multiplication against the above matrix produces [-247, -255, 0, 255, 1], but that isn't correct since the real complementary color of the above is [8, 0, 255, 255, 1].
I'm missing something obvious here and am happy to admit that I'm not completely sure what I'm doing :) Is the color vector being transformed represented in some other coordinate system? (e.g. not 0-255)
If anyone could help provide the "missing link" of my understanding, I'd be really appreciative.
Edit
I just discovered that the following matrix also works and is actually mathemtically intuitive (it produces the correct vector).
-1 0 0 0 0
0 -1 0 0 0
0 0 -1 0 0
1 1 1 1 0
0 0 0 0 1
So my new question is: why do both of these matrices work? The latter one provides me with the more satisfying solution since I can grasp why it works from an algebraic perspective. Is the four row used for scaling? And if so, why does scaling add 255? Where does it get that value from?
Sorry if these are really stupid questions, I'm trying to get this down pat.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你是对的,底线是翻译。 Hans 在他的评论中所说的是涉及 255 的缩放因子,这可能会让您感到困惑。看待这个问题的一种方法是,所有 ARGB 值首先除以 255,然后应用矩阵乘法,然后将所有值乘以 255 以给出正确的 ARGB 值。另一种看待它的方法是将平移值视为 255。两种方法都会得到相同的结果。
You are correct that the bottom line is for translation. What Hans is saying in his comment is that there's a scaling factor of 255 involved, which is probably what's confusing you. One way to look at this is that all ARGB values are first divided by 255, then the matrix multiplication is applied and then all values are multiplied back by 255 to give the correct ARGB values. Another way to look at it is to think of the translation values being 255 as big. You will arrive at the same result both ways.