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::proce*turedImage(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(proce*turedImage(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获取清晰度资源-****文库

上一篇:windos gcc 安装


下一篇:HalconDotNet实现二维码识别功能详解