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

조회 수: 12 (최근 30일)
M.S. Khan 2019년 8월 16일
댓글: M.S. Khan 2019년 9월 18일
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 2019년 8월 16일
편집: Image Analyst 2019년 8월 16일
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);
##### 댓글 수: 11이전 댓글 9개 표시이전 댓글 9개 숨기기
M.S. Khan 2019년 9월 5일
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.
regards
Bruno Luong 2019년 9월 5일

댓글을 달려면 로그인하십시오.

### 추가 답변 (2개)

Bruno Luong 2019년 8월 18일
편집: Bruno Luong 2019년 8월 23일
[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');
##### 댓글 수: 34이전 댓글 32개 표시이전 댓글 32개 숨기기
Image Analyst 2019년 9월 18일
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 2019년 9월 18일
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 2019년 8월 17일
You can use imclose (image processing toolbox required)
##### 댓글 수: 13이전 댓글 11개 표시이전 댓글 11개 숨기기
M.S. Khan 2019년 8월 20일
Thanks dearest Image Analyst, i resolved the issue. Bundle of thanks.
M.S. Khan 2019년 8월 23일
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.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by