How to find variance of several images in MATLAB

5 views (last 30 days)
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

Guillaume
Guillaume on 2 Oct 2016
The best way for you to check if your code is working is to try it on some simple images (e.g. an image made of just 127, another made of just 0 and another made of just 255), calculate the variance manually and see if your code produces the same result.
Assuming all images are all grayscale or all rgb (otherwise the cat call will fall), your code looks correct to me so I assume it does produce the correct result, but nothing beats testing.
The only oddity is the call to imlincomb which is not necessary and actually does nothing since it is equivalent to
Linear_comb_im = 1 * all_images;

More Answers (1)

Image Analyst
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');

Community Treasure Hunt

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

Start Hunting!