From e37057a123e537ccdf26122054dd5b74a1c28f29 Mon Sep 17 00:00:00 2001 From: Qianqian Fang Date: Wed, 18 Dec 2024 18:01:37 -0500 Subject: [PATCH] [feat] allow fillholes3d to perform floodfill --- fillholes3d.m | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/fillholes3d.m b/fillholes3d.m index 53a1af1..6b1fc7a 100644 --- a/fillholes3d.m +++ b/fillholes3d.m @@ -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, % % 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 @@ -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) @@ -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; @@ -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