Alex
Alex

Reputation: 163

How to find intersections in binary image lines?

I have a binary image with curved lines as shown below, but I would like to know how I can find where they would intersect if they are extended.

So could you give me some ides on how i could:

I have thought about using hough transform to find lines, then intersection, but in some images my line endpoints are not exactly straight. Is there a way to maybe only find the direction of the line at the end of it instead of over the whole line, as it is a binary image?

Thanks for any help c a b

Upvotes: 6

Views: 6092

Answers (6)

Amro
Amro

Reputation: 124563

Here is what I came up with using Hough Transform:

%# read and binarize image
I = imread('https://i.sstatic.net/XlxmL.jpg');
BW = im2bw(rgb2gray(I));

%# hough transform, detect peaks, then get lines segments
[H T R] = hough(BW, 'Theta',-10:10);   %# specific theta range
P  = houghpeaks(H, 5);
lines = houghlines(BW, T, R, P);

%# overlay detected lines over image
figure, imshow(BW), hold on
for k = 1:length(lines)
    xy = [lines(k).point1; lines(k).point2];
    plot(xy(:,1), xy(:,2), 'g.-', 'LineWidth',2);
end

%# find endpoints (intersections) and show them
xy = [vertcat(lines.point1);vertcat(lines.point2)];
[~,idx1] = min(xy(:,2));
[~,idx2] = max(xy(:,2));
xy = xy([idx1;idx2],:);     %# intersection points
plot(xy(:,1),xy(:,2), 'ro', 'LineWidth',3, 'MarkerSize',12)
hold off

screenshot

Upvotes: 2

Hovhannes Sargsyan
Hovhannes Sargsyan

Reputation: 19

fill contours into std::vector<std::vector<cv::Point> > , using findContours function from OpenCV library, then for any two contours which don't intersect ( case of intersection i'll explane later) do the following: first contour is the sequence of 2D points A1 A2 .... An and second contour is B1, B2, .., Bm, fix some i > 0 && i < n , j >0 && j < m and do extrapolation using (A1, ..., Ai) for finding extend from first endpoint of first contour than extrapolate (An-i, ... ,An) for finding extendion of the first contour from second endpoint: do this similarly for second contour (B1, ... ,Bj) &&(Bm-j, ... , Bm) : now you can extend your contours till borders of image and check are their intersect or not. i hope you know how to find intersection of the segments in 2D space. You must use this for all [Ai Ai+1] and [Bj Bj+1] i = 1,... ,n-1 && j = 1,...,m-1

Upvotes: 0

Dr. belisarius
Dr. belisarius

Reputation: 61016

Applying a dilation and then an erosion will extend your endpoints like this:

(*Code in Mathematica*)
Erosion[Dilation[i, 10], 10]

enter image description here

A full solution could be something like this:

r = Dilation[MorphologicalBranchPoints[
   Thinning[Binarize@Erosion[Dilation[i, 10], 10], 10]] // Colorize, 3]
ImageAdd[i, r]

enter image description here

Upvotes: 4

Gary Tsui
Gary Tsui

Reputation: 1755

Simply looking at your lines, they are more or less straight lines (not concave/convex curves) In my humble opinion, there's an easier way and more obvious way, since, you know either end points of the three lines. You can always get the intersection by solving x and y respectively.

http://zonalandeducation.com/mmts/intersections/intersectionOfTwoLines1/intersectionOfTwoLines1.html

gd luck

Upvotes: 1

Grega Kešpret
Grega Kešpret

Reputation: 12107

I think you should take a look at Hough transform. It calculates lines equations from binary reprentation (usually output of edge detector). Once you have this, it is a piece of cake to calculate intersections.

Upvotes: 4

ypnos
ypnos

Reputation: 52317

You could try to fit three corresponding curves and then solve the equation for the two intersections explicitely.

There exist some established models for curve fitting.

Upvotes: 3

Related Questions