树莓派_图像处理入门纪实(一)

树莓派_图像处理入门纪实(一)
先声明一点,凡是以纪实为文章标题的博客都是在强调:我只是向老板/领导/老师/同事/同学证明,我确实干活了。等把纪实的任务完成,会整理出一篇总结,把纪实中涉及的内容,用类似于教程的方法写下来。

前面已经准备好的事情:
- 设置好树莓派,升级相关软件
- 安装好OpenCV的相关库,我安装的是OpenCV3.1.0
- 确定摄像头和树莓派是兼容的

在OpenCV官网上找到了一些比较有用的信息。OpenCV使用视频,比较方便的方法是用VideoCapture类来实现。官网中给出了调用一个摄像头的例子:

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
    VideoCapture cap(0); // 使用默认摄像头
    if(!cap.isOpened())  // 检查摄像头是否成功打开
        return -1;
    Mat edges;
    namedWindow("edges",1); //建立一个窗口
    for(;;)
    {
        Mat frame;
        cap >> frame; // 获得摄像头的一帧
        //下面三句代码是利用canny算子进行边缘检测
        cvtColor(frame, edges, COLOR_BGR2GRAY);
        GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
        Canny(edges, edges, 0, 30, 3);
        imshow("edges", edges);  //在名字为“edges”的窗口显示
        if(waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor
    return 0;
}

树莓派_图像处理入门纪实(一)_第1张图片

从官网上可以看出,VideoCapture类由一下成员函数可以调用:

//filename表示要打开的文件名,index表示要打开的摄像头编号(实验了一下,树莓派基本是从0开始编号的)
        VideoCapture ()
        VideoCapture (const String &filename)
        VideoCapture (const String &filename, int apiPreference)
        VideoCapture (int index)
virtual     ~VideoCapture ()
virtual double  get (int propId)
virtual bool    grab ()
virtual bool    isOpened () const
virtual bool    open (const String &filename)
virtual bool    open (int index)
virtual bool    open (const String &filename, int apiPreference)
virtual VideoCapture &  operator>> (Mat &image)
virtual VideoCapture &  operator>> (UMat &image)
virtual bool    read (OutputArray image)
virtual void    release ()
virtual bool    retrieve (OutputArray image, int flag=0)
virtual bool    set (int propId, double value)

作为一个比较用于尝试的人,我还真的把上面的代码改造成双摄像头的,可惜失败了,内存消耗大的诡异。系统已经开始提示内存不足了。
目前发现的几个还没解决的问题:
- 无法成功使用双摄像头
- 图片颜色显示不正确
- 图片头尾位置不正确

你可能感兴趣的:(项目纪实)