Find intersection points between a line and a boundary

I am trying to find the intersection points between a line and a boundary, how can I do it?
Code:
clc;
clear;
close all;
url='https://cdn.pixabay.com/photo/2012/04/01/17/05/brazil-23548_960_720.png';
I = imread(url);
I = rgb2gray(I);
[yDim,xDim] = size(I);
if yDim>xDim
lineLength = yDim;
else
lineLength = xDim;
end
imshow(I);
hold on;
axis on;
%Boundary
BW = imbinarize(I);
[B,L] = bwboundaries(BW,'noholes');
k=1;
stat = regionprops(I,'Centroid');
b = B{k};
c = stat(k).Centroid;
yBoundary = b(:,2);
xBoundary = b(:,1);
centroidy = c(:,2);
centroidx = c(:,1);
plot(yBoundary, xBoundary, 'g', 'linewidth', 2);
angle = 270;
xLine(1) = centroidx;
yLine(1) = centroidy;
xLine(2) = xLine(1) + lineLength * cosd(angle);
yLine(2) = yLine(1) - lineLength * sind(angle);
plot(xLine, yLine);

Respuestas (1)

KSSV
KSSV el 19 de Jul. de 2020

6 comentarios

I added this line :
P = InterX([yBoundary ;xBoundary], [xLine; yLine])
and I got this error:
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
Error in InterX (line 63)
C1 = feval(hF,D(bsxfun(@times,dx1,y2)-bsxfun(@times,dy1,x2),S1),0);
Error in plotAngleLineOverOutlineTemp (line 42)
P = InterX([yBoundary ;xBoundary], [xLine; yLine])
What did I do wrong?
KSSV
KSSV el 19 de Jul. de 2020
Check the inputs to InterX....they should be of size 2*m. Read about the function examples given in the function.
So it:
P = InterX([rot90(yBoundary) ;rot90(xBoundary)], [(xLine); (yLine)])
Thanks
Why rot90? You need to make the array column major. If x and y are your corodinates of a curve/ line.
If x and y are row vectors.
L1 = [x ; y] ;
If x and y are column vectors.
L1 = [x y]' ;
Ali razi
Ali razi el 19 de Jul. de 2020
The first thing that popped in my mind. The results are the same. Any issue why not use rot90?
KSSV
KSSV el 19 de Jul. de 2020
It just needs a transpose......so we transpse using '.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2020a

Preguntada:

el 19 de Jul. de 2020

Comentada:

el 19 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by