Fiding mean and variance of sample of gray scale images

4 views (last 30 days)
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 192x164 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 32256x64. 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
  1 Comment
Salman Imtiaz
Salman Imtiaz on 13 Feb 2019
For mean, I am expecting mean2 matrix to be 32256x1 with each row location showing mean of 64 samples accross the data.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 13 Feb 2019
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)
  2 Comments
Salman Imtiaz
Salman Imtiaz on 13 Feb 2019
Hi Image Analyst,
Thanks for the response, it is fixed. Yes images are grayscale and using cell as I need to show it. I can use array also as you mentioned. Also I didn't knew mean2 is built in function, did not find it on mathworks.
I would like to know if I use meanImage=mean(array3d,2), will it work same? what does "mean" syntax in your code signifies, array3d is variable with stored images, [ ] (all rows and columns?, and 3?
Thanks
Walter Roberson
Walter Roberson on 13 Feb 2019
mean2 is mean over the entire array . That is not the same as mean with dimension 2 which calculates the mean per row.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 13 Feb 2019
you accidentally defined a variable named mean and it is still in your workspace when you try to use mean as a function .
  1 Comment
Salman Imtiaz
Salman Imtiaz on 13 Feb 2019
yes! I cleared my workspace before running the code and commented mean veriable. I knew I messed up using mean as variable when it is function. Thanks

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!