Using imfill on multiple images

조회 수: 2 (최근 30일)
Ahmed Ismail
Ahmed Ismail 2016년 6월 8일
댓글: DGM 2025년 7월 30일
myPath= 'E:\conduit_stl(smooth contour)\Collagen Contour Slices\';
fileNames = dir(fullfile(myPath, '*.tif'));
C = cell(length(fileNames), 1);
for k = 1:length(fileNames)
filename = fileNames(k).name;
C{k} = im2bw(imread(filename));
filled = imfill(C,'holes');
end
I have this code to import multiple images from a folder into MATLAB. Though now I would like to perform a fill on all the images. However, when attempting this, an error stating: "Expected input number 1, I1 or BW1, to be one of these types: double, ... etc" I tried converting the images into double precision, though that just resulted in: "Conversion to double from cell is not possible."
This is most likely due to the structure type, the images are 'housed' in, but I have no idea concerning that.
Help on this would be greatly appreciated.

채택된 답변

Ahmed Ismail
Ahmed Ismail 2016년 6월 9일
C(256,256,length(fileNames)) = uint8(0);
C_filled = C;
se = strel('disk', 2, 0);
for k = 1:length(fileNames)
C(:,:,k) = im2bw(imread(fileNames(k).name));
C_filled(:,:,k) = imfill(imclose(C(:,:,k), se),'holes');
end
  댓글 수: 1
DGM
DGM 2025년 7월 30일
Assuming files do not have a consistent page geometry:
myPath = 'sources/stackedletters'; % the base path is not pwd
D = dir(fullfile(myPath,'*.png')); % is is not just a list of names
% preallocate both input and output
C = cell(numel(D),1); % avoid using length() unless you know what it does
C_filled = cell(numel(D),1);
% we need this, but it doesn't need to be in the loop
se = strel('disk', 2, 0);
% process the files one at a time
for k = 1:numel(D)
% make sure you use the full path since files aren't in pwd
thisfname = fullfile(D(k).folder,D(k).name);
inpict = imread(thisfname); % read the file
C{k} = im2bw(inpict); % or use imbinarize() in modern versions
C_filled{k} = imfill(imclose(C{k},se),'holes');
end
Assuming that we know for certain that they all have the exact same page geometry.
myPath = 'sources/stackedletters'; % the base path is not pwd
D = dir(fullfile(myPath,'*.png')); % is is not just a list of names
% preallocate both input and output
sz = [256 256]; % assuming we know
C = false([sz numel(D)]); % avoid using length() unless you know what it does
C_filled = false([sz numel(D)]); % preallocate for the correct class!
% we need this, but it doesn't need to be in the loop
se = strel('disk', 2, 0);
% process the files one at a time
for k = 1:numel(D)
% make sure you use the full path since files aren't in pwd
thisfname = fullfile(D(k).folder,D(k).name);
inpict = imread(thisfname); % read the file
C(:,:,k) = im2bw(inpict); % or use imbinarize() in modern versions
C_filled(:,:,k) = imfill(imclose(C(:,:,k),se),'holes');
end
The output class of im2bw() is not uint8. It's logical. Keep images such that their class and scale are appropriate for each other. A [0 1] scale uint8 image is a black image with 1LSB worth of noise.
In the first example, we're using a cell array, so we don't need to worry about sizes matching, and we're not at risk of imposing an inappropriate class on the images.
There are plenty of times were we do know that a handful of images are a certain size, and we can afford to presume it. The more general we want to be, the more effort we need to make the code robust. The second example becomes the more complicated one, requiring pre-loading or querying metadata on the whole imageset prior to processing.
Using a cell array is flexible, and if in the end, if we can test or modify our images such that we know they are the same page geometry, then we can just use cell2mat() to create a monolithic logical volume at that point.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by