Ticker

6/recent/ticker-posts

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".

إرسال تعليق

0 تعليقات