1 CSI摄像头
1.1 picamera
树莓派自带的摄像头为CSI摄像头,在树莓派平台中,发行版的Raspbian默认安装了树莓派平台的picamera:
相关官方文档链接
执行该脚本将录制一段10秒的视频到本地:
import picameracamera = picamera.PiCamera()camera.resolution = (640, 480)camera.start_recording('my_video.h264')camera.wait_recording(10)camera.stop_recording()
但由于picamera输出的数据是RGB格式的,需要通过Numpy转换为BGR格式,代码如下:
import timeimport picameraimport numpy as npimport cv2with picamera.PiCamera() as camera: camera.resolution = (320, 240) camera.framerate = 24 time.sleep(2) image = np.empty((240 * 320 * 3,), dtype=np.uint8) # save as bgr format for OpenCV camera.capture(image, 'bgr') image = image.reshape((240, 320, 3)) cv2.imshow("img", image) if(cv2.waitKey(0) == ord('q')): exit(0)
当然,picamera 也提供了PIRGBArray对象,用来保存RGB 图像数据,下面将在capture_continuous函数中,不断地把RGB转换成BGR图像,供OpenCV显示,以形成视频流。
from picamera.array import PiRGBArrayfrom picamera import PiCameraimport timeimport cv2# initialize the camera and grab a reference to the raw camera capturecamera = PiCamera()camera.resolution = (640, 480)camera.framerate = 32rawCapture = PiRGBArray(camera)# allow the camera to warmuptime.sleep(0.1)# capture frames from the camerafor frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array # show the frame cv2.imshow("Frame", image) # prepare for net stream rawCapture.truncate(0) if(cv2.waitKey(1) == ord('q')): cv2.destroyAllWindows() break;
但由于picamera为树莓派或兼容树莓派平台专用的库,且仅能供树莓派使用,下面将介绍更为通用的使用方法。
1.2 V4L2
目前,CSI摄像头可通过picamera等库进行调用,但在Raspbian系统中,并不属于一个video设备而存在,若想将CSI摄像头作为一个视频设备文件使用,则要开启V4L2(Video for Linux 2)模块以开启:
开启方法如下:
sudo vim /etc/modules
在末尾添加一行:
bcm2835-v4l2
此时,CSI摄像头便成为了一个video设备,此时在终端执行:
ls -ltrh /dev/video*
便可查看到video设备,其中video0便是CSI摄像头:
该video设备可同时供Python与C++调用,具体使用请参考《树莓派OpenCV系列教程1:开发环境搭建》的例程。
1 CSI摄像头
1.1 picamera
树莓派自带的摄像头为CSI摄像头,在树莓派平台中,发行版的Raspbian默认安装了树莓派平台的picamera:
相关官方文档链接
执行该脚本将录制一段10秒的视频到本地:
import picameracamera = picamera.PiCamera()camera.resolution = (640, 480)camera.start_recording('my_video.h264')camera.wait_recording(10)camera.stop_recording()
但由于picamera输出的数据是RGB格式的,需要通过Numpy转换为BGR格式,代码如下:
import timeimport picameraimport numpy as npimport cv2with picamera.PiCamera() as camera: camera.resolution = (320, 240) camera.framerate = 24 time.sleep(2) image = np.empty((240 * 320 * 3,), dtype=np.uint8) # save as bgr format for OpenCV camera.capture(image, 'bgr') image = image.reshape((240, 320, 3)) cv2.imshow("img", image) if(cv2.waitKey(0) == ord('q')): exit(0)
当然,picamera 也提供了PIRGBArray对象,用来保存RGB 图像数据,下面将在capture_continuous函数中,不断地把RGB转换成BGR图像,供OpenCV显示,以形成视频流。
from picamera.array import PiRGBArrayfrom picamera import PiCameraimport timeimport cv2# initialize the camera and grab a reference to the raw camera capturecamera = PiCamera()camera.resolution = (640, 480)camera.framerate = 32rawCapture = PiRGBArray(camera)# allow the camera to warmuptime.sleep(0.1)# capture frames from the camerafor frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): image = frame.array # show the frame cv2.imshow("Frame", image) # prepare for net stream rawCapture.truncate(0) if(cv2.waitKey(1) == ord('q')): cv2.destroyAllWindows() break;
但由于picamera为树莓派或兼容树莓派平台专用的库,且仅能供树莓派使用,下面将介绍更为通用的使用方法。
1.2 V4L2
目前,CSI摄像头可通过picamera等库进行调用,但在Raspbian系统中,并不属于一个video设备而存在,若想将CSI摄像头作为一个视频设备文件使用,则要开启V4L2(Video for Linux 2)模块以开启:
开启方法如下:
sudo vim /etc/modules
在末尾添加一行:
bcm2835-v4l2
此时,CSI摄像头便成为了一个video设备,此时在终端执行:
ls -ltrh /dev/video*
便可查看到video设备,其中video0便是CSI摄像头:
该video设备可同时供Python与C++调用,具体使用请参考《树莓派OpenCV系列教程1:开发环境搭建》的例程。
举报