分享

linux下的OpenCV安装&学习笔记(修订版,解决无法打开视频文件问题)

 寒山月 2011-07-26
1. 首先获取ffmpeg,不装这个OpenCV打不开很多视频文件格式。

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

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

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

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


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

wget http://subversion./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库)
#make
#make install


================================================
2006-10-3
1.
    下载:opencv-0.9.9.tar.gz
        http://puzzle.dl./sourceforge/opencvlibrary/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 <stdio.h>

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

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>\n");
      return -1;
    }

  //打开视频文件
  if( !(pCapture = cvCreateFileCapture(argv[1])))
    {
      fprintf(stderr, "Can not open video file %s\n", 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;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多