How to find variance of several images in MATLAB
Show older comments
Hi everyone. I am relatively new to MATLAB and Image processing, so understand with me. I am working in experiment on image processing, and my current stage, I need to 1) read some images (about 100 images of same dimension) 2) store them in a variable (either cell array, vector or structure) 3) Find the variance of each pixel in each image 4) Form a new matrix to store each computed variance
Here is my code, but I am not sure it solves this problem not withstanding that I get result
clc;
im_File = dir('*.bmp');
files = {im_File.name};
for k=1:numel(files)
im{k} = imread(files{k});
%# Get the number of dimensions for your arrays
dim = ndims(im{k});
all_images = cat(dim+1,im{:});
% Use linear combine to acquire all the images
Linear_comb_im = imlincomb(1,all_images,'uin');
%get the variance of all images
computed_variance = var(double(Linear_comb_im),1,dim+1);
end
Accepted Answer
More Answers (1)
Image Analyst
on 2 Oct 2016
Try this. It computes the variance of each pixel after stacking all the images into layers along the third dimension. Of course the variance will be an image also.
clc;
folder = fileparts(which('cameraman.tif')); % Determine where demo folder is (works with all versions).
filePattern = fullfile(folder, '*.jpg');
im_File = dir(filePattern);
fileNames = {im_File.name};
for k = 1 : length(fileNames)
thisImage = imread(fileNames{k});
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(thisImage);
% Convert to gray scale if needed.
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
thisImage = thisImage(:, :, 2); % Take green channel.
end
imshow(thisImage, []);
title(fileNames{k}, 'Interpreter', 'none');
drawnow;
% Get size of first image.
if k == 1
rows1 = rows;
columns1 = columns;
all_images = thisImage;
end
% Resize to match first image, if needed
if rows ~= rows1 || columns ~= columns1
thisImage = imresize(thisImage, [rows1, columns1]);
end
% Add as a layer in the third dimension.
all_images = cat(3, all_images, thisImage);
end
% Get the variance of all images along the third dimension
computed_variance = var(double(all_images), 0, 3);
figure
imshow(computed_variance, []);
axis on;
title('computed_variance', 'Interpreter', 'none');
Categories
Find more on Image Arithmetic 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!