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

8 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Image Analyst
Image Analyst il 16 Ago 2019
Modificato: Image Analyst il 16 Ago 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 Commenti
M.S. Khan
M.S. Khan il 5 Set 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

Accedi per commentare.

Più risposte (2)

Bruno Luong
Bruno Luong il 18 Ago 2019
Modificato: Bruno Luong il 23 Ago 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 Commenti
Image Analyst
Image Analyst il 18 Set 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 il 18 Set 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

Accedi per commentare.


Bruno Luong
Bruno Luong il 17 Ago 2019
You can use imclose (image processing toolbox required)
  13 Commenti
M.S. Khan
M.S. Khan il 20 Ago 2019
Thanks dearest Image Analyst, i resolved the issue. Bundle of thanks.
M.S. Khan
M.S. Khan il 23 Ago 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by