Using imfill on multiple images

2 次查看(过去 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