views:

198

answers:

3

I have an image file of a map and a curvy road marked with bold red line (wider than 1 px, there is no other red objects on image). Can someone please suggest steps how to recognize this road as a line and then turn it into a function y=f(x) so I can measure precise distances. I have no idea where to start...

Thanks.

+2  A: 

Go through the image pixel by pixel and check the color of each one. If the pixel is red, add the point (x/y coordinates) to a list of points that make up the road.

I'm not sure what you mean by function. Do you want a polynomial or a function to determine whether a particular point is on the road or not? If the former, look for a math library that has a curve fitting function that will take a set of points and give you back the polynomial that best fits those points. I suspect, though, that you probably want the latter. In that case, just have the function take the coordinates in question and search through your point set to determine if the point is in the set. This would fastest if you used a map/hash that encoded the coordinates into a key that you could look up using a O(1) operation.

tvanfosson
Thanks. I need to measure distances, so I need curve fitting algorithm probably. For example I need to find out where is 50% of distance is located on the road.
serg
You could do this by calculating the points on the line between the two points and checking whether more than half of them are in your point set. You don't need a polynomial for this. In fact a polynomial is at best an approximation and would be less accurate.
tvanfosson
Sorry, I didn't understand. Which two points do you mean? Road is not straight, and it has (slightly) variable width, so if in some places it is wider it would affect this distance measuring algorithm because there is more red points even though road is not longer just wider.
serg
Pre-process your red pixels to get to a 1-pixel wide road: http://en.wikipedia.org/wiki/Erosion_(morphology)
erickson
I can't find good curve fitting algorithm, they are too inaccurate...
serg
+1  A: 

Try looking up interpolations. I've used something similar to what you are talking about on a physics simulator. For that, what you need to to is to find some general points to represent the road, all having a constant space apart. You can then map each set of 3 points to a quadratic equation (easy if the points are uniformly separated). After that you need to interpolate the nearby equations. I have some code you could use if you want.

Cory Walker
The problem with interpolation is that it has to go through all dots, and I don't have precise data for that because road has width.
serg
Why don't you just find the point for the middle of the road every pixel or so depending on the accuracy needed?
Cory Walker
How to find middle point? To find middle I need to find direction first. If road is in horizontal direction middle will be between top and bottom pixels, and if it is in vertical middle will be between left and right pixels.
serg
A: 

You could find equations for the edges of both sides and find the middle of those since you now know direction. After that you could fit the points into a new equation.

EDIT: I guess that is similar to the erosion article pointed out by a commenter. Here is the link (apparently the posted link was broken).

Cory Walker