Saturday, September 26, 2015

Python capture picamera image, display on OpenCV and matplotlib


Python example to capture image from Raspberry Pi Camera Module with picamera, display on OpenCV and matplotlib.

This demo run on Windows log-in Raspberry Pi 2/Raspbian remotely, so the preview will not shown.


pyCV_picam.py
import picamera
import picamera.array
import time
import cv2
from matplotlib import pyplot as plt

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")
    img=cap.array
    
#- display on OpenCV window -
cv2.namedWindow('imageWindow', cv2.WINDOW_AUTOSIZE)
cv2.imshow('imageWindow',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

#- display with matplotlib
#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()




more:
~ Display on Tkinter GUI also.

No comments: