Skip to content

Commit

Permalink
[feat] allow fillholes3d to perform floodfill
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Dec 18, 2024
1 parent 11c106b commit e37057a
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions fillholes3d.m
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
function resimg = fillholes3d(img, maxgap, varargin)
%
% resimg=fillholes3d(img,maxgap)
% resimg=fillholes3d(img,maxgap,mask)
%
% close a 3D image with the speicified gap size and then fill the holes
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% img: a 3D binary image
% maxgap: maximum gap size for image closing
% img: a 2D or 3D binary image
% maxgap: if is a scalar, specify maximum gap size for image closing
% if a pair of coordinates, specify the seed position for
% floodfill
% mask: (optional) neighborhood structure element for floodfilling
%
% output:
% resimg: the image free of holes
Expand All @@ -18,18 +21,22 @@
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (nargin > 1 && maxgap)
if (nargin > 1 && numel(maxgap) == 1)
resimg = volclose(img, maxgap);
else
resimg = img;
end

% if (exist('imfill', 'file'))
% resimg = imfill(resimg, 'holes');
% return;
% end
if (exist('imfill', 'file'))
resimg = imfill(resimg, 'holes');
return
end

newimg = ones(size(resimg) + 2);
if (nargin > 1 && numel(maxgap) > 1)
newimg = zeros(size(resimg) + 2);
else
newimg = ones(size(resimg) + 2);
end

oldimg = zeros(size(newimg));
if (ndims(resimg) == 3)
Expand All @@ -40,6 +47,16 @@
newimg(2:end - 1, 2:end - 1) = 0;
end

isseeded = false;
if (nargin > 1 && numel(maxgap) > 1)
if (size(maxgap, 2) == 3)
newimg(sub2ind(size(newimg), maxgap(:, 1) + 1, maxgap(:, 2) + 1, maxgap(:, 3) + 1)) = 1;
else
newimg(sub2ind(size(newimg), maxgap(:, 1) + 1, maxgap(:, 2) + 1)) = 1;
end
isseeded = true;
end

newsum = sum(newimg(:));
oldsum = -1;

Expand All @@ -55,4 +72,8 @@
resimg = newimg(2:end - 1, 2:end - 1);
end

resimg = double(~resimg);
if (~isseeded)
resimg = double(~resimg);
else
resimg = double(resimg);
end

0 comments on commit e37057a

Please sign in to comment.