Roger Travis
Roger Travis

Reputation: 8538

How to detect a line/curve in a bitmap? ( C# )

Suppose I have such bitmap from laser scanning with a red line from the laser on it, what would be the right way to find the center of that line? Either to store its coordinates in an array or to just draw a think line over it.

What approach would you suggest? Preferebly with an option to be able to smooth out that line.

enter image description here
(source: gyazo.com)

THanks

Upvotes: 3

Views: 3939

Answers (3)

Nathan Abramov
Nathan Abramov

Reputation: 115

My suggestion is:

  • you go row by row, saving the coordinates of the first and last appearance of red-ish pixel in each row.
  • then, stretch a line between each two coordinates in every row, or between the middle pixels of each two coordinates.

Upvotes: 1

Meirion Hughes
Meirion Hughes

Reputation: 26408

I would approach this with a worm. Have your worm start on one pixel and allow it to move along the line. Every time you detect a change in direction in any dimension you place a point. Then fit a spline through your points. Add the start and end locations as points too.

Important issues:

  • You need to maintain which pixels have been visited, such that when a worm finishes you can detect if you need to start a new one on what is left.
  • You need to maintain a velocity vector in your worm and weight posible forward choices based on which will more closely continue the line your're currently on. This is because...
  • You need to deal with topology changes, where you have two or more lines intersecting the same point. or a Split in the line into two.

For fitting the spline itself have a look at Numerics on NuGet

Upvotes: 1

Aloraman
Aloraman

Reputation: 1420

I'd suggest to

  • Convert image to monochrome
  • Convert image to black-white using "image thresholding"
  • Split image in small parts
  • For every part,that is not entirely black, calculate Hough Transform and fine approximating segment
  • Merge these segments into chain and then smooth them (using Catmull-Rom splines for example)

However It is not the only possible approach, there are a lot of them

Upvotes: 4

Related Questions