Sum of RGB components

14 ビュー (過去 30 日間)
Pedro Minsk
Pedro Minsk 2015 年 11 月 5 日
コメント済み: Image Analyst 2015 年 11 月 6 日
I'm trying to sum the components of RGB of a point. Like it follows in the code. For example, if I have RGB = [230 100 150],
I want to get for RGB(1) + RGB(2) + RGB(3) the value of 230 + 100 + 150 = 480. The problem is that it returns 255. I know that 255 is the maximum value in the RGB system, but how could I fix the code so I can get the right sum?
b = squeeze(a(c, d, :)); % Value in RGB of a point of a image. 'C' and 'd' are the
% coordinates of the point
if ((b(1) + b(2) + b(3)) > 725)
K = 2.00;
elseif ((b(1) > b(2)) && (674.5 < (b(1) + b(2) + b(3))) && ((b(1) + b(2) + b(3)) <= 725))
K = 2.10;

採用された回答

Stephen23
Stephen23 2015 年 11 月 5 日
編集済み: Stephen23 2015 年 11 月 5 日
Currently the numeric class is uint8, which only supports maximum value of 2^8-1 = 255. You need to convert the numeric class to single, double or uint16 (or some other integer).
What you are currently doing:
>> X = uint8([230 100 150]);
>> X(1)+X(2)+X(3)
ans = 255
Option one use sum, which converts internally to double:
>> sum(X)
ans = 480
Option two convert explicitly to uint16 or double:
>> Y = uint16(X);
>> Y(1)+Y(2)+Y(3)
ans = 480
  1 件のコメント
Pedro Minsk
Pedro Minsk 2015 年 11 月 5 日
It's worked! I used the option one.
Thank you so much.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 11 月 5 日
I get the idea that you are going to do this on all pixels in the image and then threshold. There are vectorized ways, like
% Sum R+G+B for all pixels in the image:
sumImage = sum(double(rgbImage), 3);
% Get a mask where the sum is more than 725.
brightPixelsMask = sumImage > 725
% and so on.
  3 件のコメント
Stephen23
Stephen23 2015 年 11 月 6 日
Note that sum converts to double, so there is no need to call double:
sumImage = sum(rgbImage,3);
Image Analyst
Image Analyst 2015 年 11 月 6 日
You're right. I wonder why conv() or conv2() don't do that.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConvert Image Type についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by