MATLAB: How to create a structured element that would dilate a 3d image in the z axis only

image processingImage Processing Toolboxstrel

I have a 3d binary volume, and I want to close holes in the mask only if there is a true element above and below the given pixel.
This is what I tried, but it didn't work…
mask = zeros(3,3,3);
mask(2,2,1) = 1;
mask(2,2,3) = 1;
locations = reshape(ones(1,3),1,1,3);
heights = locations;
heights(1) = -1;
heights(2) = 0;
SE = strel('arbitrary',locations,heights);
newMask = imclose(mask,e)
I was expecting that newMask(2,2,2) == 1

Best Answer

I'd use convn() and thresholding and replacement. Set up your kernel, a 3x3x3 cube with only the center column as 1's. Then convolve with convn(). Then threshold to find voxels with values of 2 or more. Then set those voxels in the original image (not the convolved one) to 1. Hint (untested):
kernel = zeros(3,3,3);
kernel(2,2,1) = 1;
kernel(2,2,3) = 1;
filteredImage = convn(double(inputImage), kernel);
% Find where vertical column is true.
pixelsToFill = filteredImage >= 2;
% Replace original input image with 1
inputImage(pixelsToFill) = 1;
Note that it is not "dilation" by the usual definition. It's actually more of a hit-miss filter.