OpenCV中的VideoCapture不仅可以打开视频、usb摄像头,还可以做很多事,例如读取流媒体文件,网络摄像头,图像序列等。
下面我简单介绍一个在Qt中用VideoCapture类打开笔记本电脑自带摄像头。
(我用的是VS2015+QT5.8+Opencv3.2)
主要用到下面两个函数
//获取默认摄像头
videocapture = new VideoCapture(0); //把摄像头获取到的某一帧图像传给 Mat matFrame
videocapture->read(matFrame);
然后 我们用Qt里面的Qtimer类定时获取图像,实现连续的每一帧图像的获取,再把Mat显示在界面里面就好啦。
MainWindow.cpp
#pragma execution_character_set("utf-8")
#include "MainWindow.h" //#include <QCameraViewfinder> //QCamera:系统摄像设备(摄像头)
//QCameraViewfinder :摄像取景器部件
//QCameraImageCapture:截图部件
using namespace cv; QtGuiApplication1::QtGuiApplication1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
this->setFixedSize(300, 400);
//setWindowState(Qt::WindowMaximized);//max
timer = new QTimer(this);
timer->stop();
connect(timer, SIGNAL(timeout()), this, SLOT(readFarme())); // 时间到,读取当前摄像头信息
ok = true; pushButton = new QPushButton(tr("开始/暂停"),this);
pushButton->setGeometry(QRect(0, 0, 300, 100));
pushButton->setFont(QFont("Times", 32, QFont::Bold));
connect(pushButton, SIGNAL(clicked()),this, SLOT(on_pushButton_clicked())); clickLabel = new QLabel(this);
clickLabel->setGeometry(0, 100, 300, 300); //打开摄像头,从摄像头中获取视频
videocapture = new VideoCapture(0);
//videocapture = new VideoCapture(0);
//timer->start(33); } QImage QtGuiApplication1::cvMat2QImage(const Mat& mat) // Mat 改成 QImage
{
if (mat.type() == CV_8UC1) // 单通道
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
image.setColorCount(256); // 灰度级数256
for (int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
uchar *pSrc = mat.data; // 复制mat数据
for (int row = 0; row < mat.rows; row++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
} else if (mat.type() == CV_8UC3) // 3通道
{
const uchar *pSrc = (const uchar*)mat.data; // 复制像素
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); // R, G, B 对应 0,1,2
return image.rgbSwapped(); // rgbSwapped是为了显示效果色彩好一些。
}
else if (mat.type() == CV_8UC4)
{
const uchar *pSrc = (const uchar*)mat.data; // 复制像素
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, mat.step, QImage::Format_ARGB32); // B,G,R,A 对应 0,1,2,3
return image.copy();
}
else
{
return QImage();
}
} Mat QtGuiApplication1::QImage2cvMat(QImage image) // QImage改成Mat
{
Mat mat;
switch (image.format())
{
case QImage::Format_ARGB32:
case QImage::Format_RGB32:
case QImage::Format_ARGB32_Premultiplied:
mat = Mat(image.height(), image.width(), CV_8UC4, (void*)image.constBits(), image.bytesPerLine());
break;
case QImage::Format_RGB888:
mat = Mat(image.height(), image.width(), CV_8UC3, (void*)image.constBits(), image.bytesPerLine());
cv::cvtColor(mat, mat, CV_BGR2RGB);
break;
case QImage::Format_Indexed8:
mat = Mat(image.height(), image.width(), CV_8UC1, (void*)image.constBits(), image.bytesPerLine());
break;
}
return mat;
} ////////////////////////////////////事件驱动///////////////////////////////////////////////////////
//打开摄像头
void QtGuiApplication1::on_pushButton_clicked()
{
// 开始计时,超时则发出timeout()信号
if(ok)timer->start(33);
else timer->stop();
ok = !ok;
}
//读取Frame图像 when timeout()
void QtGuiApplication1::readFarme()
{
videocapture->read(matFrame);
QImage imgg = cvMat2QImage(matFrame);
QPixmap qpixmap = QPixmap::fromImage(imgg); // 将图片显示到label上
clickLabel->setPixmap(qpixmap); } //exit
void QtGuiApplication1::bnClose()
{
timer->stop(); // 停止读取数据。
videocapture->release();
//exit
QApplication* app;
app->exit(0);
}
MainWindow.h
#pragma once #include <QtWidgets/QMainWindow>
#include "ui_QtGuiApplication1.h"
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "qimage.h" #include <QFileDialog>
#include <QLabel>
#include <QTimer>
#include <QPushButton>
#include <QCamera>
#include <QCameraImageCapture>
using namespace cv; //OpenCV命名空间 class QtGuiApplication1 : public QMainWindow
{
Q_OBJECT public:
QtGuiApplication1(QWidget *parent = Q_NULLPTR); private:
Ui::QtGuiApplication1Class ui;
QImage cvMat2QImage(const Mat & mat);
Mat QImage2cvMat(QImage image); QTimer *timer;
bool ok;
VideoCapture *videocapture;
Mat matFrame;
QLabel *clickLabel;
QPushButton *pushButton; private slots:
void on_pushButton_clicked();
void readFarme();
void bnClose();
};