1 | initial version |
There is no OpenCV finction for this, but it's a simple geometry problem. If you write the equation of the line:
(x-x0)-m0(y-y0)=0
then the intersection point will be the (x,y) point that solves the equation of both lines:
(x-x0)-m0(y-y0)=0
(x-x1)-m1(y-y1)=0
If my calculations are correct (better recheck), then:
y=(x0-x1+y1m1-y0m0)/(m1-m0)
x=x0+m0(y-y0)
(just check that the lines aren't parallel: m0!=m1)
Now, the two lines intersect if y is between the two endpoints (y0a<y<y0b
), and the resulting (x,y) point will give you the intersection coordinate.
You'll have to check the intersection between each pair of lines of the two contours, but as the equations are simple, this operation will still be much faster than any other method.
2 | No.2 Revision |
[EDIT] Other solution based on @davide_sd's comment below:
If the contours are not straight lines, you might avoid the case you presented by a dilatation operation on one of the contours. Then, get the common points using the AND operation on the contour images and finally eliminate the points that are too close, which represent probably the same intersection.
(in the example you presented, you'll have two intersection points if you dilate one of the contours, so you have to eliminate one of those).
[Original answer] I keep this answer as it can be useful for others:
There is no OpenCV finction for this, but it's a simple geometry problem. If you write the equation of the line:
(x-x0)-m0(y-y0)=0
then the intersection point will be the (x,y) point that solves the equation of both lines:
(x-x0)-m0(y-y0)=0
(x-x1)-m1(y-y1)=0
If my calculations are correct (better recheck), then:
y=(x0-x1+y1m1-y0m0)/(m1-m0)
x=x0+m0(y-y0)
(just check that the lines aren't parallel: m0!=m1)
Now, the two lines intersect if y is between the two endpoints (y0a<y<y0b
), and the resulting (x,y) point will give you the intersection coordinate.
You'll have to check the intersection between each pair of lines of the two contours, but as the equations are simple, this operation will still be much faster than any other method.