average image intensity problem

2 views (last 30 days)
chihyu
chihyu on 11 Jan 2022
Commented: chihyu on 11 Jan 2022
Hello, I want to average the intensity of 10 images, but the result whole image is white.
clc;clear all; close all;
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
imageArray{k} = imread(fullFileName);
imshow(imageArray{k}); % Display image.
drawnow; % Force display to update immediately.
end
[Resolution_Y, Resolution_X] = size(imageArray{1});
ReferenceImag = zeros(k,Resolution_Y,Resolution_X);
ReferenceImag(1,:,:)= double( imageArray{1} );
for j=2:k
%ReferenceImag(1,:,:)= double( imageArray{1} );
ReferenceImag(j,:,:)= squeeze(ReferenceImag(j-1,:,:))+ double( imageArray{j} );
end
ReferenceImag = ReferenceImag./k;
ReferenceImag = round(ReferenceImag,0);
newReferenceImag = squeeze(ReferenceImag(k,:,:));
imshow(mat2cell(newReferenceImag));

Accepted Answer

DGM
DGM on 11 Jan 2022
You're converting the images to floating point using double(). Use im2double().
Tools like imshow() or imwrite() expect data to be scaled according to class-dependent limits. For integer classes, that's the minimum and maximum representable values. For floating-point classes, that's 0 and 1. Using tools like uint8() or double() only casts the data, but it doesn't rescale it accordingly.
In this case, the images are probably all uint8, so the average image is scaled 0-255 where 1 is white. Add to this problem that there's no assurance that the images are all the same class. Using im2double ensures that the images are converted to a common scale and that the final average image is correctly scaled for its class.
  3 Comments
DGM
DGM on 11 Jan 2022
Oh. You can omit the round() operation, since the result is unit-scale floating point.
chihyu
chihyu on 11 Jan 2022
thank you very much

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!