您已经计算了平均图像。但是,如果要计算标准差,则必须记住所有图像的所有图像强度。请记住,标准偏差定义为每行和列位置的图像强度与该位置的平均强度之间的平方差之和的平方根除以减去 1 的图像数量。因此,我建议您存储图像作为 4D 矩阵,其中第四维表示每个图像的颜色版本。我们还需要另一个类似的变量,但它将是一个 3D 矩阵,将存储每个图像在三维上的灰度版本。之后,我们终于可以计算出每个空间位置的标准差。你甚至可以在三维中使用std 函数,这样你甚至不需要使用平均图像,但我假设你必须自己做。
假设你不能使用std,这样的事情会起作用:
% 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);