Qt总结(三)--Camera (OpenCV 实现)

开启摄像头

.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); //设置当前活动页
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值