How to find endpoints of lines in OpenCV?

别说谁变了你拦得住时间么 提交于 2020-05-29 04:30:27

问题


I have an image made up of lines; how can I find the endpoints with OpenCV?

The lines are about 20 pixels wide. They turn, branch, and can be at angles (although mostly horizontal and vertical). Note that Hough won't work because my lines aren't straight. (Maybe that makes them contours?)

I looked at this answer but it finds extreme points, not endpoints. I looked at this, but I think goodFeaturesToTrack() will pick up corners too. Maybe use a thinning algorithm, although OpenCV doesn't seem to have one?

The image below shows sample input (blue) and the desired endpoints (magenta).


回答1:


You can create a morphological skeleton (thinning algorithm which you talked about) as described here, my implementation of this is here. Then you can traverse the skeleton(s) and just look for the end of it in every way.




回答2:


I speak Chinese, while my English is poor.

So I just post my core steps.

A general way to find endpoints of lines is:

  1. Binary the gray image.
  2. Find the skeleton of the binary image.
  3. Do hit-miss operation (all end points) or goodFeaturesToTrack(all corners include endpoints) on the skeleton.

Notice:

You should select a good skeleton method to make sure that the endpoints wouldn't shrink(while my example does shrink).


This is the result.

This is a demo using hit-miss to find special points.




回答3:


The easiest way for me would be to thin the image to 1px thickness and then use hit and miss transform to detect the endpoints. Unfortunately, none of these functions are implemented in OpenCV. Thinning can also be obtained with hit or miss transform. All you need is thoroughly described in the following link: http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm

Let me know if you have any problems with this, as I have it all implemented. Hit or miss transform might be really old and simple, but it's a very powerful tool.




回答4:


If your endpoints are always either horizontal or vertical you can also try to convolve the image with a simple kernel - square (side should be equal to your line width) with background (width should be equal to minimal distance between neighboring endpoints). Color (or intensity) of square and background should match those on your images.

In this case endpoints would "match" the kernel along 3 "sides", line segments and corners along "2 sides", other shapes should have a weaker response. By thresholding response appropriately you should be able to get endpoint locations.

This approach should be simpler and faster (if implemented appropriately) than the one you settled on currently, but it may have some quirks depending on the input. Unfortunately, I don't have time to try implementing it.



来源:https://stackoverflow.com/questions/42235190/how-to-find-endpoints-of-lines-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!