# How to close the boundary of a surface already generated by filling the holes

22 views (last 30 days)
M.S. Khan on 16 Aug 2019
Commented: M.S. Khan on 18 Sep 2019
from give matrix (attached), i am using following code to close the surface and to make a boundary but i am getting errors that indexing is out of bound.
m = zeros(28)
[rows, colms] = size(m);
mOut = m;
for row = 2 : rows
%
for colm = 1:colms
if m(row-1, colm) == 3
mOut(row, colm+1) = 3;
end
if m(row-1, colm) == 3 || m(row-1,colm+2) == 3
mOut(row, colm+1) = 3;
end
if m(row-1, colm + 1) == 3
mOut(row, colm+1) = 3;
end
end
end
% end
disp(mOut)
thanks for all cooperation in advance.

Image Analyst on 16 Aug 2019
Edited: Image Analyst on 16 Aug 2019
Is the data (circle) expected to be convex? If so the ultra-easy way is to just use bwconvhull() followed by bwboundaries(), poly2mask(), and bwperim() like the code below. If not, you'll have to follow my answer to your other question where I tell you how to connect each endpoint to the closest other endpoint.
% Get and display original data.
m = uint8(numbers);
subplot(2, 2, 1);
imshow(m, [], 'InitialMagnification', 800);
axis('on', 'image');
title('Original Data', 'FontSize', 20);
% Get convex hull.
chImage = bwconvhull(m);
subplot(2, 2, 2);
imshow(chImage, [], 'InitialMagnification', 800);
axis('on', 'image');
boundary = bwboundaries(chImage);
boundary = boundary{1}; % Extract the outermost boundary from the cell.
x = boundary(:, 2);
y = boundary(:, 1);
title('Convex Hull', 'FontSize', 20);
% Show results.
subplot(2, 2, 3);
imshow(m, [], 'InitialMagnification', 800);
axis('on', 'image');
hold on;
plot(x, y, 'r.-', 'LineWidth', 2, 'MarkerSize', 15);
title('Original Data with Boundary Overlaid', 'FontSize', 20);
% Get the perimeter
[rows, columns] = size(m);
% Get the perimeter only.
% Show results.
subplot(2, 2, 4);
imshow(perimImage, [], 'InitialMagnification', 800);
axis('on', 'image');
title('New, Convex Perimeter', 'FontSize', 20);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
If you don't want the x,y coordinates and only want the matrix, you can skip calling bwboundaries() and just call bwperim() directly on the convex hull image.
perimImage = bwperim(chImage);
Bruno Luong on 5 Sep 2019

Bruno Luong on 18 Aug 2019
Edited: Bruno Luong on 23 Aug 2019
[m,n] = size(Aorg);
A = Aorg>0;
P=[1 2 3;
8 0 4;
7 6 5];
B = zeros(m,n,8);
for k=1:8
[i,j] = find(P==k);
end
As = A & sum(diff(B(:,:,[1:8 1]),1,3)==1,3)<=1;
[y,x] = find(As);
p = length(x);
xy = [x,y];
xy1 = reshape(xy,1,[],2);
xy2 = reshape(xy,[],1,2);
d = sum(abs(xy1-xy2),3);
d(d==0) = NaN;
i = (1:size(d,1))';
[dmin,j] = min(d,[],2);
keep = dmin <= gapmax;
keep = keep & j<i;
i = i(keep);
j = j(keep);
for k=1:length(i)
xyi = xy(i(k),:);
xyj = xy(j(k),:);
dxy = xyi-xyj;
if abs(dxy(1)) > abs(dxy(2))
xr = linspace(xyi(1),xyj(1),abs(dxy(1))+1);
yr = round(interp1([xyi(1),xyj(1)],[xyi(2),xyj(2)],xr));
else
yr = linspace(xyi(2),xyj(2),abs(dxy(2))+1);
xr = round(interp1([xyi(2),xyj(2)],[xyi(1),xyj(1)],yr));
end
A(sub2ind(size(A),yr,xr)) = 1;
end
close all
subplot(1,2,1);
imagesc(Aorg);
title('original');
subplot(1,2,2);
imagesc(A);
title('filling gap');
M.S. Khan on 18 Sep 2019
yes i am using imfill() functions but i want then to differentiate between boundary and interor.
For example interior is filled as 2s and boundary as 1s or 3s.
i want the interior as filled with 2s to be different color.
Boundary filled with1s and 3s as a different color
Exterior can be any other different color.
Thanks for cooperation

Bruno Luong on 17 Aug 2019
You can use imclose (image processing toolbox required)
M.S. Khan on 23 Aug 2019
Hi Image Analyst and all community members,
thanks for all support and professional cooperation.
May i ask you two questions
Q1: in the 3rd pic, there are 2 whits dots. how can we can we remove it
Q2: in the X-axis & Y -axis, how can we change the scalling of our own choice.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by