Find the intersection between object boundaries and a line

I have a binary mask and a line that passes through the mask as shown in the attached figure. I want to find the intersection point between the mask and the line (the red plus point in the figure).
Can you please help me with this?
I have a binary mask and a line that passes through the mask as shown in the attached figure. I want to find the intersection point between the mask and the line (the red plus point in the figure).
Can you please help me with this?
This is the code to generate the output image:
mask = imread(mask.png);
bw = bwareafilt(im2bw(mask));
s = regionprops(bw, 'Centroid', 'Extrema');
corners = s.Extrema;
Pt1dx = (corners(4,1) + corners(5,1))/2;
Pt1dy = (corners(4,2) + corners(5,2))/2;
Pt2dx = s.Centroid(1);
Pt2dy = s.Centroid(2);
figure;
imshow(bw);
hold on;
plot(Pt1dx, Pt1dy, 'g+');
slope = (Pt1dy-Pt2dy)/(Pt1dx-Pt2dx);
Targetdx = -75.590551181 * cos(slope) + Pt1dx;
Targetdy = -75.590551181 * sin(slope) + Pt1dy;
hold on;
plot(Targetdx, Targetdy, 'b*');
hold on;
line([Pt1dx, Pt1dy], [Targetdx, Targetdy])
hold on;
slopeInv = -1/slope;
xLine = 1:size(bw,2);
yLine = slopeInv.*(xLine - Targetdx) + Targetdy;
plot(xLine, yLine, 'b'); %# Plot the perpendicular line

댓글 수: 2

The output image.
Yes, but what about the input image mask.png. You forgot to attach that.

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

 채택된 답변

Ghada Alzamzmi
Ghada Alzamzmi 2020년 9월 2일

0 개 추천

I was able to find the points using the script below:
https://www.mathworks.com/matlabcentral/fileexchange/11837-fast-and-robust-curve-intersections
Thank you all for your responses.

추가 답변 (1개)

Matt J
Matt J 2020년 8월 14일
편집: Matt J 2020년 8월 14일
B=cell2mat(bwboundaries(bw,8,'noholes'));
[x,y]=deal(B(:,2),size(bw,1)+1-B(:,1));
[d,loc]=min( abs( slopeInv.*(x - Targetdx) + Targetdy -y) );
intersection = [x(loc), y(loc)]

댓글 수: 2

Thanks a lot Matt for your quick response!
I followed your steps and I got the point of the other side. I want the second intersection point.
Please see the attached image.
This is the code:
B = cell2mat(bwboundaries(bw,8,'noholes'));
[x,y]=deal(B(:,2),size(bw,1)+1-B(:,1));
[d,loc]=min( abs( slopeInv.*(x - Targetdx) + Targetdy -y) );
intersection1 = [x(loc), y(loc)];
hold on
plot(intersection1(1), intersection1(2), 'g+');
Matt J
Matt J 2020년 8월 15일
편집: Matt J 2020년 8월 15일
I don't know what general criterion you use to define the "correct side". This version looks for the intersection with the largest y-coordinate:
B = cell2mat(bwboundaries(bw,8,'noholes'));
[x,y]=deal(B(:,2),size(bw,1)+1-B(:,1));
[a,b,c]=deal(slopeInv,-1, Targetdy-slopeInv.*Targetdx);
loc = abs( (a*x+b*y+c)/norm([a,b]) ) <= sqrt(2)*1.0001;
[~,ymax]=max( y(loc) );
intersection = [x(loc(ymax)), y(loc(ymax))];
hold on
plot(intersection1(1), intersection1(2), 'g+');

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

카테고리

도움말 센터File Exchange에서 Display Image에 대해 자세히 알아보기

질문:

2020년 8월 14일

답변:

2020년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by