樹(shù)莓派使用的是3B+
1. 將攝像頭硬件正確的安裝到板子上
2. 檢查攝像頭是否正確安裝
$ vcgencmd get_camera
supported=1 detected=1
detected=1表示已經(jīng)檢測(cè)到攝像頭
3. 添加驅(qū)動(dòng)程序文件
sudo vim /etc/modules
在文件的最后一行添加bcm2835-v412;添加后的文件內(nèi)容為:
i2c-dev
bcm2835-v412
4. 運(yùn)行配置項(xiàng)設(shè)置程序
sudo raspi-config
選擇5 interfacting Options
進(jìn)入后,選擇P1 Camera
設(shè)置完后,重啟設(shè)備,
6. 抓取圖片
運(yùn)行以下命令抓取圖片
raspistill -o image.jpg -tl 5000
//raspistill -o image%d.jpg -rot 0 -w 300 -h 300 -t 5000 -tl 5000 -v
抓取的圖片效果為:
image
常用參數(shù)意義:
-v:調(diào)試信息查看
-w:圖像寬度
-h:圖像高度
-rot:圖像旋轉(zhuǎn)角度,只支持 0、90、180、270 度(這里說(shuō)明一下,測(cè)試發(fā)現(xiàn)其他角度的輸入都會(huì)被轉(zhuǎn)換到這四個(gè)角度之上)
-o:圖像輸出地址,例如image.jpg,如果文件名為“-”,將輸出發(fā)送至標(biāo)準(zhǔn)輸出設(shè)備
-t:獲取圖像前等待時(shí)間,默認(rèn)為5000,即5秒
-tl:多久執(zhí)行一次圖像抓取
7. 生成h246文件
使用raspivid指令來(lái)生成.h246的文件
raspivid -o imagechain.h264
這樣就會(huì)在當(dāng)前文件夾下面生成imagechain.h264的文件,默認(rèn)時(shí)間是5秒;可以設(shè)置錄制的視頻寬高和時(shí)間(raspivid --help)
"-t" 選項(xiàng)來(lái)設(shè)置你想要的長(zhǎng)度就行了(單位是毫秒)。
"-w" 和 "-h" 選項(xiàng)設(shè)置分辨率
8.安裝和使用opencv
安裝opencv
sudo apt-get install libopencv-dev
sudo apt-get install python-opencv
測(cè)試是否安裝成功
ython
Python 2.7.13 (default, Sep 26 2018, 18:42:22)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'2.4.9.1'
>>>
opencv測(cè)試相機(jī)預(yù)覽
opencv.py
# 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