使用OpenCV python查看10位usb摄像头

2024-10-02 02:24:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在OpenCV 4.2.0 Python 3.5.6中查看OmnivisionOV7251摄像头的输出。相机的输出是10位原始灰度数据,我相信这是正确的16位字对齐

当我使用此OpenCV代码时:

import cv2

cam2 = cv2.VideoCapture(0)
cam2.set(3, 640)            # horizontal pixels
cam2.set(4, 480)            # vertical pixels

while True:
    b, frame = cam2.read()

    if b:
        cv2.imshow("Video", frame)

        k = cv2.waitKey(5)

        if k & 0xFF == 27:
            cam2.release()
            cv2.destroyAllWindows()
            break

这是我得到的图像:

OpenCV raw 10-bit OV7251

大概是OpenCV使用了错误的过程将10位原始数据转换为RGB,认为它是某种YUV或其他东西

有什么方法可以让我:

  • 告诉OpenCV相机的正确数据格式,以便正确进行转换
  • 获取原始相机数据,以便我可以手动进行转换

Tags: 数据代码importifcv2frameopencv灰度
1条回答
网友
1楼 · 发布于 2024-10-02 02:24:13

一种方法是获取原始相机数据,然后使用numpy进行校正:

import cv2
import numpy as np

cam2 = cv2.VideoCapture(0)
cam2.set(3, 640)            # horizontal pixels
cam2.set(4, 480)            # vertical pixels

cam2.set(cv2.CAP_PROP_CONVERT_RGB, False);          # Request raw camera data

while True:
    b, frame = cam2.read()

    if b:
        frame_16 = frame.view(dtype=np.int16)       # reinterpret data as 16-bit pixels
        frame_sh = np.right_shift(frame_16, 2)      # Shift away the bottom 2 bits
        frame_8  = frame_sh.astype(np.uint8)        # Keep the top 8 bits       
        img      = frame_8.reshape(480, 640)        # Arrange them into a rectangle

        cv2.imshow("Video", img)

        k = cv2.waitKey(5)

        if k & 0xFF == 27:
            cam2.release()
            cv2.destroyAllWindows()
            break

相关问题 更多 >

    热门问题