EasyManua.ls Logo

SunFounder picar-x - Page 66

Default Icon
153 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
SunFounder picar-x
(continued from previous page)
import numpy as np
# Create the in-memory stream
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
camera.capture(stream, format='jpeg')
# Construct a numpy array from the stream
data = np.fromstring(stream.getvalue(), dtype=np.uint8)
# "Decode" the image from the array, preserving colour
image = cv2.imdecode(data, 1)
# OpenCV returns an array with data in BGR order. If you want RGB instead
# use the following...
image = image[:, :, ::-1]
To avoid the losses with JPEG encoding and decoding, use the classes in the picamera.array module. This will
also potentially increase the speed of image processing.
As OpenCV images are simply numpy arrays arranged in BGR order, the PiRGBArray class, and simply capture
with the ‘bgr’ format. Note: RGB data and BGR data are the same size and configuration, but have reversed color
planes.
PiRGBArray
import time
import picamera
import picamera.array
import cv2
with picamera.PiCamera() as camera:
camera.start_preview()
time.sleep(2)
with picamera.array.PiRGBArray(camera) as stream:
camera.capture(stream, format='bgr')
# At this point the image is available as stream.array
image = stream.array
Combined with the method of capturing timelapse sequences, these 3-dimensional RGB arrays are shown by OpenCV.
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
#init camera
with PiCamera() as camera:
camera.resolution = (640,480)
camera.framerate = 24
rawCapture = PiRGBArray(camera, size=camera.resolution)
for frame in camera.capture_continuous(rawCapture, format="bgr",use_video_
˓port=True): # use_video_port=True
img = frame.array
cv2.imshow("video", img) # OpenCV image show
rawCapture.truncate(0) # Release cache
# click ESC key to exit.
(continues on next page)
62 Chapter 4. Play with Python

Related product manuals