开启摄像头
.pro文件添加,然后要清理项目,qmake, 构建项目 更新,包含头文件
INCLUDEPATH += D:/projects/OpenCV/opencv/build/include/opencv2\
D:/projects/OpenCV/opencv/build/include/opencv\
D:/projects/OpenCV/opencv/build/include
LIBS += D:/projects/OpenCV/opencv/build/x64/vc14/lib/opencv_world346d.lib
打开摄像头、定时器
//从摄像头读入视频 0表示从摄像头读入 VideoCapture* capture; Mat* frame
capture=new VideoCapture(CAP_DSHOW);
frame=new Mat;
capture->open(0);
//参数设置
capture->set(CV_CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M', 'J', 'P', 'G')); //图像编码
capture->set(CV_CAP_PROP_FRAME_WIDTH, 640);
capture->set(CV_CAP_PROP_FRAME_HEIGHT, 480);
if (capture->isOpened())//先判断是否打开摄像头
{
double rate= capture->get(CV_CAP_PROP_FPS);
QString s=QString(tr("is open fps %1")).arg(rate);
ui->stateLabel->setText(s); //显示帧率
*capture >> *frame;
if (!frame->empty())
{
cv::cvtColor(*frame,*frame,CV_BGR2RGB);
QImage img = cvMat2QImage(*frame);
ui->label->setPixmap(QPixmap::fromImage(img));
//开启定时器,获取下一帧
timer = new QTimer(this);
timer->setInterval(1000/rate); //set timer match with FPS
connect(timer, SIGNAL(timeout()), this, SLOT(nextFrame()));
timer->start(); //启动ding's定时器
}
}
获取下一帧
void camera::nextFrame()
{
*capture >> *frame;
if (capture->isOpened())//先判断是否打开摄像头
{
if (!frame->empty())
{
//录制
if(recordFlag)
writer->write(*frame);
}
QImage img=cvMat2QImage(*frame);
ui->label->setPixmap(QPixmap::fromImage(img));
}
}
录制视频
writer= new VideoWriter;
writer->open("./myrec.avi",cv::VideoWriter::fourcc('M','J','P','G'), /*capture.get(CV_CAP_PROP_FPS)*/30
, cv::Size(frame->cols, frame->rows),true);
Mat转QImage
QImage MainWindow::cvMat2QImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS = 1
if(mat.type() == CV_8UC1)
{
QImage image(mat.cols, mat.rows, QImage::Format_Indexed8);
// Set the color table (used to translate colour indexes to qRgb values)
image.setColorCount(256);
for(int i = 0; i < 256; i++)
{
image.setColor(i, qRgb(i, i, i));
}
// Copy input Mat
uchar *pSrc = mat.data;
for(int row = 0; row < mat.rows; row ++)
{
uchar *pDest = image.scanLine(row);
memcpy(pDest, pSrc, mat.cols);
pSrc += mat.step;
}
return image;
}
// 8-bits unsigned, NO. OF CHANNELS = 3
else if(mat.type() == CV_8UC3)
{
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
//注意是mat.step, 网上其它一些方法这里不对,会导致显示图片倾斜、损坏
QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_RGB888);
return image.rgbSwapped();
}
else if(mat.type() == CV_8UC4)
{
qDebug() << "CV_8UC4";
// Copy input Mat
const uchar *pSrc = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage image(pSrc, mat.cols, mat.rows, (int)mat.step, QImage::Format_ARGB32);
return image.copy();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
}
Qlabel上显示
QImage img = cvMat2QImage(src);
QPixmap pixmap=QPixmap::fromImage(img);
//按比例缩放 饱满填充是 IgnoreAspectRatio
pixmap = pixmap.scaled(ui->label->width(), ui->label->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
ui->label->setPixmap(pixmap);
rgb=(*frame)(Rect(160,0,320,480)); //截取特定区域
tabwidget添加
//tabwidget 添加起始页
camera=new Camera();
ui.tabWidget->insertTab(0,camera,"相机");
ui.tabWidget->setCurrentIndex(0); //设置当前活动页