从 RGB 值计算单个代表 "pixel value"

Calculating a single representative "pixel value" from a RGB value

我的问题与此处的堆栈溢出讨论有关。

https://math.stackexchange.com/questions/161780/about-sum-of-squared-differences

讨论给出了差平方和的公式,答案给出了以下示例。


例如,如果您要比较两个像素(即每个图像中的一个像素),则您有一个 1 像素的区域。假设它是第一行中的第五个像素:x = 0,y = 4。像素值分别为 f、g 的 10,3。对于一个2n1=1=>n1=0的区域,n2也是一样。

SSD=(f(x+i,y+j)−g(x+i,y+j))2
SSD=(f(0+0,4+0)−g(0+0,4+0))2
SSD=(f(0,4)−g(0,4))2
SSD=(10−3)2=49


我的问题是他如何从每个像素的 RGB 值中获取 f(x,y) 的 10 像素值和 g(x,y) 的 3 像素值? RGB 不是单一值。那就是当我有一个图像的单个像素时,我有一个三元组的信息:我有红色、绿色和蓝色的颜色值。如何从这个 RGB 值变为示例提供的单个值?

我假设他使用的是灰度颜色表示,其中值 10 和 3 对应于两个像素的亮度或发光度。

一种将RGB转换为整数表示的方法:

RGB (int) = 

R * (256^2) + 

G * (256) + 

B

... 但是,计算这种表示中值之间的 SSD 将毫无意义,这给我们带来了一般计算颜色距离的问题:任何试图将 RGB 扁平化为单一的维度难免会把一些信息崩塌

将您的 RGB 值表示为 HSL/HSB/HSV 可能使您能够更有意义地比较色调、饱和度或亮度(一次一个维度),但很难同时折叠和比较所有 3 个维度以有意义的方式。

您可能会找到 this 使用文章。

(1)

"...How is he getting the pixel values of 10 for f(x,y) and 3 for g(x,y) from the RGB values for each pixel?"

这些似乎只是基本的示例值,可以更轻松地说明他的观点。您仍然可以在计算中使用完整的 RGB 值(例如: 16777215 用于全白像素)。

另请参阅此答案:How does sum of squared difference algorithm work?

(2)
相反,如果您更喜欢 255 作为白色像素的平面值,请参见下文:

"RGB are not a single value... I have a triplet of information: I have the red, green, and blue color values. How does one go from this RGB value to a single value as the example provides?"

将您的 input_rgb 值分解为 R-G-B 分量..

temp_R = input_rgb >> 16 & 0x0ff; in
temp_G = input_rgb >> 8 & 0x0ff;
temp_B = input_rgb >> 0 & 0x0ff;

然后得到一个单一的代表值(像素的亮度)

value = (temp_R + temp_G + temp_B) / 3 ); //# averaged, gives range between 0 and 255

您可以通过 (value / 255)

进一步将结果 value 缩小到 0 到 1 之间的范围内