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

 采纳的回答

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 个)

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+');
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 中查找有关 Author Block Masks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by