MATLAB: Fiding mean and variance of sample of gray scale images

image processingmean

Hi,
I am trying to find the mean and variance of 64 samples of images between each same pixel location of my image database. Each image is 192×164 resolution which I call from a directory to read in a cell variable 'data'. Then using for loop, I read and conver each image into column vector, double and store in variable 'M2' so it's dimension is 32256×64. Each column consist of pixel of each image sample. Now when I try to find mean along rows by applying mean2=mean(M2,2); matlab is giving this error Index in position 1 is invalid. Array indices must be positive integers or logical values.
A = dir('Input Folder');
TotalFiles = length(A);
data = cell(1, TotalFiles);
for k = 1:TotalFiles
data{k} = imread(A(k).name);
end
% first=cell2mat(data([1]));
% [row col]=size(first);
% M=double(zeros(Size(1),Size(2)));
% M1=M(:); Size=size(first);
for i=1:TotalFiles
M=double(cell2mat(data(i)));
% M1(1:row*col,1)=M(:)+M1;
M2(:,i)=M(:);
end
% mean=(1/TotalFiles)*M1;
% mean1=reshape(mean,[192,168]);
mean2= mean(M2,2);
I don't understand why it is giving this error. Same way if I tried to find variance of this matrix, var2=var(M2,2), it gives same error. Kindly someone help me what is that I am doing wrong. I am using doule class for integers, Thanks

Best Answer

Hmmm.. Do I tell you how to fix messed up code, or just tell you to not use messed us code in the first place? I think I'll do the latter. Don't use cells - no need for that complication. If you have enough memory and want to find the mean and standard deviation image, just read them in as slices in a 3-D array.
By the way, mean2 is a built-in function so you won't want to assign anything to mean2 like you did.
Something like (untested)
TotalFiles = length(A);
array3d = double(192, 164, TotalFiles);
for k = 1:TotalFiles
fullFileName = fullfile(pwd, A(k).name);
array3d(:, :, k) = imread(fullFileName); % The image had better be grayscale!
end
meanImage = mean(array3d, [], 3);
stdImage = std(array3D, 0, 3)