I\'m a program that find contours in a stream, for example :
I want to find \"set of points \
maybe try this approach with a distance transform and ridge detection:
cv::Mat input = cv::imread("fitLine.jpg");
cv::Mat gray;
cv::cvtColor(input,gray,CV_BGR2GRAY);
cv::Mat mask = gray>100;
cv::imshow("mask",mask);
cv::Mat dt;
cv::distanceTransform(mask,dt,CV_DIST_L1,CV_DIST_MASK_PRECISE);
cv::imshow("dt", dt/15.0f);
cv::imwrite("fitLineOut.png",255*dt/15.0f);
//care: this part doesn't work for diagonal lines, a ridge detection would be better!!
cv::Mat lines = cv::Mat::zeros(input.rows, input.cols, CV_8UC1);
//only take the maxDist of each row
for(unsigned int y=0; y(current) > biggestDist)
{
biggestDist = dt.at(current) ;
biggestDistLoc = current;
}
}
lines.at(biggestDistLoc) = 255;
}
//and the maxDist of each row
for(unsigned int x=0; x(current) > biggestDist)
{
biggestDist = dt.at(current) ;
biggestDistLoc = current;
}
}
lines.at(biggestDistLoc) = 255;
}
cv::imshow("max", lines);
cv::waitKey(-1);
the idea is to compute the distance transform and find the ridges within the contour.
this is how the distance transformed image looks like: you can see that there is a local ridge maximum in the middle of the lines.
then I use a very simple method: just find the maximum distance in each row/column. That is very sloppy and should be changed for a real ridge detection or thinning method!!!!
edit: additional description:
The idea is to find all points that are in the "middle" of the contour. In mathematics/graphics, the medial axis in some kind of "middle" of an object and it's definition is to be all points that have the same minimum distance to at least two contour points at the same time.
A way to approximate the medial axis is to compute the distance transform. The distance transform is a matrix that holds for each pixel the distance to the next object point (for example a contour of an object)(see http://en.wikipedia.org/wiki/Distance_transform too). That's the first image. There you can see that the points in the middle of the lines are a little bit brighter than the points closer to the border, which means that the brightest points along the lines can be interpreted as the medial axis (approximation), since if you move away from it (orthogonal to the line direction) the distances become smaller, so the peak is the point where the distance to both borders in close to equality.
That way if you can find those "ridges" in the distance transform, you're done. Ridge detection is normally done by a Harris Operator (see http://en.wikipedia.org/wiki/Ridge_detection ).
In the fast and dirty version that I've posted, I try to detect the ridge by accepting only the maximum value in each line and in each row. That's ok for most horizontal and vertical ridges, but will fail for diagonal ones. So maybe you really want to exchange the for loops
with a real ridge detection
.