C++/Qt 使用OpenCV打开摄像头,旋转视频,计算fps

C++/Qt 使用OpenCV打开摄像头,旋转视频,计算fps

设置摄像头参数 不要随意修改,同时也不一样会修改成功,需要根据实际摄像头的参数选择设置

    /*设置摄像头参数 不要随意修改
    capture.set(CV_CAP_PROP_FRAME_WIDTH, 1080);//宽度
    capture.set(CV_CAP_PROP_FRAME_HEIGHT, 960);//高度
    capture.set(CV_CAP_PROP_FPS, 30);//帧数
    capture.set(CV_CAP_PROP_BRIGHTNESS, 1);//亮度 1
    capture.set(CV_CAP_PROP_CONTRAST,40);//对比度 40
    capture.set(CV_CAP_PROP_SATURATION, 50);//饱和度 50
    capture.set(CV_CAP_PROP_HUE, 50);//色调 50
    capture.set(CV_CAP_PROP_EXPOSURE, 50);//曝光 50
    */

transpose

函数原型
/**
* 转置,相当于沿对角线翻转
* @param src 输入的源图像
* @param dst 输出的目标图像
* @return
*/

flip

函数原型
/**
* 实现图像的翻转
* @param src 输入的源图像
* @param dst 输出的目标图像
* @param flipCode 翻转码,
* 0 : 表示沿 X 轴翻转
* > 0 : 如 1,表示沿 Y 轴翻转,
* < 0 : 如 -1,表示既沿 X 轴翻转,又沿 Y 轴翻转,等价于旋转180°
* @return
*/

测试代码:

//#include <iostream>
//#include <opencv2/core.hpp>
//#include <opencv2/highgui.hpp>
//#include <opencv2/imgproc.hpp>
#include "opencv2/opencv.hpp"
#include <QTime>
#include <QDebug>

//using namespace std;


int main(void)
{
#if 1
    cv::Mat srcir, srcvis;
    
//    cv::VideoCapture captureir (1);
    cv::VideoCapture capturevis (0);
    
    int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G');
    capturevis.set(cv::CAP_PROP_FOURCC, fourcc);
    capturevis.set(cv::CAP_PROP_FPS, 25);
    capturevis.set(cv::CAP_PROP_FRAME_WIDTH, 800);
    capturevis.set(cv::CAP_PROP_FRAME_HEIGHT, 600);


//    captureir.set(cv::VideoCaptureProperties::CAP_PROP_FOURCC, fourcc);
//    captureir.set(cv::VideoCaptureProperties::CAP_PROP_FPS, 25);
//    captureir.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_WIDTH, 208);
//    captureir.set(cv::VideoCaptureProperties::CAP_PROP_FRAME_HEIGHT, 156);

    //查看参数是否设置成功
    qDebug() << "vis width = "<< capturevis.get(cv::CAP_PROP_FRAME_WIDTH);
    qDebug() << "vis height = "<< capturevis.get(cv::CAP_PROP_FRAME_HEIGHT);

//    qDebug() << "ir width = "<< captureir.get(cv::CAP_PROP_FRAME_WIDTH);
//    qDebug() << "ir height = "<< captureir.get(cv::CAP_PROP_FRAME_HEIGHT);
//    printf("width = %.2f\n", capture.get(CV_CAP_PROP_FRAME_WIDTH));
//    printf("height = %.2f\n", capture.get(CV_CAP_PROP_FRAME_HEIGHT));
//    printf("fps = %.2f\n", capture.get(CV_CAP_PROP_FPS));
//    printf("brightness = %.2f\n", capture.get(CV_CAP_PROP_BRIGHTNESS));
//    printf("contrast = %.2f\n", capture.get(CV_CAP_PROP_CONTRAST));
//    printf("saturation = %.2f\n", capture.get(CV_CAP_PROP_SATURATION));
//    printf("hue = %.2f\n", capture.get(CV_CAP_PROP_HUE));
//    printf("exposure = %.2f\n", capture.get(CV_CAP_PROP_EXPOSURE));

    //计算fps
    static int fps = 0;
    static QTime lastTime = QTime::currentTime(); // ms
    static int frameCount = 0;

    while(1)
    {      
//        QTime st = QTime::currentTime();
//        captureir >> srcir;
        capturevis >> srcvis;
//        QTime et = QTime::currentTime();
//        int hs = st.msecsTo(et);
//        qDebug() << "hs: "<<hs<<" ms";

//        cv::transpose(srcvis, srcvis);
//        cv::flip(srcvis, srcvis, 1);

//        cv::transpose(srcir, srcir);
//        cv::flip(srcir, srcir, 1);

//        if(srcir.empty() == true) {
//            std::cout << "irframe empty, exit..." << std::endl;
//            return -1;
//        }

        if (srcvis.empty() == true) {
            std::cout << "visframe empty, exit..." << std::endl;
            return -1;
        }

//       cv::imshow("captureir",srcir);
       cv::imshow("capturevis",srcvis);

       ++frameCount;

       QTime curTime = QTime::currentTime();
       if (lastTime.msecsTo(curTime) > 1000) // 取固定时间间隔为1秒
       {
           fps = frameCount;
           frameCount = 0;
           lastTime = curTime;
           qDebug()<<"fps: "<<fps;
       }
       char(key)=(char)cv::waitKey(1);
       if(key==27)
           break;

    }
    return 0;
#endif
}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZLOZL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值