我有以下代码,我在其中加载图像,并希望根据鼠标事件的选择生成ROI图像。
void mouseHandler(int event, int x, int y, int flags, void* param)
{
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point1 = Point(x, y);
drag = 1;
}
if (event == CV_EVENT_MOUSEMOVE && drag)
{
Mat img1 = img.clone();
point2 = Point(x, y);
rectangle(img1, point1, point2, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("image", img1);
waitKey(0);
}
if (event == CV_EVENT_LBUTTONUP && drag)
{
point2 = Point(x, y);
rect = Rect(point1.x,point1.y,x-point1.x,y-point1.y);
drag = 0;
roiImg = img(rect);
}
if (event == CV_EVENT_LBUTTONUP)
{
select_flag = 1;
drag = 0;
}
}
int main()
{
Mat img = imread("C:\\Users\\ashutosh\\Downloads\\Documents\\Experiments\\PL_Pizza\\0001.jpg");
if (!img.data)
{
std::cout << "Image not loaded";
return -1;
}
// imshow("image",img);
while(1)
{
cvSetMouseCallback("image", mouseHandler, NULL);
if (select_flag == 1)
{
imshow("ROI", roiImg);
}
rectangle(img, rect, CV_RGB(255, 0, 0), 3, 8, 0);
imshow("image", img);
k = waitKey(10);
if (k == 27)
{
break;
}
}
return 0;
}
但我一次又一次收到以下错误:
OpenCV错误:imshow中的断言失败(size.width&gt; 0&amp; size.height&gt; 0),文件C:\ opencv \ sources \ modules \ highgui \ src \ window.cpp,第281行。此应用程序已请求运行时以不寻常的方式终止它。 有关更多信息,请联系应用程序的支持团队。 在抛出&#39; cv :: Exception&#39;的实例后终止调用 What():C:\ opencv \ sources \ modules \ highgui \ src \ window.cpp:281:错误:(-215)size.width&gt; 0&amp;&amp;函数imshow中的size.height&gt; 0
请帮帮我。在此先感谢!!