首页 树莓派正文

树莓派OpenCV系列教程:摄像头的基本使用

云水 树莓派 2020-04-16 12:21:36 2565 0 树莓派OpenCV

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

1 CSI摄像头

首先将讲解CSI摄像头的使用:

1.1 picamera

树莓派自带的摄像头为CSI摄像头,在树莓派平台中,发行版的Raspbian默认安装了树莓派平台的picamera:

相关官方文档链接

执行该脚本将录制一段10秒的视频到本地:

import picamera

camera = 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:开发环境搭建》的例程。

2 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开发环境搭建》的例程。

3 网络摄像头

除了使用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 Tutorial
01_IP_Camera.py
A 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<iostream>#include<opencv2/opencv.hpp>#include<opencv2/highgui/highgui.hpp>#include<opencv2/imgproc/imgproc.hpp>using namespace std;using namespace cv;int main()
{    cout << "Waveshare OpenCV Tutorial"<< endl;    cout << "00_Test_OpenCV"<cout << "A demo to show whether The OpenCV and IP camera is well installed" <cout << endl;    cout << "OpenCV Version:" << CV_VERSION << endl;    const string ip_camera_url = "http://admin:admin@192.168.10.215:8081";
    VideoCapture cap(ip_camera_url);    if(!cap.isOpened())
    {        cout<<"can't open this camera"<return -1;
    }    
    Mat FrameImage;    while(1)
    {
        cap>>FrameImage;
        imshow("Camera Capture",FrameImage);        //Press Q to quit
        if(char(waitKey(1)) == 'q')
        {            break;
        }
    }    return 0;
}


版权声明

1.本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行测试。
2.本站资源仅供学习和交流使用,版权归资源原作者所有,请在下载后24小时之内自觉删除。
3.若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。
4.若内容涉及侵权或违法信息,请联系本站管理员进行下架处理,邮箱ganice520@163.com(本站不支持其他投诉反馈渠道,谢谢合作)

本文链接:http://apod.cc/index.php/post/348.html

发表评论

评论列表(0人评论 , 2565人围观)
☹还没有评论,来说两句吧...