使用OpenCV自动实时拍摄不同曝光时间的照片

2024-10-06 08:36:36 发布

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

我正试图用OpenCV制作一个python程序,它打开网络摄像头,实时(40毫秒、95毫秒、150毫秒)拍摄多张不同曝光的图像,最后对它们进行平均。 我尝试创建一个循环,在其中更改曝光时间,更新渲染(帧)并将其保存在列表中,但问题是显示保持静态,渲染几乎没有更改(合并图像后,会生成曝光时间接近40的图像)

我认为在设置曝光时间之后,帧更新需要一些时间,所以我添加了方法time.sleep来暂停执行3秒,但这是徒劳的

这是我的密码

import numpy as np
import cv2
import os
import time

capture = cv2.VideoCapture(0, cv2.CAP_V4L2)

while True:
    (grabbed, frame) = capture.read()

    if not grabbed:
        break

    # Resize frame
    width = 1500
    height = 1000
    dim = (width, height)
    frame = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
    
    cv2.imshow('RGB', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    if cv2.waitKey(1) == ord('h') or cv2.waitKey(1) == ord('H'):
        #repertory = input("Enter the name of the directory: ")
        if not os.path.exists(repertory):
            os.mkdir(repertory)
        exposure = [40,95,150]
        ims = []
        for i in exposure:
            capture.set(cv2.CAP_PROP_EXPOSURE, i) # Setting Exposure
            (grabbed, frame) = capture.read() # Updating frame
            if grabbed:
                cv2.imshow('RGB', frame) #Display
                ims.append(frame)


        # Convert to numpy
        ims = np.array([np.array(im) for im in ims])
        # average et conversion en uint8
        imave = np.average(ims, axis=0)
        imave = imave.astype(np.uint8)

        # image HDR
        cv2.imwrite(repertory + '/' + repertory + '_HDR8.jpg', imave)


capture.release()
cv2.destroyAllWindows()

是否有一个最佳的解决方案,允许以实时和自动的方式拍摄不同曝光时间的照片


Tags: 图像importifosnp时间cv2frame