Qt OpenCV网络摄像头流打开和关闭
Qt OpenCV Webcam Stream Opening and Closing
我使用Qt
创建了一个非常简单的UI,它由一个简单的按钮和一个标签组成。当发出按钮的clicked()
信号时,将调用使用OpenCV
从网络摄像头捕获帧的函数。我目前用于实现此目的的代码是:
cv::Mat MainWindow::captureFrame(int width, int height)
{
//sets the width and height of the frame to be captured
webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);
//determine whether or not the webcam video stream was successfully initialized
if(!webcam.isOpened())
{
qDebug() << "Camera initialization failed.";
}
//attempts to grab a frame from the webcam
if (!webcam.grab()) {
qDebug() << "Failed to capture frame.";
}
//attempts to read the grabbed frame and stores it in frame
if (!webcam.read(frame)) {
qDebug() << "Failed to read data from captured frame.";
}
return frame;
}
捕获帧后,必须将其转换为QImage
才能显示在标签中。为了实现这一点,我使用以下方法:
QImage MainWindow::getQImageFromFrame(cv::Mat frame) {
//converts the color model of the image from RGB to BGR because OpenCV uses BGR
cv::cvtColor(frame, frame, CV_RGB2BGR);
return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
}
我的 MainWaindow
类的构造函数如下所示:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
resize(1280, 720);
move(QPoint(200, 200));
webcam.open(0);
fps = 1000/25;
qTimer = new QTimer(this);
qTimer->setInterval(fps);
connect(qTimer, SIGNAL(timeout()), this, SLOT(displayFrame()));
}
QTimer
应该通过调用dislayFrame()
来显示帧
void MainWindow::displayFrame() {
//capture a frame from the webcam
frame = captureFrame(640, 360);
image = getQImageFromFrame(frame);
//set the image of the label to be the captured frame and resize the label appropriately
ui->label->setPixmap(QPixmap::fromImage(image));
ui->label->resize(ui->label->pixmap()->size());
}
每次发出其timeout()
信号时。然而,虽然这似乎在一定程度上有效,但实际发生的是来自我的网络摄像头(罗技 Quickcam Pro 9000)的视频捕获流反复打开和关闭。表示网络摄像头已打开的蓝色环反复闪烁的事实证明了这一点。这会导致网络摄像头视频流标签的刷新率非常低,并且是不可取的。有没有办法使网络摄像头流保持打开状态,以防止这种"闪烁"发生?
我似乎通过删除行解决了网络摄像头流打开和关闭的问题:
webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);
从 captureFrame()
函数并设置要在 MainWindow
构造函数中捕获的帧的宽度和高度。
相关文章:
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- 在本地网络中通过OpenCV(C++)实时流式传输图像
- 无法使用 OpenCv 3.4.5 从具有C++ dll 的网络摄像机 (rtsp) 中抓取帧
- 从 OpenCV 3 切换到 OpenCV 4 会导致网络摄像头以最大 5 fps 的速度录制,而不是通常的 30 f
- OpenCV 3 中的神经网络权重
- 无法使用 OpenCV 读取网络从咖啡加载数字训练的 Caffe 模型
- OpenCV没有显示来自Macbook网络摄像头的视频
- 使用OpenCV和C++在按键上截取网络摄像头源的屏幕截图
- 使用OpenCV在Android上使用神经网络进行灰度图像分类
- OpenCV 无法从树莓创建的 gstreamer 打开网络流
- TensorFlow OpenCV网络摄像头悬挂
- OpenCV 将剪影与网络摄像头流隔离
- OpenCV-从C/C++中的默认网络摄像头获取图片-GTK问题
- 在 OpenCV 中,设置曝光会导致我的网络摄像头软件无法自动调整曝光
- 无法使用OpenCV从辅助网络摄像头读取VideoCapture中的帧
- 使用Opencv直接从网络摄像头获取灰度图像
- OpenCV - 网络摄像头不起作用
- Qt OpenCV网络摄像头流打开和关闭
- openCV-网络摄像头的视频捕获-延迟问题