python,cv2.imshow(),raspberryPi和黑屏

时间:2018-09-21 07:25:51

标签: python raspberry-pi3 opencv3.0

当前尝试使用GUI编写代码,这将允许切换开/关图像处理。理想情况下,该代码将允许打开/关闭窗口视图,实时图像处理(相当基本)以及控制外部板。

我遇到的问题与cv2.imshow()函数有关。几个月前,我通过从picamera切换到cv2来提高处理速度,在这里我可以执行更复杂的计算,例如背景减法,而不必一直调用python。使用 bcm2835-v4l2 包,我能够使用cv2直接从picamera提取图像。

快进6个月,在尝试更新代码时,我发现函数cv2.imshow()无法正确显示。我以为 bcm2835-v4l2 可能是个问题,但是使用matplotlib进行的测试表明连接正常。它似乎与cv2.imshow()有关,所以我想。

我实际上正在使用threading模块创建一个单独的线程来进行图像捕获,我想知道这是否可能是罪魁祸首。我不认为是输入命令

import cv2
camera = cv2.VideoCapture(0)
grabbed,frame = camera.read()
cv2.imshow(frame)

产生相同的黑屏

下面是我正在使用的代码(在RPi3上),一些图像显示了错误以及预期的结果。

作为参考,这里是有关我的系统的详细信息

Raspberry pi3
raspi stretch
python 3.5.1
opencv 3.4.1

代码

import cv2
from threading import Thread
import time
import numpy as np
from tkinter import Button, Label, mainloop, Tk, RIGHT

class GPIOControllersystem:
    def __init__(self,OutPinOne=22, OutPinTwo=27,Objsize=30,src=0):
        self.Objectsize = Objsize

        # Build GUI controller
        self.TK = Tk()                                                          # Place TK GUI class into self

        # Variables
        self.STSP = 0
        self.ShutdownVar = 0
        self.Abut = []
        self.Bbut = []
        self.Cbut = []
        self.Dbut = []

        # setup pi camera for aquisition
        self.resolution = (640,480)
        self.framerate = 60

        # Video capture parameters
        (w,h) = self.resolution
        self.bytesPerFrame = w * h

        self.Camera = cv2.VideoCapture(src)
        self.fgbg = cv2.createBackgroundSubtractorMOG2()

    def Testpins(self):
        while True:
            grabbed,frame = self.Camera.read()
            frame = self.fgbg.apply(frame)

            if self.ShutdownVar ==1:
                break
            if self.STSP == 1:
                pic1, pic2 = map(np.copy,(frame,frame))
                pic1[pic1 > 126] = 255
                pic2[pic2 <250] = 0
                frame = pic1
            elif self.STSP ==1:
                time.sleep(1)
            cv2.imshow("Window",frame)
        cv2.destroyAllWindows()

    def MProcessing(self):
        Thread(target=self.Testpins,args=()).start()
        return self

    def BuildGUI(self):
        self.Abut = Button(self.TK,text = "Start/Stop System",command = self.CallbackSTSP)
        self.Bbut = Button(self.TK,text = "Change Pump Speed",command = self.CallbackShutdown)
        self.Cbut = Button(self.TK,text = "Shutdown System",command = self.callbackPumpSpeed)
        self.Dbut = Button(self.TK,text = "Start System",command = self.MProcessing)

        self.Abut.pack(padx=5,pady=10,side=RIGHT)
        self.Bbut.pack(padx=5,pady=10,side=RIGHT)
        self.Cbut.pack(padx=5,pady=10,side=RIGHT)
        self.Dbut.pack(padx=5,pady=10,side=RIGHT)
        Label(self.TK, text="Controller").pack(padx=5, pady=10, side=RIGHT)
        mainloop()

    def CallbackSTSP(self):
        if self.STSP == 1:
            self.STSP = 0
            print("stop")
        elif self.STSP == 0:
            self.STSP = 1
            print("start")

    def CallbackShutdown(self):
        self.ShutdownVar = 1

    def callbackPumpSpeed(self):
        pass

if __name__ == "__main__":
    GPIOControllersystem().BuildGUI()

使用matplotlib.pyplot.imshow(),我可以看到树莓派pi相机和opencv之间的连接正在通过bcm2835-v4l2连接工作。 Image of frame captured using system but plotting with matplotlib

但是,当使用opencv.imshow()时,窗口将显示为黑盒,则不会显示任何内容。 enter image description here

更新:因此在测试时我发现执行以下任务时

import cv2
import matplotlib

camera = cv2.VideoCapture(0)
grab,frame = camera.read()
matplotlib.pyplot.imshow(frame)

grab,frame = camera.read()
matplotlib.pyplot.imshow(frame)

更新已解决,与主要问题无关。这是一个缓冲问题。似乎与cv2.imshow()没有关联

1 个答案:

答案 0 :(得分:0)

您应该在树莓上使用     从picamera导入PiCamera 为此结帐pyimagesearch