linux下的OpenCV安装&学习笔记

54 篇文章 1 订阅
本文出自 “娜一片天空” 博客,请务必保留此出处http://wanwentao.blog.51cto.com/2406488/435867
linux下的OpenCV安装&学习笔记
 

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 \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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值