# java – I’ve got a road on map. How to convert it into a function? – Education Career Blog

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.

,

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.

,

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.

,

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).