opencv计算摄像头图像帧数

运行效果

在这里插入图片描述

代码

#include <iostream>
#include "opencv2/opencv.hpp"
#include <time.h>
#include <string.h>

int main()
{
    // 新建窗口
    cv::namedWindow("camera", cv::WINDOW_FREERATIO);
    // 打开相机
    cv::VideoCapture camera;
    camera.open(0);
    camera.set(cv::CAP_PROP_FRAME_WIDTH, 9999);
    camera.set(cv::CAP_PROP_FRAME_HEIGHT, 9999);
    cv::Mat frame;
    camera.read(frame);
    std::cout << "frame info->" << "width:" << frame.cols << ",height:" << frame.rows << std::endl;
    // 配置时钟
    clock_t begin, end;
    int counter = 0; // 计算获取了多少张图像
    double timeUse = 0;
    double FPS = 0;
    // 对图像进行处理需要的变量
    cv::Mat m0;
    while (true)
    {
        // 开始计时
        if (counter == 0)
            begin = clock();
        camera.read(frame);
        // 对图像进行处理
        cv::flip(frame, frame, 1); // 水平翻转画面
        m0 = frame.clone();
        counter++;
        // 结束计时
        static char tempBuf[30];
        static cv::Size textSize;
        static int val;
        if (counter == 10) // 每10帧数据计算一次帧数
        {
            end = clock();
            timeUse = (end - begin) / (double)CLOCKS_PER_SEC;
            FPS = 1 / timeUse * 10;
            // std::cout << "FPS:" << FPS << std::endl;
            // 把帧数写上m0
            sprintf(tempBuf, "%04.1f", FPS);
            textSize = cv::getTextSize(tempBuf, cv::FONT_HERSHEY_PLAIN, 3.0, 2, &val);
            std::cout << "val:" << val << std::endl;
            counter = 0;
        }
        cv::putText(m0, tempBuf, cv::Point(frame.cols - textSize.width, textSize.height + 10), cv::FONT_HERSHEY_PLAIN,
                    3.0, cv::Scalar(0, 0, 255), 2);
        // 显示画面
        cv::imshow("camera", m0);
        if (cv::waitKey(1) == '1')
        {
            break;
        }
    }
    cv::destroyAllWindows();
    camera.release();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值