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

View a raw10 bit usb camera using OpenCV python

我正在尝试在 OpenCV 4.2.0 Python 3.5.6 中查看 Omnivision OV7251 相机的输出。相机输出是 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 使用错误的过程将 10 位原始数据转换为 RGB,认为它是某种 YUV 或其他东西。

有什么办法可以:

一种方法是获取原始相机数据,然后使用 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