user3101751
user3101751

Reputation: 59

subtract one color from another in RGB color space

I would like to subtract color from another. For example, I have two image 100X100 pixel, one with color R:236 G:226 B:43, and another R:63 G:85 B:235. I would like to cut color R:236 G:226 B:43 from R:63 G:85 B:235. But I know it can't subtract like the mathematically method, by layer R:236-63, G:226-85, B:43-235 because i found that the color that less than 0 and more than 255 can't define.

I found another color space in RYB color space.but i don't know how it really work.

Thank you for your help.

Upvotes: 0

Views: 4745

Answers (1)

sepdek
sepdek

Reputation: 1402

You cannot actually subtract colors. But you surely can detect their difference. I suppose this is what you need, anyway.
Here are some thoughts and remarks:

  1. Convert your images to HSV colorspace which transforms RGB values to Hue, Saturation and Brightness (Value).
  2. All your images should be around a yellowish color (near 60 deg. on the Hue circle) so they should all have about the same Hue with minor differences.
  3. Typically if all images are taken at constant lighting conditions they should have the same Value (brightness).
  4. Saturation, which corresponds to the mixture of white in a color, typically represents how intense you perceive a color to be. This would typically be of about the same value for all your images in constant lighting conditions.

According to your first description, the main difference should be detected in the Hue channel.

A good thing about HSV is that H (hue) is represented by a counterclockwise circle and colors are just positions on this circle, so positive and negative values all make sense (search google for a description of HSV colorspace to get a view of how it looks and works).

You may either detect differences by a subtraction that will lead you to a value either positive either negative, or by taking the absolute value of the subtraction, which will just give a measure of the difference of the two values of Hue (but without any information on the direction of the difference). If you need the direction of the difference you should just stick to a plain subtraction.

For example:

Hue_1 - Hue_2 = Hue_3 (typically a small value for your problem)
if Hue_3 > 0 this means that Hue_1 is a bit towards Green
if Hue_3 < 0 this means that Hue_1 is a bit towards Red

Of course you may also need to take a look at the differences in the other channels, S and V to see if colors are more saturated or more bright, but I cannot be sure you need to do this since we haven't seen any images here.

Of course you can do a lot more sophisticated things...Like apply clustering or classification techniques on the detected hues and classify them to classes according to your problem needs...

Upvotes: 2

Related Questions