Pixel means from a batch of images

1 view (last 30 days)
Hi,
Can someone tell me how I would go about getting the mean of the same pixel in a batch of 204 images? As it is now I can get the mean of the whole image(s), but I would like to choose a certain pixel location and have it go through the batch and get the mean for all of those pixels of the same location. Any help will be greatly appreciated.
Thanks

Accepted Answer

Image Analyst
Image Analyst on 6 Mar 2012
Sum up the images, and divide by the number of images. This will be another image and you can get the value at any pixel simply by indexing into that average image. Here's a snippet of code from a button callback that sums up the images that were selected in a listbox.
% Get a list of all the filenames.
ListOfImageNames = get(handles.lstImageList, 'string');
% Get a list of what files they selected.
selectedItems = get(handles.lstImageList, 'value');
% If none or only 1 are selected, use them all.
numberOfSelectedImages = length(selectedItems);
if numberOfSelectedImages <= 1
numberOfSelectedImages = length(ListOfImageNames);
selectedItems = 1 : numberOfSelectedImages;
end
% Get a list of the selected files only.
% Warning: This will not include folders so we will have to prepend the folder.
ListOfImageNames = ListOfImageNames(selectedItems);
caption = sprintf('Please wait...Constructing sum of %d images...', numberOfSelectedImages);
title(caption, 'FontSize', 20);
set(handles.txtInfo, 'string', caption);
drawnow;
for k = 1 : numberOfSelectedImages % Loop though all selected indexes.
% Get the filename for this selected index.
baseImageFileName = cell2mat(ListOfImageNames(k));
imageFullFileName = fullfile(handles.ImageFolder, baseImageFileName);
% Read and display the image.
imageArray = imread(imageFullFileName);
axes(handles.axesImage);
% Clear out the axes, otherwise it gets slower and slower each time you run it.
axes(handles.axesImage);
cla reset;
imshow(imageArray);
% Replace underlines with "backslash underline" otherwise the character following the underline will turn into a subscript.
caption = strrep(caption, '_', '\_');
title(caption, 'FontSize', 20);
set(handles.txtInfo, 'string', caption);
caption = sprintf('Displaying single image\n%s', baseImageFileName);
drawnow;
if k == 1
sumImage = int32(imageArray);
else
sumImage = sumImage + int32(imageArray);
end
end
averageImage = uint8(sumImage / numberOfSelectedImages);
% Display the final summed image.
axes(handles.axesImage);
imshow(averageImage, []);
axis on; % Show tick marks
caption = sprintf('Displaying average of %d images.', numberOfSelectedImages);
title(caption, 'FontSize', 20);
  2 Comments
Ross
Ross on 7 Mar 2012
Thank you. That was helpful. As a follow on to that do you know how I could take the std of a pixel throughout the whole batch?
Thanks
Image Analyst
Image Analyst on 7 Mar 2012
Same concept. You know the formulas for variance <(x-xbar)^2> or <x2> - xbar^2 so you just have to also have a sum2image that is the sum of the square of the image, which is int32(imageArray).^2 Then do the math. Having two loops where you calculate the mean, xBar, first and then subtract it from the value and then squaring it is more accurate for large images for reasons that you probably learned in your first college course on numerical analysis or linear algebra.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!