fis*_*rks 5 c++ opencv edge-detection hough-transform
我在 OpenCV (c++) 中实现了霍夫线变换,我在霍夫空间中得到了奇怪的工件。下图显示了霍夫空间。距离 rho 在行中描绘,而 180 列表示从 0 到 179 度的角度。如果放大第 45 和 135 列,您会看到一条垂直线,其中暗像素和亮像素交替出现。 http://imgur.com/NDtMn6S
对于更高的阈值,可以很好地检测到围栏的线条,但是当我降低阈值时,在最终图片中可以将伪影视为 45° 或 135° 旋转的线条: 检测到中等阈值的线条
起初我认为这是我实现霍夫线方法的错误,但使用 OpenCV 的霍夫线方法获得了中等阈值的类似线。使用 Canny 而不是 Sobel 时,我也遇到了同样的问题。
所以问题是:为什么我会得到这些文物,我该如何摆脱它们?我无法找到任何关于此的信息,任何帮助将不胜感激。
这是我与 OpenCV Hough Lines 方法一起使用的代码:
// read in input image and convert to grayscale
Mat frame = imread("fence.png", CV_LOAD_IMAGE_COLOR);
Mat final_out;
frame.copyTo(final_out);
Mat img, gx, gy, mag, angle;
cvtColor(frame, img, CV_BGR2GRAY);
// get the thresholded maggnitude image
Sobel(img, gx, CV_64F, 1, 0);
Sobel(img, gy, CV_64F, 0, 1);
cartToPolar(gx, gy, mag, angle);
normalize(mag, mag, 0, 255, NORM_MINMAX);
mag.convertTo(mag, CV_8U);
threshold(mag, mag, 55, 255.0, THRESH_BINARY);
// apply the hough lines transform and draw the lines
vector<Vec2f> lines;
HoughLines(mag, lines, 1, CV_PI / 180, 240);
for( size_t i = 0; i < lines.size(); i++ )
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
pt1.x = 0;
pt1.y = (rho - pt1.x * cos(theta))/sin(theta);
pt2.x = mag.cols;
pt2.y = (rho - pt2.x * cos(theta))/sin(theta);
line(final_out, pt1, pt2, Scalar(0,0,255), 1, CV_AA);
}
// show the image
imshow("final_image", final_out);
cvWaitKey();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1441 次 |
| 最近记录: |