我有一个嘈杂的图像,我必须检测特定大小的圆圈。在少数图像上,可能会有非常精细的线条切割。目前我使用的算法基本上是如果在基于轮廓的圆检测中未检测到外部(黑色)和内部中心(白色)圆,我声明有线切割。但这种方法并不总是有效。同样难以检测细线(默认情况下,背景曲面上的线条不应被检测到)。
圆检测和线切割的伪代码
1. convert image to grey scale
2. find hough circles
3. from the circles found, store the ones with expected radii.
4. apply canny to the grey scale image and dilate.
5. find contours on the canny image.
6. check if contour is circle :
if(contour.size() > lineThresh) {
cv::RotatedRect rect = cv::fitEllipse(contour);
float width = rect.size.width * 0.5;
float height = rect.size.height * 0.5;
float shortAxis = 0.0;
float longAxis = 0.0;
if (width < height) {
shortAxis = width;
longAxis = height;
} else {
shortAxis = height;
longAxis = width;
}
if (longAxis == 0.0) {
longAxis = 1.0;
}
float circleMeasure = ((longAxis - shortAxis) / longAxis);
float radius = (longAxis + shortAxis) / 2;
float perimeter = cv::arcLength(contour, false);
float area = abs(cv::contourArea(contour));
if (area <= 0) {
area = 1;
}
float area_diff = std::abs(area - ((radius * perimeter) / 2));
float area_delta = area_diff / area;
if(circleMeasure < this->circleError &&
area_delta < this->areaDelta) {
return true;
}else
return false;
7. if both (exterior and interior central) circles are found, circles are good
else if, found in hough and not in contours, there's a line cut.
这是扩张后得到的图像。有没有什么好方法可以检测到这一行?
答案 0 :(得分:4)
好人们终于找到了解决方案!
- Use tophat morphology on the gray image. This enhances the line cuts along with noise
- Use line filter and rotate it by 10 degrees.
e.g. Mat kernel = (cv::Mat_<float>(3, 3) << -1, 4, -1,
-1, 4, -1,
-1, 4, -1;
(Now only the straight contours remain)
- Check the aspect ratio of the contours and store if it exceeds a threshold.
- Check for collinearity of the contours. If the length of collinear segment is more than a threshold, declare it as a line cut.
这个算法对我有用