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文库

相关推荐
如若1233 小时前
主要用于图像的颜色提取、替换以及区域修改
人工智能·opencv·计算机视觉
「QT(C++)开发工程师」5 小时前
【qt版本概述】
开发语言·qt
xcLeigh5 小时前
HTML5超酷响应式视频背景动画特效(六种风格,附源码)
前端·音视频·html5
lsjweiyi6 小时前
极简AI工具箱网站开源啦!
opencv·开源·微信支付·支付宝支付·百度ai·极简ai工具箱·ai图像处理
韩曙亮7 小时前
【FFmpeg】FFmpeg 内存结构 ③ ( AVPacket 函数简介 | av_packet_ref 函数 | av_packet_clone 函数 )
ffmpeg·音视频·avpacket·av_packet_clone·av_packet_ref·ffmpeg内存结构
一路冰雨9 小时前
Qt打开文件对话框选择文件之后弹出两次
开发语言·qt
Jack黄从零学c++9 小时前
opencv(c++)---自带的卷积运算filter2D以及应用
c++·人工智能·opencv
老赵的博客10 小时前
QT 自定义界面布局要诀
开发语言·qt
码码哈哈0.010 小时前
VSCode 2022 离线安装插件QT VSTOOl报错此扩展不能安装在任何当前安装的产品上。
ide·vscode·qt
9527华安11 小时前
FPGA实现PCIE3.0视频采集转10G万兆UDP网络输出,基于XDMA+GTH架构,提供工程源码和技术支持
网络·fpga开发·udp·音视频·xdma·pcie3.0·万兆网