opencv获取摄像头帧率分辨率

本文介绍如何使用Python和C++的OpenCV库来设置摄像头的分辨率,并获取摄像头的实时帧率。通过代码示例展示了如何调整摄像头的宽度、高度及曝光时间,并在C++中演示了帧率的计算方法。

python opencv 获取摄像头分辨率,设置摄像头分辨率

import cv2
 
cap = cv2.VideoCapture(0)
 
#先设置参数,然后读取参数
 
cap.set(3,1280)  # width
 
cap.set(4,1024)  # height
 
cap.set(15, 0.1)
 
print("width={}".format(cap.get(3)))
 
print("height={}".format(cap.get(4)))
 
print("exposure={}".format(cap.get(15)))
 
 
while True:
 
ret, img = cap.read()
 
cv2.imshow("input", img)
 
# 按 ESC 键退出
 
key = cv2.waitKey(10)
 
if key == 27:
 
break
 
 
cv2.destroyAllWindows()
 
cv2.VideoCapture(0).release()

c++代码:

 

#include <stdlib.h>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "opencv2/imgproc/imgproc.hpp"
int main(int argc, const char** argv)
{
    cv::Mat frame;
    // 可从摄像头输入视频流或直接播放视频文件
    cv::VideoCapture capture(0);
//    cv::VideoCapture capture("vedio1.avi");
    double fps;
    char string[10];  // 用于存放帧率的字符串
    cv::namedWindow("Camera FPS");
    double t = 0;
    while (1)
    {
        t = (double)cv::getTickCount();
        if (cv::waitKey(1) == 1){ break; }
        if (capture.isOpened())
        {
            capture >> frame;
            // getTickcount函数:返回从操作系统启动到当前所经过的毫秒数
            // getTickFrequency函数:返回每秒的计时周期数
            // t为该处代码执行所耗的时间,单位为秒,fps为其倒数
            t = ((double)cv::getTickCount() - t) / cv::getTickFrequency();
            fps = 1.0 / t;
            sprintf(string, "%.2f", fps);      // 帧率保留两位小数
            std::string fpsString("FPS:");
            fpsString += string;                    // 在"FPS:"后加入帧率数值字符串
            printf("fps: %.2f width:%d height:%d fps:%.2f\n", fps,frame.cols,frame.rows, capture.get(CV_CAP_PROP_FPS));
            // 将帧率信息写在输出帧上
          cv::putText(frame, // 图像矩阵
                    fpsString,                  // string型文字内容
                    cv::Point(5, 20),           // 文字坐标,以左下角为原点
                    cv::FONT_HERSHEY_SIMPLEX,   // 字体类型
                    0.5, // 字体大小
                    cv::Scalar(0, 0, 0));       // 字体颜色
            cv::imshow("Camera FPS", frame);
        }
        else
        {
            std::cout << "No Camera Input!" << std::endl;
            break;
        }
    }
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AI算法网奇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值