简体   繁体   English

PiCamera和CV2解码总是返回None

[英]PiCamera and CV2 imdecode always returning None

I have a problem using CV2 on Raspberry PI with the PiCamera. 我在PiCamera的Raspberry PI上使用CV2时遇到问题。 This is my code: 这是我的代码:

camera = PiCamera()
camera.resolution = ( 320, 240 )
camera.framerate = 60
rawCapture = PiRGBArray( camera, size=( 320, 240 ) )
time.sleep(1)
start = time.time()

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):


    # and occupied/unoccupied text
    images = frame.array

    # show the frame
    cv2.imshow("Frame", images)


    cv_img = images.astype(np.uint8)

    gray  = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_GRAYSCALE)
    image = cv2.imdecode(np.fromstring(cv_img, dtype=np.uint8), cv2.IMREAD_UNCHANGED)

    print("IMAGE: ",image)
    print("GRAY: ",gray)

image and gray are always None while images and cv_image contains the right frame. image和gray始终为None,而images和cv_image包含正确的帧。 I can't really understand why it won't work, Could you please help me? 我真的不明白为什么它不起作用,请您能帮我吗?

Thank you in advance. 先感谢您。

I've found picamera.array to be a reliable way to bridge between picamara and cv2 . 我发现picamera.array是在picamaracv2之间picamara的可靠方法。 The docs have some examples that aren't far off from yours. 这些文档中的示例与您的示例相距不远。

Once you're squarely in cv2 territory, 一旦您完全进入cv2领域,

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

is the typical way to do the grayscale conversion. 是进行灰度转换的典型方法。 Doing it by way of imdecode is excessive. 通过imdecode是多余的。

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

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