嵌入式学习小组
直播中

王耀昱

7年用户 246经验值
私信 关注

摄像头如何使用?

本章将介绍计算机视觉中最核心传感器-摄像头的基本使用,主要讲解了CSI摄像头,USB摄像头,网络摄像头的基本使用。

回帖(3)

h1654155275.5708

2020-11-6 14:21:27
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:开发环境搭建》的例程。
举报

张秀珍

2020-11-6 14:21:39
USB摄像头

树莓派除了支持CSI摄像头外,同样也支持USB摄像头:

若采用USB摄像头,若摄像头本身免驱,接入树莓派后,将直接识别的video设备,在/dev下可找到对应的video设备,在中断,输入命令:
ls -ltrh /dev/video*即可查询到新接入的video设备。
但使用Python,C++使用摄像头,区分不同的摄像头时:
c++:
VideoCapture cap(0);python:
cap = cv2.VideoCapture(0)传入参数0代表使用CSI摄像头,传入其它参数代表使用其它video序号对应的USB摄像头。
具体使用请参考《1.OpenCV开发环境搭建》的例程。
举报

魏紫瑜

2020-11-6 14:21:56
网络摄像头

除了使用CSI摄像头,USB摄像头外,还可以将手机变成一个IP摄像头,具体方法如下:
在手机应用市场中,搜索这款名为IP摄像头的APP,图标如下:

安装好后,连接wifi,确保树莓派和手机处在同一局域网内,手机打开IP摄像头软件,同时点击打开IP摄像头服务器,即可将手机变成一个IP摄像头。如下图所示:

此时,在浏览器中打开该IP地址,输入用户名:admin,密码:admin,即可浏览摄像头画面:

此时说明手机成为了一个IP摄像头,此摄像头可供OpenCV调用。
OpenCV调用IP摄像头的源程序如下:
3.1 Python3

#!/usr/bin/env python'''Waveshare OpenCV Tutorial01_IP_Camera.pyA demo to show whether The OpenCV and IP camera is well installed'''import numpy as npimport cv2def main():    print("OpenCV Version:{}".format(cv2.__version__))    # 0: use CSI camera,1:use USB camera    ip_camera_url = 'http://admin:admin@192.168.10.215:8081'    cap = cv2.VideoCapture(ip_camera_url)    if(not cap.isOpened()):        print("can't open this camera")    while(True):        ret, FrameImage = cap.read()        if ret == True:            cv2.imshow('Camera Capture',FrameImage)            #Press Q to quit            if (cv2.waitKey(1)) == ord('q'):                cap.release()                break        else:            breakif __name__ == '__main__':    print(__doc__)    main()    # Release resource    cv2.destroyAllWindows()运行效果如下图所示:

3.2 C++

相应地,C++程序读取网络摄像头的程序如下:
#include#include#include#includeusing namespace std;using namespace cv;int main(){    cout << "Waveshare OpenCV Tutorial"<< endl;    cout << "00_Test_OpenCV"<>FrameImage;        imshow("Camera Capture",FrameImage);        //Press Q to quit        if(char(waitKey(1)) == 'q')        {            break;        }    }    return 0;}
举报

更多回帖

发帖
×
20
完善资料,
赚取积分