MatthewS
MatthewS

Reputation: 485

Computing grayscale images' standard deviation at each pixel

The following is part of a homework problem:

Compute a matrix holding the grayscale images’ standard deviation at each pixel (i.e., X(i,j) holds the standard deviation across all the images’ gray pixel intensities at row i, column j).

I have (or believe I have) the average image in grayscale. I also have the average image in color, but do not think that is relevant to this problem. I know that standard deviation requires I go through and sum the difference between each value and the average value, but not sure how to get there.

% Matrix initialization
setsum1 = zeros(215, 300, 3, 'double');
% Loop through all the image files in one directory and store in the matrix
filelist = dir('set1\*.jpg');
for i=1:length(filelist)
imname = ['\set1\' filelist(i).name];
nextim = imread(imname);
setsum1 = setsum1 + im2double(nextim);
end
% Compute the average image in color
setsum1_rgb = setsum1./length(filelist);
% Compute the average image in grayscale
setsum1_gray = rgb2gray(setsum1_rgb);
% grayscale images’ standard deviation at each pixel
deviation_setsum1_gray = sqrt(sum(power(??? - setsum1_gray, 2)));

I am trying to figure out how to get what should go in place of ???. Suggestions in the right direction would be much appreciated.

Upvotes: 1

Views: 1767

Answers (1)

rayryeng
rayryeng

Reputation: 104484

You have already computed the average image. However, you must remember all of the image intensities over all images if you want to compute the standard deviation. Remember that the standard deviation is defined as the square root of the sum of squared differences between the image intensities per row and column location with the mean intensity at that location, divided by the number of images subtracted by 1. Therefore I would recommend you store the images as a 4D matrix where the fourth dimension represents the colour version of each image. We will also need to have another variable that is similar but it'll be a 3D matrix that will store the grayscale version of each image over the third dimension. After, we can finally compute the standard deviation of each spatial location. You can even use the std function in the third dimension so you don't even need to use the average image, but I'm assuming you have to do this yourself.

Assuming you can't use std, something like this would work:

% Loop through all the image files in one directory and store in the matrix
filelist = dir('set1\*.jpg');

% Matrix initialization
% New - Make the fourth channel as long as the total number of images
setsum1 = zeros(215, 300, 3, numel(filelist), 'double');

% New - Store the grayscale images too
% Make the third channel as long as the total number of images
setsum1_gray = zeros(215, 300, numel(filelist), 'double');

for i=1:length(filelist)
    imname = ['\set1\' filelist(i).name];
    nextim = imread(imname);
    setsum1(:,:,:,i) = im2double(nextim); % New - Store the image per channel
    setsum1_gray(:,:,i) = rgb2gray(setsum1(:,:,:,i)); % New - Grayscale convert the colour image and save it
end

% Compute the average image in grayscale and colour
% Note - I would just use mean if possible
% setsum1_gray_avg = mean(setsum1_gray, 3);
% setsum1_rgb = mean(setsum1, 4);
% ... or 
% setsum1_gray_avg = sum(setsum1_gray, 3) / numel(filelist);
% setsum1_rgb = sum(setsum1, 4) / numel(filelist);
setsum1_rgb = zeros(215, 300, 3);
setsum1_gray_avg = zeros(215, 300);
for i = 1 : numel(filelist)
    setsum1_rgb = setsum1_rgb + setsum1(:,:,:,i);
    setsum1_gray_avg = setsum1_gray_avg + setsum1_gray(:,:,i);
end
setsum1_rgb = setsum1_rgb / numel(filelist);
setsum1_gray_avg = setsum1_gray_avg / numel(filelist);

% Now compute standard deviation for each pixel
% Note - I would use std if possible
% setsum1_stddev = std(setsum1_gray, [], 3);
setsum1_stddev = zeros(215, 300);
for i = 1 : numel(filelist)
    setsum1_stddev = setsum1_stddev + (setsum1_gray(:,:,i) - setsum1_gray_avg).^2;
end
setsum1_stddev = sqrt((1 / (numel(filelist) - 1)) * setsum1_stddev);

Upvotes: 1

Related Questions