Sunday, September 27, 2015

Capture Raspberry Pi Camera image, display on OpenCV, Matplotlib PyPlot and Tkinter GUI

This example capture photo from Raspberry Pi Camera Module, and display with OpenCV, Matplotlib PyPlot and Tkinter GUI.


usage:
python pyCV_picam.py 1 - display wiyh OpenCV window
python pyCV_picam.py 2 - display with matplotlib
python pyCV_picam.py 3 - display with Tkinter

import picamera
import picamera.array
import time
import cv2
from matplotlib import pyplot as plt
import Tkinter 
import Image, ImageTk
import sys

def capturePiCam():
    with picamera.PiCamera() as camera:
        cap=picamera.array.PiRGBArray(camera)
        camera.resolution = (640, 480)
        camera.start_preview()
        time.sleep(3)
        camera.capture(cap,format="bgr")
        global img
        img =cap.array

#- display on OpenCV window -
def displayAtOpenCV():
    cv2.namedWindow('imageWindow', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('imageWindow',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

#- display with matplotlib -
def displayAtPyplot():
    plt.figure().canvas.set_window_title("Hello Raspberry Pi")
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
    plt.show()
    
#- display on Tkinter -
def displayAtThinter():
    root = Tkinter.Tk() 
    b,g,r = cv2.split(img) 
    img2 = cv2.merge((r,g,b))
    img2FromArray = Image.fromarray(img2)
    imgtk = ImageTk.PhotoImage(image=img2FromArray) 
    Tkinter.Label(root, image=imgtk).pack() 
    root.mainloop()

def displayUsage():
    print("usage: ")
    print("python pyCV_picam.py 1 - display wiyh OpenCV window")
    print("python pyCV_picam.py 2 - display with matplotlib")
    print("python pyCV_picam.py 3 - display with Tkinter")

if len(sys.argv) != 2:
    displayUsage()
    sys.exit()
    
opt = sys.argv[1]

if opt=="1":
    print("display wiyh OpenCV window")
    capturePiCam()
    displayAtOpenCV()
elif opt=="2":
    print("display with matplotlib")
    capturePiCam()
    displayAtPyplot()
elif opt=="3":
    print("display with Tkinter")
    capturePiCam()
    displayAtThinter()
else:
    displayUsage()
    



To use ImageTk in your python, refer to "Install PIL (with jpg supported) and ImageTk on Raspberry Pi/Raspbian".

5 comments:

Unknown said...

Thank you, this is good! I've have used OpenCv, but needed i more advanced GUI. I need to show a exit button, to exit the picture/window. Seems simple, but man.. It is a tough one :) Hopefully Tkinter will do the trick. Or matplotlab.. i have never heard of it. Thanks again, this gives me some work hours.

Erik said...

hello Simon Bjørkvig,

May be you can check my another post of Python to capture image from Pi Camera Module.

I have a tkButtonQuit button and quit() function.

Unknown said...

Thanks for your answer! I see you have a lot of great stuff in here. I couldnt do any overlay button with Tkinter on my cv2 image show. Have you tried working with pygame? I'm trying right now to capture a preview image with picamera, and display it with pygame. It should be interesting for your blog as well. Thanks again, keep blogging.

Erik said...

Simon Bjørkvig,

Sorry, I haven't tried pygame.

Thiago said...

Initially I would like to congratulate you for the help you have given to python users. have a question. My question is: I have two tkinter windows. One main and one secondary. The secondary window is called by a menu bar.
When second window is called, it is shown in the video captured by the picamera. I can not view the video.