【发布时间】:2019-06-11 09:16:46
【问题描述】:
我想在 c++ 中的 opencv 框架上画一条线。为此,我在下面使用setMouseCallback(winName, onMouse, NULL); 的代码。在下面的代码中,我使用的是图像:
Mat src;
const char* winName = "Image";
int start_x = 0;
int start_y = 0;
bool run_once = false;
void onMouse(int event, int x, int y, int f, void*)
{
if (cv::EVENT_LBUTTONDOWN)
{
if (f == 1)
{
if (run_once == false)
{
start_x = x;
start_y = y;
cout << "start x,y is : " << x << y << endl;
run_once = true;
}
}
if (f == 3)
{
cout << "start x,y is : " << start_x << start_y << endl;
int end_x = x;
int end_y = y;
cout << "end x,y is : " << end_x << end_y << endl;
cv::line(src, cv::Point(start_x, start_y), cv::Point(end_x, end_y), Scalar(255), 2, 8, 0);
imshow(winName, src);
run_once = false;
}
}
}
int main()
{
src = imread(<img path>, 1);
namedWindow(winName, WINDOW_NORMAL);
setMouseCallback(winName, onMouse, NULL);
imshow(winName, src);
while(1)
{
}
}
使用上面的代码,如果我在框架上使用鼠标左键单击,它会记录start_x start_y。我将鼠标向左/向右拖动,然后单击鼠标右键,它会记录 end_x end_y 并简单地画一条线并显示它。这工作正常,但我想在实时视频帧中实现此功能。
我在实时视频帧中面临的问题是,在实时视频源中,我们不断在while(1) 循环中显示帧,因此在下一帧中删除了绘制的线
void onMouse(int event, int x, int y, int f, void*)
{
/*
* SAME AS ABOVE
*/
}
int main(int, char**)
{
VideoCapture cap(1); // open the default camera
if (!cap.isOpened()) // check if we succeeded
return -1;
namedWindow(winName, WINDOW_NORMAL);
setMouseCallback(winName, onMouse, NULL);
for (;;)
{
cap >> src; // get a new frame from camera
imshow(winName, src);
if (waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
在上面的代码中,我们有 onMouse 函数,我们使用 imshow 来显示在框架上绘制的线条,但我们在 while(1) 循环中也有 imshow,因此绘制的线条没有显示。
无论如何我可以在实时视频馈送帧上画线。请帮忙。谢谢
【问题讨论】:
-
您可以在另一个
Mat对象中绘制而不是直接在src上绘制,例如lines,然后使用imshow(winName, src + lines)。这样,src将在切换到新框架时发生变化,但lines保持不变 -
@Sunreef 你能举个例子吗。我想我明白了,但需要更多参考。谢谢