opencv获取摄像头的最大分辨率图像

事情是这样的,在拼多多花了40买了一个4k高清的摄像偷,确实清楚。但是我一直以为网络摄像头分辨率只有640*480,于是用python测试了一下,上代码

复制代码
import cv2

def get_max_resolution(camera_index):
    """
    获取摄像头的最大分辨率。
    """
    cap = cv2.VideoCapture(camera_index)
    if not cap.isOpened():
        return None, None

    max_width, max_height = 0, 0
    # 定义常见分辨率列表
    resolutions = [
        (640, 480),
        (1280, 720),
        (1920, 1080),
        (2560, 1440),
        (3840, 2160)
    ]

    for width, height in resolutions:
        cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
        cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

        actual_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
        actual_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

        if actual_width == width and actual_height == height:
            max_width, max_height = actual_width, actual_height

    cap.release()
    return max_width, max_height


def find_all_cameras(max_index=10):
    """
    遍历所有可用的摄像头并输出最大分辨率。
    """
    cameras = []

    for camera_index in range(max_index):
        cap = cv2.VideoCapture(camera_index)
        if cap.isOpened():
            cameras.append(camera_index)
            cap.release()

    print(f"发现 {len(cameras)} 个摄像头:")
    for camera in cameras:
        max_width, max_height = get_max_resolution(camera)
        if max_width and max_height:
            print(f"摄像头 {camera} 的最大分辨率为:{max_width}x{max_height}")
        else:
            print(f"摄像头 {camera} 无法获取分辨率")


def capture_image_with_maximized_window(camera_index, width, height):
    """
    采集摄像头图像并以最大化窗口显示。
    """
    cap = cv2.VideoCapture(camera_index)
    if not cap.isOpened():
        print(f"无法打开摄像头 {camera_index}")
        return

    # 设置摄像头分辨率
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)

    actual_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    actual_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    print(f"实际采集的图像分辨率: 宽度={actual_width}, 高度={actual_height}")

    ret, frame = cap.read()
    if ret:
        # 创建窗口并最大化
        cv2.namedWindow("Captured Frame", cv2.WINDOW_NORMAL)
        cv2.resizeWindow("Captured Frame", 1920, 1080)  # 设置默认分辨率窗口大小
        cv2.imshow("Captured Frame", frame)

        # 保存图像到本地
        cv2.imwrite("captured_image.jpg", frame)
        print("图像已保存为 'captured_image.jpg'")
    else:
        print("无法采集图像")

    # 等待按键退出
    cv2.waitKey(0)
    cap.release()
    cv2.destroyAllWindows()


if __name__ == "__main__":
    # 遍历所有摄像头并输出最大分辨率
    find_all_cameras()

    # 示例:采集第一个摄像头的图像
    camera_index = 0
    capture_width = 1920
    capture_height = 1080
    capture_image_with_maximized_window(camera_index, capture_width, capture_height)

输出为:发现 2 个摄像头:

摄像头 0 的最大分辨率为:1920x1080

摄像头 2 的最大分辨率为:1920x1080

实际采集的图像分辨率: 宽度=1920, 高度=1080

买了2个摄像头,一个2K,一个4k实际都是2k吧,请问另外的2k哪里去了

相关推荐
程序员三藏36 分钟前
如何使用Jmeter进行压力测试?
自动化测试·软件测试·python·测试工具·jmeter·测试用例·压力测试
carpell39 分钟前
【语义分割专栏】3:Segnet原理篇
人工智能·python·深度学习·计算机视觉·语义分割
24K纯学渣40 分钟前
Python编码格式化之PEP8编码规范
开发语言·ide·python·pycharm
怒视天下42 分钟前
零基础玩转Python生物信息学:数据分析与算法实现
开发语言·python
zhanshuo1 小时前
Python元组黑科技:3招让数据安全暴增200%,学生管理系统实战揭秘!
python
空中湖1 小时前
免费批量图片格式转换工具
图像处理·python·程序人生
Mantanmu2 小时前
Python训练day40
人工智能·python·机器学习
天天爱吃肉82182 小时前
新能源汽车热管理核心技术解析:冬季续航提升40%的行业方案
android·python·嵌入式硬件·汽车
ss.li2 小时前
TripGenie:畅游济南旅行规划助手:个人工作纪实(二十二)
javascript·人工智能·python
l木本I2 小时前
大模型低秩微调技术 LoRA 深度解析与实践
python·深度学习·自然语言处理·lstm·transformer