QT 获取视频帧Opencv获取清晰度

先展示结果:

1.获取摄像头的分辨率

    mResSize.clear();
    mResSize = camera_->supportedViewfinderResolutions();
    ui->comboBox_resulation->clear();
    int i=0;
    foreach (QSize msize, mResSize) {
        qDebug()<<msize;
        ui->comboBox_resulation->addItem(QString::number(msize.width(),10)+"*"+QString::number(msize.height(),10), i++);
    }  //摄像头支持分辨率打印

2. 拍照

2.1 保存图片

void MainWindow::processCapturedImage(int request_id, const QImage &img)
{
    i++;
    isDirExist("D://capture");
    img_name = "D://capture//" +QString::number(i) +".png";
    //qDebug() << "img_name :" << QString::fromStdString(img_name);
    qDebug() << "img_name :" << img_name;

    img.save(img_name);
}

2.2 判断目录是否存在

bool MainWindow::isDirExist(QString fullPath)
{
    QDir dir(fullPath);
    if(dir.exists())
    {
      return true;
    }
    else
    {
       bool ok = dir.mkpath(fullPath);//创建多级目录
       qDebug() << "success create";
       return ok;
    }
}

3.获取4个角上的亮度与中间亮度

    cv ::Mat image= surface_->matimage();  //从视频帧获取每一帧图片
    cvtColor(image, image, CV_RGBA2GRAY);
    cv::Mat img_1,img_2,img_3,img_4,img_center;
    //cout << " image ::" << image.size() << endl;
    int widgh = image.cols;
    int height = image.rows;
    qDebug() << "weight = " << widgh << " height = "<< height;

    cv::Rect rec_1(0,0,100,100);
    cv::Rect rec_2(0,height-100,100,100);
    cv::Rect rec_3(widgh-100,0,100,100);
    cv::Rect rec_4(height-100,height-100,100,100);
    cv::Rect rec_center(height/2-50,widgh/2-50,100,100);

    img_1 = image(rec_1);
    cv::Scalar graysca_1 = mean(img_1);
    float graylight_1 = graysca_1.val[0];
    qDebug() << "左上 == " << graylight_1 << endl;

    QString str_1 = QString::number(graylight_1,'f',2);
    str_1 = tr("%1").arg(str_1);
    //qDebug() << "str_1 == " << QString::fromStdWString(str_1) ;
    //ui->label_leftup->setText(str_1);


    img_2 = image(rec_2);
    cv::Scalar graysca_2 = mean(img_2);
    float graylight_2 = graysca_2.val[0];
    qDebug() << "左下 == " << graylight_2 << endl;

    QString str_2 = QString::number(graylight_2,'f',2);
    str_2 = tr("%1").arg(str_2);
   // qDebug() << "str_2 == " << QString::fromStdWString(str_2) ;
   // ui->label_leftdown->setText(str_2);

    img_3 = image(rec_3);
    cv::Scalar graysca_3 = mean(img_3);
    float graylight_3 = graysca_3.val[0];
    qDebug() << "右上 == " << graylight_3 << endl;

    QString str_3 = QString::number(graylight_3,'f',2);
    str_3 = tr("%1").arg(str_3);
    //qDebug() << "str_3 == " << QString::fromStdWString(str_3) ;
    //ui->label_rightup->setText(str_3);

    img_4 = image(rec_4);
    cv::Scalar graysca_4 = mean(img_4);
    float graylight_4 = graysca_4.val[0];
    qDebug() << "右下 == " << graylight_4 << endl;

    QString str_4 = QString::number(graylight_4,'f',2);
    str_4 = tr("%1").arg(str_4);
    //qDebug() << "str_4 == " << QString::fromStdWString(str_4) ;
    //ui->label_rightdown->setText(str_4);

    img_center = image(rec_center);
    cv::Scalar graysca_center = mean(img_center);
    float graylight_center = graysca_center.val[0];
    qDebug() << "中间 == " << graylight_center << endl;

    QString str_center = QString::number(graylight_center,'f',2);
    str_center = tr("%1").arg(str_center);

4. 清晰度

        image = QImage2cvMat(img); //Qimage 转换成cv::Mat

        cvtColor(image, image, CV_BGR2RGB);
        Laplacian(image, imageSobel, CV_16U);
        //Sobel(image, imageSobel, CV_16U, 1, 1);

        //图像的平均灰度
        meanValue = cv::mean(imageSobel)[0];
        meanValue = QString::number(meanValue,'f',1).toDouble();

        //qDebug()<<"meanValue == " << meanValue ;

5. 重新开启摄像头

void MainWindow::on_pushButton_open_clicked()
{
    camera_ = new QCamera;
    surface_ = new MyVideoSurface(this);
    camera_->setViewfinder(surface_);

    image_capture = new QCameraImageCapture(camera_);
    connect(image_capture, SIGNAL(imageCaptured(int, QImage)), this, SLOT(processCapturedImage(int, QImage)));

    connect(surface_,&MyVideoSurface::sendsharpness,this,&MainWindow::updatesharpness);

    connect(ui->pushButton_pic,&QPushButton::clicked,this,&MainWindow::pushButton_pic_click);

    //connect(ui->pushButton_getlight,&QPushButton::clicked,this,&MainWindow::pushButton_pic_click);
    connect(ui->pushButton_getlight,&QPushButton::clicked,this,&MainWindow::pushButton_getlight_clicked);
    camera_->start();
    SetFrameResolution();
}

6. 获取某一点的RGB值

        QColor clrCurrent( img.pixelColor( 50, 175 ) );

        qDebug()  << "00 red == " << clrCurrent.red() << "green == "
        << clrCurrent.green() << " blue  == " << clrCurrent.blue();

7. 每一帧的摄像头数据

bool MyVideoSurface::present(const QVideoFrame &frame) //每一帧摄像头的数据,都会经过这里
{
    if (surfaceFormat().pixelFormat() != frame.pixelFormat() || surfaceFormat().frameSize() != frame.size()) {
        setError(IncorrectFormatError);
        stop();
        return false;
    }
    currentFrame_ = frame;  //获取每一帧数据
    widget_->repaint(targetRect_);
    return true;
}

源码:QT摄像头获取每一帧图像数据以及opencv获取清晰度资源-CSDN文库

相关推荐
AI小杨9 分钟前
【车道线检测】一、传统车道线检测:基于霍夫变换的车道线检测史诗级详细教程
人工智能·opencv·计算机视觉·霍夫变换·车道线检测
Black_Friend21 分钟前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC30 分钟前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子1 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
安步当歌2 小时前
【WebRTC】视频编码链路中各个类的简单分析——VideoStreamEncoder
音视频·webrtc·视频编解码·video-codec
顾北川_野2 小时前
Android CALL关于电话音频和紧急电话设置和获取
android·音视频
顶呱呱程序2 小时前
2-143 基于matlab-GUI的脉冲响应不变法实现音频滤波功能
算法·matlab·音视频·matlab-gui·音频滤波·脉冲响应不变法
撞南墙者3 小时前
OpenCV自学系列(1)——简介和GUI特征操作
人工智能·opencv·计算机视觉
苏三有春4 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt
EasyCVR4 小时前
萤石设备视频接入平台EasyCVR多品牌摄像机视频平台海康ehome平台(ISUP)接入EasyCVR不在线如何排查?
运维·服务器·网络·人工智能·ffmpeg·音视频