I am an entry level programmer in Opencv2.3 and playing around with codes available in the internet and various useful resources. There is a code implemented by Stack User User:Froyo in his website website. When I tried executing the code, the program exits as soon as I drag and draw a bounding box around the region of interest. The Runtime error is
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
The source code is available for download github. Can somebody please help in resolving what is the error so that the program may run at my end. I have no clue what seems to be the problem!
Does the error occur after the mouse button up?
Can you check if the ROI is selected correctly by just outputting the position of the point1 and point2?
std::cout << "point1" << point1 << std::endl;
just bellow the mouse callback function in the main() function?
I am not able to test the code at the moment. For understanding the principle and using OpenCV for tracking feature, I suggest you start from 2D images. Please refer to Features2D + Homography to find a known object
I tested the code. Here is the problem: the new position of ROI could cause error when its value is larger than the resolution of your webcam. So I modify the newPoint() function as bellow and it works properly
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);
}