樹莓派安裝Python-OpenCV

在樹莓派上安裝OpenCV,網絡上已經有很多教程方案,樹莓派上的系統爲2016年的jessie-raspbian,嘗試過源代碼安裝的方式,但編譯過程總是會出錯,多方查找仍解決不了。
放棄了源代碼安裝,發現有很便捷的方式,可以安裝Python版的OpenCV,只需2行命令。
在此之前請務必更新一下系統。

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv

到此已完成OpenCV的安裝,可以在Python中使用OpenCV了。

再編寫個調用樹莓派攝像頭採集視頻測的例程試一下:

# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

# allow the camera to warmup
time.sleep(0.1)

# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # grab the raw NumPy array representing the image, then initialize the timestamp
    # and occupied/unoccupied text
    image = frame.array

    # show the frame
    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF

    # clear the stream in preparation for the next frame
    rawCapture.truncate(0)

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break

祝學習愉快。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章