Skip to content

Commit

Permalink
[feat] drop image proc toolbox dep. self-implement volgrow,shrink,clo…
Browse files Browse the repository at this point in the history
…se,open
  • Loading branch information
fangq committed Dec 18, 2024
1 parent 2f2d2c6 commit 11c106b
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 55 deletions.
4 changes: 2 additions & 2 deletions deislands2d.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@

cleanimg = zeros(size(img));
if (sum(img(:)))
img = imclose(img, strel('disk', 3));
img = volclose(img, 1, true(5, 5));
islands = bwislands(img);
end

if (length(islands))
if (~isempty(islands))
% remove small islands of the foreground
maxblock = -1;
maxblockid = -1;
Expand Down
44 changes: 33 additions & 11 deletions fillholes3d.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function resimg = fillholes3d(img, maxgap)
function resimg = fillholes3d(img, maxgap, varargin)
%
% resimg=fillholes3d(img,maxgap)
%
Expand All @@ -18,19 +18,41 @@
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

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

if (isoctavemesh)
if (~exist('bwfill'))
error('you need to install octave-image toolbox first');
end
for i = 1:size(resimg, 3)
resimg(:, :, i) = bwfill(resimg(:, :, i), 'holes');
end
% if (exist('imfill', 'file'))
% resimg = imfill(resimg, 'holes');
% return;
% end

newimg = ones(size(resimg) + 2);

oldimg = zeros(size(newimg));
if (ndims(resimg) == 3)
oldimg(2:end - 1, 2:end - 1, 2:end - 1) = resimg;
newimg(2:end - 1, 2:end - 1, 2:end - 1) = 0;
else
resimg = imfill(resimg, 'holes');
oldimg(2:end - 1, 2:end - 1) = resimg;
newimg(2:end - 1, 2:end - 1) = 0;
end

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

while (newsum ~= oldsum)
newimg = (volgrow(newimg, 1, varargin{:}) & ~(oldimg > 0));
oldsum = newsum;
newsum = sum(newimg(:));
end

if (ndims(resimg) == 3)
resimg = newimg(2:end - 1, 2:end - 1, 2:end - 1);
else
resimg = newimg(2:end - 1, 2:end - 1);
end

resimg = double(~resimg);
16 changes: 2 additions & 14 deletions thickenbinvol.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function vol = thickenbinvol(vol, layer)
function vol = thickenbinvol(varargin)
%
% vol=thickenbinvol(vol,layer)
%
Expand All @@ -19,16 +19,4 @@
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

dim = size(vol);
dxy = dim(1) * dim(2);
fulllen = prod(dim);

offs = [1, -1, dim(1), -dim(1), dxy, -dxy];
for i = 1:layer
idx = find(vol);
for j = 1:6
idxnew = idx + offs(j);
idxnew = idxnew(find(idxnew > 0 & idxnew < fulllen));
vol(idxnew) = 1;
end
end
vol = volgrow(varargin{:});
31 changes: 3 additions & 28 deletions thinbinvol.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function vol = thinbinvol(vol, layer, nobd)
function vol = thinbinvol(varargin)
%
% vol=thinbinvol(vol,layer,nobd)
% vol=thinbinvol(vol,layer,mask)
%
% thinning a binary volume by a given pixel width
% this is similar to bwmorph(vol,'thin',n) except
Expand All @@ -21,29 +21,4 @@
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

dim = size(vol);
dxy = dim(1) * dim(2);
fulllen = prod(dim);

if (nargin < 3)
nobd = 0;
end

if (nobd == 1)
bdmask = vol;
if (ndims(vol) == 2)
bdmask(2:end - 1, 2:end - 1) = 0;
elseif (ndims(vol) == 3)
bdmask(2:end - 1, 2:end - 1, 2:end - 1) = 0;
end
end

for i = 1:layer
idx = find(~vol);
idxnew = [idx + 1; idx - 1; idx + dim(1); idx - dim(1); idx + dxy; idx - dxy];
idxnew = idxnew(find(idxnew > 0 & idxnew < fulllen));
vol(idxnew) = 0;
if (nobd)
vol = vol | bdmask;
end
end
vol = volshrink(varargin{:});
26 changes: 26 additions & 0 deletions volclose.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function newvol = volclose(varargin)
%
% vol = volclose(vol, layer, mask)
%
% close a binary volume by applying layer-counter of growth operation
% followed by layer-count of shrinkage operation
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% vol: a volumetric binary image
% layer: number of iterations for the thickenining
% mask: (optional) a 3D neighborhood mask
%
% output:
% vol: the volume image after closing
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (isempty(varargin))
error('must provide a volume');
end

newvol = volgrow(varargin{:});
newvol = volshrink(newvol, varargin{2:end});
43 changes: 43 additions & 0 deletions volgrow.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function newvol = volgrow(vol, layer, mask)
%
% vol = volgrow(vol, layer, mask)
%
% thickening a binary image or volume by a given pixel width
% this is similar to bwmorph(vol,'thicken',3) except
% this does it in both 2d and 3d
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% vol: a volumetric binary image
% layer: number of iterations for the thickenining
% mask: (optional) a 3D neighborhood mask
%
% output:
% vol: the volume image after the thickening
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (nargin < 2)
layer = 1;
end

if (nargin < 3)
if (ndims(vol) == 3)
mask = zeros(3, 3, 3);
mask(2, 2, :) = 1;
mask(:, 2, 2) = 1;
mask(2, :, 2) = 1;
else
mask = [0 1 0; 1 1 1; 0 1 0];
end
end

newvol = vol;

for i = 1:layer
newvol = (convn(newvol, mask, 'same') > 0);
end

newvol = double(newvol);
26 changes: 26 additions & 0 deletions volopen.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function newvol = volopen(varargin)
%
% vol = volopen(vol, layer, mask)
%
% open a binary volume by applying layer-counter of shrink operation
% followed by layer-count of growth operation
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% vol: a volumetric binary image
% layer: number of iterations for the thickenining
% mask: (optional) a 3D neighborhood mask
%
% output:
% vol: the volume image after opening
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (isempty(varargin))
error('must provide a volume');
end

newvol = volshrink(varargin{:});
newvol = volgrow(newvol, varargin{2:end});
44 changes: 44 additions & 0 deletions volshrink.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function newvol = volshrink(vol, layer, mask)
%
% vol = volshrink(vol, layer, mask)
%
% thinning a binary image or volume by a given pixel width
% this is similar to bwmorph(vol,'thin',3) except
% this does it in both 2d and 3d
%
% author: Qianqian Fang, <q.fang at neu.edu>
%
% input:
% vol: a volumetric binary image
% layer: number of iterations for the thickenining
% mask: (optional) a 3D neighborhood mask
%
% output:
% vol: the volume image after the thinning operations
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%

if (nargin < 2)
layer = 1;
end

if (nargin < 3)
if (ndims(vol) == 3)
mask = zeros(3, 3, 3);
mask(2, 2, :) = 1;
mask(:, 2, 2) = 1;
mask(2, :, 2) = 1;
else
mask = [0 1 0; 1 1 1; 0 1 0];
end
end

totalmask = sum(mask(:));
newvol = vol;

for i = 1:layer
newvol = (convn(logical(newvol), logical(mask), 'same') == totalmask);
end

newvol = double(newvol);

0 comments on commit 11c106b

Please sign in to comment.