1

How to get an array of coordinates of a (drawn) line in image? Coordinates should be relative to image borders. Input: *.img . Output array of coordinates (with fixed step). Any 3rd party software to do this? For example there is high contrast difference - white background and color black line; or red and green etc.

Example:
enter image description here

8
  • What platform are you going to do this on?
    – Sam R.
    Nov 21, 2012 at 23:22
  • 1
    OpenVC is a good choice if you are going to extract corners with high contrasts.
    – Sam R.
    Nov 21, 2012 at 23:24
  • any good tutorial may be suggested?
    – J.Olufsen
    Nov 21, 2012 at 23:45
  • 1
    Here is a link I've found. Take a look to see if it's what you are looking for.
    – Sam R.
    Nov 22, 2012 at 8:07
  • 1
    Also this
    – Sam R.
    Nov 22, 2012 at 8:09

1 Answer 1

1

Oh, you mean non-straight lines. You need to define a "line". Intuitively, you might mean a connected area of the image with a high aspect ratio between the length of its medial axis and the distance between medial axis and edges (ie relatively long and narrow, even if it winds around). Possible approach:

  • Threshold or select by color. Perhaps select by color based on a histogram of colors, or posterize as described here: Adobe Photoshop-style posterization and OpenCV, then call scipy.ndimage.measurements.label()

  • For each area above, skeletonize. Helpful tutorial: "Skeletonization using OpenCV-Python". However, you will likely need the distance to the edges as well, so use skimage.morphology.medial_axis(..., return_distance=True)

  • Do some kind of cleanup/filtering on the skeleton to remove short branches, etc. Thinking about your particular use, and assuming your lines don't loop around, you can just find the longest single path in the skeleton. This is where you can also decide if a shape is a "line" or not, based on how long the longest path in its skeleton is, relative to distance to the edges. Not sure how to best do that in opencv, but "Analyze Skeleton" in Fiji/ImageJ will let you filter by branch length.

  • What is left is the most elongated medial axis of the original "line" shape. You can resample that to some step that you prefer, or fit it with a spline, etc.

Due to the nature of what you want to do, it is hard to come up with a sample code that will work on a range of images. This is likely to require some careful tuning. I recommend using a small set of images (corpus), running any version of your algo on them and checking the results manually until it is pretty good, then trying it on a large corpus.

EDIT: Original answer, only works for straight lines:

You probably want to use the Hough transform (OpenCV tutorial).

Python sample code: Horizontal Line detection with OpenCV

EDIT: Related question with sample code to skeletonize: How can I get a full medial-axis line with its perpendicular lines crossing it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.