Splitting an edge HELP!!

조회 수: 4 (최근 30일)
Francesco Pignatelli
Francesco Pignatelli 2023년 3월 11일
댓글: Francesco Pignatelli 2023년 3월 14일
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

채택된 답변

Adam Drake
Adam Drake 2023년 3월 11일
편집: Adam Drake 2023년 3월 13일
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
Adam Drake
Adam Drake 2023년 3월 13일
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 2023년 3월 13일
I mean use a spline to "smooth" the two edges once they're splitted.

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

추가 답변 (1개)

Adam Drake
Adam Drake 2023년 3월 14일
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
Francesco Pignatelli
Francesco Pignatelli 2023년 3월 14일
Hej! Of course I accept it! Thanks a ton!! :D

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

카테고리

Help CenterFile Exchange에서 Splines에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by