2

我是 Opencv2.3 的入门级程序员,我在玩互联网上可用的代码和各种有用的资源。Stack User User:Froyo在他的网站有一段代码。当我尝试执行代码时,只要我在感兴趣的区域周围拖动并绘制一个边界框,程序就会退出。运行时错误是

Unhandled exception at 0x7509c41f (KernelBase.dll) in SIFT_Track.exe: Microsoft C++ exception: cv::Exception at memory location 0x0036f144..
Bad argument: <img is empty or has an incorrect type) in unknown function , file c:\Users\vp\work\ocv\opencv\modules\features2d\src\sift.cpp line 1681
Opencv Error: Assertion failed <0< =roi.x && <roi.width && roi.x+roi.width etc...c:\Users\vp\work\ocv\opencv\modules\core\src\matrix.cpp line 303

源代码可在github下载。有人可以帮助解决什么是错误,以便程序可以在我结束时运行。我不知道似乎是什么问题!

4

1 回答 1

1

鼠标按钮向上后是否出现错误?

您可以仅通过输出point1和point2的位置来检查是否正确选择了ROI?

std::cout << "point1" << point1 << std::endl;

只是在 main() 函数中吼叫鼠标回调函数?

我目前无法测试代码。为了理解原理和使用 OpenCV 进行跟踪功能,我建议你从 2D 图像开始。请参考Features2D + Homography 找到已知对象

我测试了代码。问题是:当 ROI 的值大于网络摄像头的分辨率时,新位置的 ROI 可能会导致错误。所以我修改了 newPoint() 函数如下,它可以正常工作

void newPoints(vector<double> diff, Point cen, double rad)
{
printf("rad = %lf\tcen=(%d, %d)\n",rad, cen.x, cen.y);
printf("%f %f %f %f\n",diff[0], diff[1], diff[2], diff[3]);
point1.x = cen.x - rad - diff[0] >= 0 ? cen.x - rad - diff[0]:0;
point1.x = cen.x - rad - diff[0] <= img.cols ? cen.x - rad - diff[0]:img.cols;

point1.y = cen.y - rad - diff[1] >= 0 ? cen.y - rad - diff[1]:0;
point1.y = cen.y - rad - diff[1] <= img.rows ? cen.y - rad - diff[1]:img.rows;

point2.x = cen.x + rad + diff[2] >= 0 ? cen.x + rad + diff[2]:0;
point2.x = cen.x + rad + diff[2] <= img.cols ? cen.x + rad + diff[2]:img.cols;

point2.y = cen.y + rad + diff[3] >= 0 ? cen.y + rad + diff[3]:0;
point2.y = cen.y + rad + diff[3] <= img.rows ? cen.y + rad + diff[3]:img.rows;

printf("(%d, %d), (%d, %d)\n", point1.x, point1.y, point2.x, point2.y);
}
于 2013-04-17T07:33:31.127 回答