简体   繁体   中英

Calculating a single representative “pixel value” from a RGB value

My question is related to this stack overflow discussion here.

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

The discussion gives us the formula for the sum of squared differences and an answer gives the following example.


For example, if you are comparing two pixels (ie one pixel in each image), you have a region of 1 pixel. Let's say it is the fifth pixel in the first row: x = 0, y = 4. The pixel values are 10,3 for f,g respectively. For the region of one 2n1=1=>n1=0, and the same goes for 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


My question is 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 are not a single value. That is when I have a single pixel of an image 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?

I presume he's using a grayscale color representation, where the values 10 and 3 correspond the to brightness, or luminosity, of the two pixels.

There is a method to convert RGB to an integer representation:

RGB (int) = 

R * (256^2) + 

G * (256) + 

B

... However , calculating SSD between values in this representation will be meaningless, which brings us to the question of calculating colour distance in general: any attempt to flatten RGB into a single dimension will inevitably collapse some of the information.

Representing your RGB values as HSL/HSB/HSV may enable you to compare hues, or saturation, or brightness more meaningfully (one dimension at a time), but it is hard to collapse and compare all 3 dimensions at once in a meaningful manner.

You may find this article of use.

(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?"

These seem like just basic example values to make it easier to illustrate his point. You could still use the full RGB value in calculations ( eg: 16777215 for all-white pixel).

See also this answer: How does sum of squared difference algorithm work?

(2)
If instead you prefer 255 as flat value for a white pixel see below:

"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?"

Take your input_rgb value and break it up to RGB components..

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

Then to get a single representative value (pixel's luminance)

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

You can further reduce the result value into range of between 0 and 1 by doing a (value / 255)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM