MATLAB Answers

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

57 views (last 30 days)
M.S. Khan
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)
m = load('Matrix_Boundary_fills_3s_only.txt');
[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.

  0 Comments

Sign in to comment.

Accepted Answer

Image Analyst
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.
[numbers, strings, raw] = xlsread('matrix.xlsx');
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);
mask = poly2mask(x, y, rows, columns); % Create a solid mask.
% Get the perimeter only.
perimImage = bwperim(mask);
% 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);

  11 Comments

Show 8 older comments
M.S. Khan
M.S. Khan on 26 Aug 2019
Thanks for reply. Image Analyst,
i means the the x-axis and y-axis labelling and scalling.
The X-axis that you have given on x-axis as shown in the first figure --> 5 10 15 20 25
The Y-axis--> 5 10 15 20 25 30
Please guide me, how the change the size of hole?
Really very very thankful for your expert guidance.
Regards
M.S. Khan
M.S. Khan on 5 Sep 2019
Dear Image Analyst and all Mathwork expert, i want to ask two questions plz.
Question #1
can i set the axis limits by following commands as shown in above coding.?
ax.xlim = [-120:10:120]
ax.ylim = [-120:10:120]
Question #2
Can i replace the filled gaps by any other number like 3 i?
i want to different to differentiate the fillied gaps from other numbers.
Thanks in advance for all your kind cooperation.
regards

Sign in to comment.

More Answers (2)

Bruno Luong
Bruno Luong on 18 Aug 2019
Edited: Bruno Luong on 23 Aug 2019
Add the salt to suit your need
Aorg = xlsread('Y_slice.xlsx');
gapmax = 5; % adjust to your need
[m,n] = size(Aorg);
A = Aorg>0;
P=[1 2 3;
8 0 4;
7 6 5];
Apad = zeros(m+2,n+2);
Apad(2:end-1,2:end-1) = A;
B = zeros(m,n,8);
for k=1:8
[i,j] = find(P==k);
B(:,:,k) = Apad(i-2+(2:end-1),j-2+(2:end-1));
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');

  34 Comments

M.S. Khan
M.S. Khan on 17 Sep 2019
Dear Image Analyst, holes can be any size but if its filled on the boundary surface, then I want to have boundary filled as color blue, holes filled as yellow and interior filled as green.
Is it possible, as you have already filled the boundary in the thread above.
Regards for best cooperation.
Image Analyst
Image Analyst on 18 Sep 2019
If the holes can be any size, then you can simply use imfill() to make a solid blob, then use that image, plus the original image, in cat() to construct the RGB image.
I don't really know the difference between "holes" and "interior" since you said holes can be any size whatsoever from 1 pixel up to nearly the entire blob size. So I don't know what distinguishes between what would be a yellow region and a green region. Please supply a diagram and point out what differences the yellow and green regions have (since you said it's NOT their size).
M.S. Khan
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

Sign in to comment.


Bruno Luong
Bruno Luong on 17 Aug 2019
You can use imclose (image processing toolbox required)

  13 Comments

M.S. Khan
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.
Thanks for all your help in advance.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!