使用 Hough OpenCV 时出错
Error when using Hough OpenCV
如果我什么都不做(也就是说,不通过控件 Window 更改颜色检测 HSV),应用程序运行良好。但是,如果我在应用程序为 运行 时更改 HSV 值,我会收到以下错误。我已经在没有 Hough 的情况下测试了代码,它运行良好。
CPU错误-
Unhandled exception at 0x00007FF9ECA64388 (ucrtbase.dll) in HoughFinder.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
这是我的代码 -
VideoCapture capture(0); // 0 is my webcam
...
capture.read(displayOriginal))
...(Code to detect colors for extra accuracy)
cudaCanny->detect(imgThresholded, imgCanny);
vector<Vec2f> lines;
//Ptr<HoughLinesDetector> hough = createHoughLinesDetector(1, CV_PI / 180, 100); CUDA code...
//hough->detect(imgCanny, lines); CUDA code...
HoughLines(displayCanny, lines, 1, CV_PI / 180, 100, 0, 0); // CPU code...
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000 * (-b));
pt1.y = cvRound(y0 + 1000 * (a));
pt2.x = cvRound(x0 - 1000 * (-b));
pt2.y = cvRound(y0 - 1000 * (a));
line(displayHough, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
}
imshow("Hough", displayHough);
imshow("Live Video", displayOriginal);
额外信息 -
如果我用CUDA代码使用Hough,我会得到这个错误-
Unhandled exception at 0x00007FF9F561A1C8 in HoughFinder.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A75E81EB70.
应用程序错误(使用 CPU 代码时不会出现此错误)-
OpenCV Error: Assertion failed (d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in cv::_OutputArray::create, file OPENCV_DIR\opencv-sources\modules\core\src\matrix.cpp, line 2363
有人可以帮忙吗?如果 CPU 或 CUDA 代码是固定的,那很好,但我更希望 CUDA 错误被修复(因为 CUDA 有额外的速度)。
我遇到了类似的问题。我的代码是这样的:
cv::Mat src, src_gray, src_blured, detected_edges, hough_lines;
std::vector<cv::Vec4i> lines;
src = cv::cvarrToMat(img, false);
opencv_run_ = true;
while (opencv_run_) {
cv::cvtColor(src, src_gray, CV_BGR2GRAY);
cv::medianBlur(src_gray, src_blured, blur_size_);
cv::Canny(src_blured, detected_edges, canny_low_threshold_, canny_max_threshold_, canny_aperture_size_);
cv::HoughLinesP(detected_edges, lines, 2, CV_PI / 2, hough_votes_, hough_min_line_length_, hough_max_line_gap_);
hough_lines = cv::Mat::zeros(size_horizontal_, size_vertical_, CV_8UC4);
for (size_t i = 0; i < lines.size(); i++) {
cv::line(hough_lines, cv::Point(lines[i][0], lines[i][1]), cv::Point(lines[i][2], lines[i][3]), cv::Scalar(0, 0, 255), 3, 8);
}
if (lines.size() > 0) lines.clear();
cv::imshow("Gray scale", src_gray);
cv::imshow("Edges", detected_edges);
cv::imshow("Hough", hough_lines);
//Some logic in waitKey and sleeptime
cv::waitKey(sleep_time);
}
cvDestroyAllWindows();
cvReleaseImageHeader(&img);
其中 img
是指向 IplImage
的指针,我在其中手动配置值。 (我的图像数据来自 API 给我 void *
即原始数据的相机)
这段代码在 boost::thread 中 运行。虽然在循环内一切 运行 都很好,但当我
opencv_run = false;
this_boost_thread.join();
为了停止线程,我遇到了无效参数异常。让我感到困惑的是,异常是在线程 return 之后抛出的,这让我担心这是一个典型的堆栈损坏案例。
经过几个小时的搜索,我在某个论坛上遇到了一些 post 说这可能是链接库的问题。所以我检查了我的 opencv 安装,发现我的库在 vc12 文件夹中,这意味着 Visual Studio 2014(我喜欢安装预构建,因为我是个白痴),与我使用的 VS 2015 不同。
所以我搜索了 Visual Studio 2015 opencv 库,在 opencv 3.1 版本 https://sourceforge.net/projects/opencvlibrary/files/opencv-win/ , while i was using opencv 2.4.13. I decided not use them and build opencv from scratch. So i cloned from here https://github.com/opencv/opencv, followed the instructions given here http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html 中找到了一些,并构建了似乎可以工作的 vc14 x86 opencv3.1 库。
经过多次尝试和错误,我终于找到了解决方案。实际上检测中的输出应该是 GpuMat
而不是 vect2d
。我早就想通了,但是 OpenCV 的文档非常混乱。这是编辑后的代码 -
Ptr<HoughLinesDetector> houghLines = createHoughLinesDetector(1, CV_PI / 180, 120);
GpuMat tmpLines; // This should be GpuMat...
vector<Vec2d> lines;
GpuMat imgCanny;
...
while(true) {
...
houghLines->detect(imgCanny, tmpLines);
houghLines->downloadResults(tmpLines, lines);
...
}
OpenCV GPU Error (Function Not Implemented) in Hough Transform
如果我什么都不做(也就是说,不通过控件 Window 更改颜色检测 HSV),应用程序运行良好。但是,如果我在应用程序为 运行 时更改 HSV 值,我会收到以下错误。我已经在没有 Hough 的情况下测试了代码,它运行良好。
CPU错误-
Unhandled exception at 0x00007FF9ECA64388 (ucrtbase.dll) in HoughFinder.exe: An invalid parameter was passed to a function that considers invalid parameters fatal.
这是我的代码 -
VideoCapture capture(0); // 0 is my webcam
...
capture.read(displayOriginal))
...(Code to detect colors for extra accuracy)
cudaCanny->detect(imgThresholded, imgCanny);
vector<Vec2f> lines;
//Ptr<HoughLinesDetector> hough = createHoughLinesDetector(1, CV_PI / 180, 100); CUDA code...
//hough->detect(imgCanny, lines); CUDA code...
HoughLines(displayCanny, lines, 1, CV_PI / 180, 100, 0, 0); // CPU code...
for (size_t i = 0; i < lines.size(); i++)
{
float rho = lines[i][0], theta = lines[i][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000 * (-b));
pt1.y = cvRound(y0 + 1000 * (a));
pt2.x = cvRound(x0 - 1000 * (-b));
pt2.y = cvRound(y0 - 1000 * (a));
line(displayHough, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
}
imshow("Hough", displayHough);
imshow("Live Video", displayOriginal);
额外信息 -
如果我用CUDA代码使用Hough,我会得到这个错误-
Unhandled exception at 0x00007FF9F561A1C8 in HoughFinder.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A75E81EB70.
应用程序错误(使用 CPU 代码时不会出现此错误)-
OpenCV Error: Assertion failed (d == 2 && (sizes[0] == 1 || sizes[1] == 1 || sizes[0]*sizes[1] == 0)) in cv::_OutputArray::create, file OPENCV_DIR\opencv-sources\modules\core\src\matrix.cpp, line 2363
有人可以帮忙吗?如果 CPU 或 CUDA 代码是固定的,那很好,但我更希望 CUDA 错误被修复(因为 CUDA 有额外的速度)。
我遇到了类似的问题。我的代码是这样的:
cv::Mat src, src_gray, src_blured, detected_edges, hough_lines;
std::vector<cv::Vec4i> lines;
src = cv::cvarrToMat(img, false);
opencv_run_ = true;
while (opencv_run_) {
cv::cvtColor(src, src_gray, CV_BGR2GRAY);
cv::medianBlur(src_gray, src_blured, blur_size_);
cv::Canny(src_blured, detected_edges, canny_low_threshold_, canny_max_threshold_, canny_aperture_size_);
cv::HoughLinesP(detected_edges, lines, 2, CV_PI / 2, hough_votes_, hough_min_line_length_, hough_max_line_gap_);
hough_lines = cv::Mat::zeros(size_horizontal_, size_vertical_, CV_8UC4);
for (size_t i = 0; i < lines.size(); i++) {
cv::line(hough_lines, cv::Point(lines[i][0], lines[i][1]), cv::Point(lines[i][2], lines[i][3]), cv::Scalar(0, 0, 255), 3, 8);
}
if (lines.size() > 0) lines.clear();
cv::imshow("Gray scale", src_gray);
cv::imshow("Edges", detected_edges);
cv::imshow("Hough", hough_lines);
//Some logic in waitKey and sleeptime
cv::waitKey(sleep_time);
}
cvDestroyAllWindows();
cvReleaseImageHeader(&img);
其中 img
是指向 IplImage
的指针,我在其中手动配置值。 (我的图像数据来自 API 给我 void *
即原始数据的相机)
这段代码在 boost::thread 中 运行。虽然在循环内一切 运行 都很好,但当我
opencv_run = false;
this_boost_thread.join();
为了停止线程,我遇到了无效参数异常。让我感到困惑的是,异常是在线程 return 之后抛出的,这让我担心这是一个典型的堆栈损坏案例。
经过几个小时的搜索,我在某个论坛上遇到了一些 post 说这可能是链接库的问题。所以我检查了我的 opencv 安装,发现我的库在 vc12 文件夹中,这意味着 Visual Studio 2014(我喜欢安装预构建,因为我是个白痴),与我使用的 VS 2015 不同。
所以我搜索了 Visual Studio 2015 opencv 库,在 opencv 3.1 版本 https://sourceforge.net/projects/opencvlibrary/files/opencv-win/ , while i was using opencv 2.4.13. I decided not use them and build opencv from scratch. So i cloned from here https://github.com/opencv/opencv, followed the instructions given here http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_install/windows_install.html 中找到了一些,并构建了似乎可以工作的 vc14 x86 opencv3.1 库。
经过多次尝试和错误,我终于找到了解决方案。实际上检测中的输出应该是 GpuMat
而不是 vect2d
。我早就想通了,但是 OpenCV 的文档非常混乱。这是编辑后的代码 -
Ptr<HoughLinesDetector> houghLines = createHoughLinesDetector(1, CV_PI / 180, 120);
GpuMat tmpLines; // This should be GpuMat...
vector<Vec2d> lines;
GpuMat imgCanny;
...
while(true) {
...
houghLines->detect(imgCanny, tmpLines);
houghLines->downloadResults(tmpLines, lines);
...
}
OpenCV GPU Error (Function Not Implemented) in Hough Transform