zoukankan      html  css  js  c++  java
  • linux下的OpenCV安装&学习笔记

    http://www.linuxdiyf.com/viewarticle.php?id=20731

    (本想在fedora下安装编译的,但目前opencv官网、sourceforge等网站都无法访问下载,真的很烦人。等后续有时间再试试吧!)

     1. 首先获取ffmpeg,不装这个OpenCV打不开很多视频文件格式。

    很多人找不到怎么下载,其实之前ffmpeg可以通过cvs下载,不过最近他已经换成了更加强大的svn

    如何使用SVN我这里不再介绍,网上还有大量的安装和使用的文章可以借鉴,这里简单罗列几个SVN辅助的软件:

    SubVersion,从 http://subversion.tigris.org/ 下载,支持linux,我们这里就装这个

    TortoiseSVN,从 http://tortoisesvn.tigris.org/ 下载,是很不错的SVN客户端程序,为windows外壳程序集成到windows资源管理器和文件管理系统的Subversion客户端,用起来很方便,commit动作变得就像Winrar右键压缩一样方便。


    ok,那我们先装subversion,记住最好之前装过apr和apr-util,在apache.org网站能下到

    wget http://subversion.tigris.org/downloads/subversion-1.3.2.tar.gz
    tar zvxf subversion-1.3.2.tar.gz
    cd subversion-1.3.2
    ./configure --with-apr=/usr/local/apr-httpd --with-apr-util=/usr/local/apr-util-httpd/
    make
    make install


    到此,我们就可以通过svn命令获取最新的ffmpeg了

    svn checkout svn://svn.mplayerhq.hu/ffmpeg/trunk ffmpeg

    你会发现在你所在的目录,自动出现一个ffmpeg的目录,就是你下载的源代码。

    2.下面是ffmpeg的编译

    ./configure --enable-libogg --enable-shared --enable-gpl

    (一定要加上 --enable-shared,不然OpenCV找不到ffmpeg库)


    ================================================
    2006-10-3
    1.
    下载:opencv-0.9.9.tar.gz
    http://puzzle.dl.sourceforge.net ... opencv-0.9.9.tar.gz

    2.
    #tar -xzvf opencv-0.9.9.tar.gz

    3.
    #./configure --with-quicktime=no --with-ffmpeg

    (OpenCV默认用quicktime打开视频文件,我把他改成ffmpeg)
    4.
    #make
    5.
    #make install

    6.
    相关配置

    修改/etc/ld.so.conf

    添加一行/usr/local/lib

    # ldconfig (root用户)

    然后将/usr/local/lib/pkgconfig中的opencv.pc 拷贝到/usr/lib/pkgconfig中,(如果不做这步,根本编译不起)

    可以采用这个操作

    # cp /usr/local/lib/pkgconfig/opencv.pc /usr/lib/pkgconfig

    7.
    编辑opencv程序的方法

    以编辑cvtest.c文件为例子(因为highgui中采用了c++,所以一定要用g++编译才可以)

    A. g++ `pkg-config --cflags opencv` -o cvtest cvtest.c `pkg-config --libs opencv`

    B. 编译: g++ `pkg-config --cflags opencv` -c cvtest.c

    链接: g++ `pkg-config --libs opencv` -o cvtest cvtest.o

    =====================================================
    2006-10-3
    例子:

    /**************************************************
    * 背景建模,运动物体检测
    *
    **************************************************/

    /***********************************************************************
    * OpenCV example
    * Copyright (C) 2006 Shiqi Yu
    *
    * This program is free software; you can redistribute it and/or modify
    * it under the terms of the GNU General Public License as published by
    * the Free Software Foundation; either version 2 of the License, or
    * (at your option) any later version.
    *
    * This program is distributed in the hope that it will be useful,
    * but WITHOUT ANY WARRANTY; without even the implied warranty of
    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    * GNU General Public License for more details.
    *
    * You should have received a copy of the GNU General Public License
    * along with this program; if not, write to the Free Software
    * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
    ***********************************************************************/

    #include

    #include
    #include
    #include

    int main( int argc, char** argv )
    {
    //声明IplImage指针
    IplImage* pFrame = NULL;
    IplImage* pFrImg = NULL;
    IplImage* pBkImg = NULL;

    CvMat* pFrameMat = NULL;
    CvMat* pFrMat = NULL;
    CvMat* pBkMat = NULL;

    CvCapture* pCapture = NULL;

    int nFrmNum = 0;

    //创建窗口
    cvNamedWindow("video", 1);
    cvNamedWindow("background",1);
    cvNamedWindow("foreground",1);
    //使窗口有序排列
    cvMoveWindow("video", 30, 0);
    cvMoveWindow("background", 360, 0);
    cvMoveWindow("foreground", 690, 0);



    if( argc != 2 )
    {
    fprintf(stderr, "Usage: bkgrd <VIDEO_FILE_NAME> ");
    return -1;
    }

    //打开视频文件
    if( !(pCapture = cvCreateFileCapture(argv[1])))
    {
    fprintf(stderr, "Can not open video file %s ", argv[1]);
    return -2;
    }

    //逐帧读取视频
    while(pFrame = cvQueryFrame( pCapture ))
    {
    nFrmNum++;

    //如果是第一帧,需要申请内存,并初始化
    if(nFrmNum == 1)
    {
    pBkImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);
    pFrImg = cvCreateImage(cvSize(pFrame->width, pFrame->height), IPL_DEPTH_8U,1);

    pBkMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
    pFrMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);
    pFrameMat = cvCreateMat(pFrame->height, pFrame->width, CV_32FC1);

    //转化成单通道图像再处理
    cvCvtColor(pFrame, pBkImg, CV_BGR2GRAY);
    cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);

    cvConvert(pFrImg, pFrameMat);
    cvConvert(pFrImg, pFrMat);
    cvConvert(pFrImg, pBkMat);
    }
    else
    {
    cvCvtColor(pFrame, pFrImg, CV_BGR2GRAY);
    cvConvert(pFrImg, pFrameMat);
    //高斯滤波先,以平滑图像
    //cvSmooth(pFrameMat, pFrameMat, CV_GAUSSIAN, 3, 0, 0);

    //当前帧跟背景图相减
    cvAbsDiff(pFrameMat, pBkMat, pFrMat);

    //二值化前景图
    cvThreshold(pFrMat, pFrImg, 60, 255.0, CV_THRESH_BINARY);

    //进行形态学滤波,去掉噪音
    //cvErode(pFrImg, pFrImg, 0, 1);
    //cvDilate(pFrImg, pFrImg, 0, 1);

    //更新背景
    cvRunningAvg(pFrameMat, pBkMat, 0.003, 0);
    //将背景转化为图像格式,用以显示
    cvConvert(pBkMat, pBkImg);

    //显示图像
    cvShowImage("video", pFrame);
    cvShowImage("background", pBkImg);
    cvShowImage("foreground", pFrImg);

    //如果有按键事件,则跳出循环
    //此等待也为cvShowImage函数提供时间完成显示
    //等待时间可以根据CPU速度调整
    if( cvWaitKey(2) >= 0 )
    break;


    }

    }




    //销毁窗口
    cvDestroyWindow("video");
    cvDestroyWindow("background");
    cvDestroyWindow("foreground");

    //释放图像和矩阵
    cvReleaseImage(&pFrImg);
    cvReleaseImage(&pBkImg);

    cvReleaseMat(&pFrameMat);
    cvReleaseMat(&pFrMat);
    cvReleaseMat(&pBkMat);

    return 0;
    }

  • 相关阅读:
    python 抓取网页
    Vim XDebug调试PHP php远程调试
    10 条 nmap 技巧
    Linux修改文件及文件夹权限
    mysql 常用命令 汇总
    VS2010打开过多的IntelliTrace.exe进程导致虚拟内存不足的解决办法
    黄聪:MYSQL远程连接失败:ERROR 1130: mysql 1130连接错误的有效解決方法
    黄聪:WordPress搬家更换域名教程
    黄聪:使用 ALinq 实现 Linq to MySQL【转】
    黄聪:Filezilla 二进制上传设定
  • 原文地址:https://www.cnblogs.com/felixjia/p/3437201.html
Copyright © 2011-2022 走看看