How can I perform arithmetic operations with RGB values?

1 view (last 30 days)
I have an image, and I want to get values of a parameter for each pixel of the image. The parameter is a function of the RGB values of the image. In order to evaluate the parameter, I need to perform basic arithmetic operations involving RGB values, but I am not able to to that as RGB values are scaled ones. e.g., if R=243, and I want to add 30 with it, the answer becomes 255 instead of 273, as that is the maximum permissible limit for the pixel intensity values. What I understand is that I cannot treat RGB values as general numeric entities. Any help regarding converting them into numerical values so that arithmetic operations can be performed is highly appreciated.

Accepted Answer

Stephen23
Stephen23 on 24 Sep 2017
Edited: Stephen23 on 24 Sep 2017
"What I understand is that I cannot treat RGB values as general numeric entities."
Yes you can. The fact that your data are RGB is totally irrelevant. The problems you are having have nothing to do with RGB per se, just the numeric type that they happen to be stored in, which happens to be uint8 (with a maximum value of 255):
You can check the class of those RGB values by calling class:
class(X)
for your data X. Then take a look at this example, which shows that what you are observing has nothing to do with the fact that you data are RGB values:
>> X = uint8(243);
>> X+30 % uint8 has max value 255
ans =
255
>> double(X)+30 % convert to double -> much max value
ans =
273
Do you see how I fixed your "problem"? By simply converting the value to double, which is a numeric class with a much higher maximum value. Depending on your needs and the algorithm you are implementing it may be suitable to convert to another integer class.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!