Splitting an edge HELP!!
5 views (last 30 days)
Show older comments
Francesco Pignatelli
on 11 Mar 2023
Commented: Francesco Pignatelli
on 14 Mar 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
0 Comments
Accepted Answer
Adam Drake
on 11 Mar 2023
Edited: Adam Drake
on 13 Mar 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 Comments
Adam Drake
on 13 Mar 2023
Reconstruct the top and bottom pieces back together or use a spline instead of a linear function to define the split?
More Answers (1)
Adam Drake
on 14 Mar 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')
See Also
Categories
Find more on Splines in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!