title: python, cv2.imshow(), raspberryPi and a black screen
body: <p>Currently trying write code with a GUI which will allow for toggling on/off image processing. Ideally the code will allow for turning on/off window view, real time image processing (pretty basic), and controlling an external board.</p>
  
  <p>The problem I'm having revolves around the <code>cv2.imshow()</code> function. A few months back I made a push to increase processing rates by switching from <code>picamera</code> to <code>cv2</code> where I can perform more complex computations like background subtraction without having to call python all the time. using the <strong>bcm2835-v4l2</strong> package, I was able to pull images directly from the picamera using <code>cv2</code>.</p>
  
  <p>fast forward 6 months and while trying to update the code, I find that the function <code>cv2.imshow()</code> does not display correctly anymore. I thought it might be a problem with <strong>bcm2835-v4l2</strong> but tests using matplotlib show that the connection is fine. it appears to have everything to do with <code>cv2.imshow()</code> or so I guess. </p>
  
  <p>I am actually creating a separate thread using the <code>threading</code> module for image capture and I am wondering if this could be the culprit. I don't think so though as typing in the commands</p>
  
  <pre><code>import cv2
  camera = cv2.VideoCapture(0)
  grabbed,frame = camera.read()
  cv2.imshow(frame)
  </code></pre>
  
  <p>produces the same black screen</p>
  
  <p>Down below is my code I am using (on the RPi3) and some images show the error and what is expected. </p>
  
  <p>as for reference here are the details about my system</p>
  
  <pre><code>Raspberry pi3
  raspi stretch
  python 3.5.1
  opencv 3.4.1
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>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 &gt; 126] = 255
                  pic2[pic2 &lt;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()
  </code></pre>
  
  <p>Using <code>matplotlib.pyplot.imshow()</code>, I can see that the connection between the raspberry pi camera and opencv is working through the bcm2835-v4l2 connection.
  <a href="https://i.stack.imgur.com/FHdP1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FHdP1.png" alt="Image of frame captured using system but plotting with matplotlib"></a></p>
  
  <p>However when using opencv.imshow() the window result in a blackbox, nothing is displayed.
  <a href="https://i.stack.imgur.com/cvQnN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cvQnN.png" alt="enter image description here"></a></p>
  
  <p>Update: so while testing I found out that when I perform the following task</p>
  
  <pre><code>import cv2
  import matplotlib
  
  camera = cv2.VideoCapture(0)
  grab,frame = camera.read()
  matplotlib.pyplot.imshow(frame)
  
  grab,frame = camera.read()
  matplotlib.pyplot.imshow(frame)
  </code></pre>
  
  <p>update was solved and not related to the main problem. This was a buffering issue. Appears to have no correlation to cv2.imshow()</p>