繁体   English   中英

Python Opencv黑色窗口

[英]Python Opencv black window

我使用python和opencv在20幅图像上平均图像,它们之间的间隔为0.5秒。 到目前为止,我已经成功使用了这个库,但是这个给我带来了一个问题。 这是我的代码

import numpy as np
import cv2
import time

webcam = cv2.VideoCapture(0)
webcam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
webcam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 480)
webcam.set(cv2.cv.CV_CAP_PROP_EXPOSURE, .2)

total = np.zeros((480,640,3), int)
time.sleep(0.500)
x = 0
while x < 19:
    retval, img = webcam.read()
    if(retval == True):
        total += img;
        time.sleep(0.500)
        x += 1

result = np.zeros((480,640,3), int)
result = np.divide(total, 20, dtype=int)

cv2.namedWindow("result",cv2.cv.CV_WINDOW_AUTOSIZE)
cv2.cv.MoveWindow("result", 0, 0)
while True:
    cv2.imshow("result", result)
    if(cv2.waitKey(10) == 27):
        break

cv2.destroyAllWindows()
webcam.release()

尽管运行该程序没有出现错误,但它确实显示黑色图像。 img变量返回一个工作图像。 平均可能在这里产生错误,但是据我所知结果矩阵的内容是正确的。

您在OpenCV中混合使用np.uint8int32 ,这将导致出现所有黑人

当你需要确定一个累加器阵列total有足够的比特深度增量+=总结阶段,但您np.divide()要传递给cv2.imshow() 都将导致显示器能够dtype = np.uint8

result = np.zeros( (480,640,3),        np.uint8 )    # needless to pre-allocate
result = np.divide( total, 20, dtype = np.uint8 )    # just enough to assign here

NB1:

您可能还会注意到double .set()的错字,如果VideoCapture()设备仍返回(480,640,3) np.uint8 -s数组,则不会出现任何问题

webcam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 640)
webcam.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 480)
#                                   |||||  ^^^ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM