average image intensity problem
1 次查看(过去 30 天)
显示 更早的评论
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));
0 个评论
采纳的回答
DGM
2022-1-11
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 个评论
DGM
2022-1-11
Oh. You can omit the round() operation, since the result is unit-scale floating point.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Basic Display 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!