Splitting an edge HELP!!

5 visualizaciones (últimos 30 días)
Francesco Pignatelli
Francesco Pignatelli el 11 de Mzo. de 2023
Comentada: Francesco Pignatelli el 14 de Mzo. de 2023
Hello all,
I have the following edge extracted (in yellow) that I want to split in two different parts based on the line passing through the midpoint of each row between the two edges (black line)
Anyone knows how to do it? I attach the edge matrix (full image).
Thanks a lot!
Best
Francesco

Respuesta aceptada

Adam Drake
Adam Drake el 11 de Mzo. de 2023
Editada: Adam Drake el 13 de Mzo. de 2023
Almost certainly not the most efficient way to do this, but it works.
load edge.mat
XLim = 1:550;
YLim = 300:1015;
subBUB = BUB(YLim,XLim);
y1 = 0; y2 = 716;
x1 = 0; x2 = 425;
m = (y1 - y2)/(x1 - x2);
x = XLim;
y = m * (x - x1) + y1;
top = zeros(size(subBUB));
bottom = zeros(size(subBUB));
for r = 1:size(subBUB,1)
for c = 1:size(subBUB,2)
if r < m * (c - x1) + y1 % less-than because of y-axis inversion on imagesc plot
top(r,c) = subBUB(r,c);
else
bottom(r,c) = subBUB(r,c);
end
end
end
figure
subplot(1,3,1)
imagesc(subBUB)
title('Original')
line(x,y,'Color','black')
subplot(1,3,2)
imagesc(top)
title('Top')
line(x,y,'Color','black')
subplot(1,3,3)
imagesc(bottom)
title('Bottom')
line(x,y,'Color','black')
  3 comentarios
Adam Drake
Adam Drake el 13 de Mzo. de 2023
Reconstruct the top and bottom pieces back together or use a spline instead of a linear function to define the split?
Francesco Pignatelli
Francesco Pignatelli el 13 de Mzo. de 2023
I mean use a spline to "smooth" the two edges once they're splitted.

Iniciar sesión para comentar.

Más respuestas (1)

Adam Drake
Adam Drake el 14 de Mzo. de 2023
Your second question was an entirely different problem. I had to vectorize the image, do a spline, then recombine. Increasing stepsize will smooth your spline but reduce the number of output pixels. You could do an initial spline at a large step size and a second spline with a stepsize of 1, but I think you can handle that. This is where I say adieu.
Please Accept the Answer! Thanks!
clc, clear variables, close all
load edge.mat
XLim = 10:550;
YLim = 300:1015;
subBUB = BUB(YLim,XLim);
y1 = 0; y2 = 716;
x1 = 0; x2 = 425;
m = (y1 - y2)/(x1 - x2);
x = XLim;
y = m * (x - x1) + y1;
top = zeros(size(subBUB));
bottom = zeros(size(subBUB));
for r = 1:size(subBUB,1)
for c = 1:size(subBUB,2)
if r < m * (c - x1) + y1
top(r,c) = subBUB(r,c);
else
bottom(r,c) = subBUB(r,c);
end
end
end
figure
subplot(1,3,1)
imagesc(subBUB)
title('Original')
line(x,y,'Color','black')
subplot(1,3,2)
imagesc(top)
title('Top')
line(x,y,'Color','black')
subplot(1,3,3)
imagesc(bottom)
title('Bottom')
line(x,y,'Color','black')
% Convert img matrices to vectors
[yt,xt] = find(top);
[yb,xb] = find(bottom);
%% Spline
stepsize = 1;
xxt = min(xt):stepsize:max(xt);
xxb = min(xb):stepsize:max(xb);
% Spline function requires unique values
[xt_d,ut_idx,~] = unique(xt);
yt_d = yt(ut_idx);
[xb_d,ub_idx,~] = unique(xb);
yb_d = yb(ub_idx);
yyt = spline(xt_d,yt_d,xxt);
yyb = spline(xb_d,yb_d,xxb);
figure
subplot(1,2,1)
plot(xt,yt)
set(gca, 'YDir','reverse')
title('Top')
subplot(1,2,2)
plot(xb,yb)
set(gca, 'YDir','reverse')
title('Bottom')
figure
subplot(1,2,1)
plot(xxt,yyt)
set(gca, 'YDir','reverse')
title('Top')
subplot(1,2,2)
plot(xxb,yyb)
set(gca, 'YDir','reverse')
title('Bottom')
%% Recombine
% Preallocate array
out = zeros(size(subBUB));
% make x and y values integers (introduces a bit of error)
xt_out = round(xxt);
xb_out = round(xxb);
yt_out = round(yyt);
yb_out = round(yyb);
% Put 1 at x and y values
for i = 1:length(xt_out)
r = yt_out(i);
c = xt_out(i);
out(r,c) = 1;
end
for i = 1:length(xb_out)
r = yb_out(i);
c = xb_out(i);
out(r,c) = 1;
end
figure
imagesc(out)
title('Recombined')
line(x,y,'Color','black')
  1 comentario
Francesco Pignatelli
Francesco Pignatelli el 14 de Mzo. de 2023
Hej! Of course I accept it! Thanks a ton!! :D

Iniciar sesión para comentar.

Categorías

Más información sobre Splines en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by