When I try to find the mean rgb for the picture below, it give the value [230,160,168]. For my information, the value for red is [255,0,0]. Is it the background influence the value?

1 view (last 30 days)

Accepted Answer

Image Analyst
Image Analyst on 25 Nov 2017
Assuming the background is all perfect white (255,255,255) you can mask it
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get mask:
mask = bwareafilt(min(rgbImage, 3) == 255, 1);
% Get mean R, G, and B
meanR = mean(redChannel(mask))
meanG = mean(greenChannel(mask))
meanB = mean(blueChannel(mask))
  7 Comments
Steven Lord
Steven Lord on 19 Aug 2019
You say "quirky", I say "unambigous".
This is a bit before the start of my tenure at MathWorks, but min and max were part of MATLAB before N-dimensional arrays (N > 2) were. Both min and max can accept either one input, a matrix whose minimum or maximum values you want to compute, or two inputs, where each element of the output is the minimum of the corresponding elements of each input. To avoid an ambiguity with scalar expansion for this command:
min(A, 1)
if you want to compute the minimum of A along the first dimension you have to disambiguate by calling min with 3 inputs.
min(A, [], 1)
Image Analyst
Image Analyst on 19 Aug 2019
Of course, they could have used the name, value pairs like many/most other functions,
minValue = mean(A, 'Dimension', 3);
but I guess they wanted to keep legacy code working and not break it.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 25 Nov 2017
Edited: John D'Errico on 25 Nov 2017
First of all, there are MANY different subtle variations of red. If every pixel in that red shirt was all the same color, you would not see any variation in the color! It would just be a block of red, in the shape of a shirt. So the color [255 0 0] is not the only red. In fact, it is true that most of the pixels in that shirt are not [255 0 0], yet they are all colors that you would describe as red.
But yes, you took the mean of the entire image. The background is white. It messes up your mean computation, if all you wanted was the mean color of the red shirt.
You will need to find only the pixels from the shirt, and then form the mean of them. Be careful, as even that tag in the collar of the shirt is not at all red. But if you take the mean of that entire area, it will subtly impact the mean, though not by a huge amount.

Tags

Community Treasure Hunt

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

Start Hunting!