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:
- extend the line endpoints in the same direction,
- how to find the intersections?
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?
Thank开发者_StackOverflow中文版s for any help
Applying a dilation and then an erosion will extend your endpoints like this:
(*Code in Mathematica*)
Erosion[Dilation[i, 10], 10]
A full solution could be something like this:
r = Dilation[MorphologicalBranchPoints[
Thinning[Binarize@Erosion[Dilation[i, 10], 10], 10]] // Colorize, 3]
ImageAdd[i, r]
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.
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.
Here is what I came up with using Hough Transform:
%# read and binarize image
I = imread('http://i.stack.imgur.com/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
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
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
精彩评论