Getting Standard Deviation for Image based on normalized color band

I need to evaluate the SD for an imported image (PNG). The thing is the image actually represents a color band from 0 - 1 (blue - red) and the SD is calculated for the about the mean i.e SD of (conc - 0.5). How do I implement this in MATLAB? I tried using mat2gray to normalize the matrix and used std2 but the result doesn't quite match. It seems I need to normalize on the band -0.5 to 0.5 but not really sure. Suggestions?

 Accepted Answer

I tried both your suggestions but it doesn't seem to give me the right answer. I am including the image. To give more clarity to the problem, in this image, the colors represent the concentration of a species, let's say c. The blue region represents c as 0 while the red region is 1 and green is around 0.5. The software where I generated the image calculates the SD for this image at 0.1486 while your suggestions for the same don't give me the expected result.

5 Comments

You'd need to know the colormap that was used if you want to get accurate values.
This is the original image with the colormap
So what is that numerically? Is that jet(32)? If you have the image to display it, then you'll have the colormap, and you'll also have the original matrix. I'm getting the feeling that you've lifted this as-is from some article or web site. Is that right?
No. Based on a simulation I did on a CFD software, this represents the species concentration on a cross section. I need to validate the codes generated SD to one given by MATLAB. So what, you're suggesting is I generate a matrix for this image instead and then use that for the SD?
Yes. If you can export the matrix itself , rather than a pseudocolored version of it, that would of course be much much better because you'd have the actual values and there would be no need to "undo" the color map.

Sign in to comment.

More Answers (2)

std() always subtracts the mean so you do not need to shift the mean before you do std().
However, std() is sensitive to the range of values: multiplying the range by a factor of alpha multiplies the standard deviation by alpha.

2 Comments

Okay. But when I print the image and add the colorbar, the range goes from 0 to 60 ish whereas the place where I imported the image from had it from 0-1. How do I get the Matlab colorbar range to the one I need?
PNG files cannot store floating point, so you will find that the data read in is in the range 0 to 255 (or less.) im2double() will convert that to the 0 to 1 range.

Sign in to comment.

Are you saying that you have a PNG image that goes from blue to red, so that the green channel is always 0. And if the color is pure blue (red component = 0) then this represents a "0" and if the color is pure red (with a blue component = 0) then this represents a 1? So that the blue plus red components added together always give 255? If so, you can convert this image into a 0 to 1 image this way
rgbImage = imread(filename);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Create an image where blue goes to 0, red goes to 1
% and it's linearly scaled in between based on how much blue and red there is.
outputImage = double(redChannel) / 255;
% Now get the standard deviation
stDev = std(outputImage);

Community Treasure Hunt

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

Start Hunting!