首先,的步骤当然是下载和编译opencv,根据这位大神提供的思路http://www.samontab.com/web/2010/04/installing-opencv-2-1-in-ubuntu/,安装和编译还是比较轻松。主要有以下几步:
1、下载opencv解压opencv
2、进入解压后的目录,建立一个目录用来安放编译后的文件,目录的名字自己取,我去的名字是release
3、不记得哪个版本的opencv之后,编译前的配置不再用configure文件了。而改用cmake ,所有赶紧看看自己的系统中是否安装了cmake 没有的话,赶紧安装吧。
4、进去我们刚才创建的目录中,运行cmake
5、之后就是make && make install 了。
这里特别提示你需要安装一个库,不然在运行的时候你就会发现一个问题
OpenCV Error: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script) in cvNamedWindow, file /home/bush/OpencvSrc/OpenCV-2.3.0/modules/highgui/src/window.cpp, line 275
terminate called after throwing an instance of 'cv::Exception'
what(): /home/bush/OpencvSrc/OpenCV-2.3.0/modules/highgui/src/window.cpp:275: error: (-2) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function cvNamedWindow
问题的原因信息已经提示了要安装libgtk2.0-dev and pkg-config,这个不难,键入以下的命令就可以了。pkg-config已经在系统中安装了。
以上几个步骤对应的命令是
1、
wget http://nchc.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.3/OpenCV-2.3.0.tar.bz2;
tar -xvf OpenCV-2.3.0.tar.bz2
2、
cd OpenCV-2.3.0
sudo mkdir relese
cd relese
sudo apt-get install libgtk2.0-dev
3、这里就需要看看你的系统中是否安装了cmake了,如果没有安装,就请自行安装吧命令也很简单
sudo apt-get install cmake
我一般将安装和编译的过程写在脚本当中,
#########################################################################
# File Name: Install_cmake.sh
# Author: ma6174
# mail: ma6174@163.com
# Created Time: 2014年02月28日 星期五 13时32分53秒
#########################################################################
#!/bin/bash
##############################################
# FunctionName:echocolor
# Author: bush2582
# Role:the output will have color
# Created Time:
##############################################
echocolor( )
{
echo -e "\e[0;33m${@}\e[0m";
}
##############################################
# FunctionName:InstallGCC
# Author: bush2582
# Role:check g++ is already in system
# Created Time:
##############################################
function InstallGCC ( )
{
which g++;
if [ $? -eq 1 ];
then
read -p " g++ is not installed in this system do you want to install? (Y/y/n/N) " ynInstall_GCC;
if [ $ynInstall_GCC = "Y" ] || [ $ynInstall_GCC = "y" ] ;
then
#echo " now we will install g++ ";
echocolor "now we will install g++"
sudo apt-get install g++;
fi
else
echocolor "g++ already install in this system ";
fi
}
##############################################
# FunctionName:InstallCmake
# Author: bush2582
# Role:install Cmake
# Created Time:
##############################################
function InstallCmake( )
{
InstallGCC;
echocolor " now we will star the program that CMake is installed in this system ";
cd cmake-2.8.0;
./configure;
sudo make;
sudo make install;
exit 0;
}
#########################################################################
read -p "Do you want to download Cmake? (Y/y/n/N)?" downyn
if [ $downyn = "Y" ] || [ $downyn = "y" ];
then
wget http://down1.chinaunix.net/distfiles/cmake-2.8.0.tar.gz;
echocolor "now Staring Tar cmake";
tar -xvf cmake-2.8.0.tar.gz;
else
echocolor "now Staring Tar cmake";
tar -xvf cmake-2.8.0.tar.gz;
fi
read -p " Do you want to install camke now (Y/y/n/N)? " yn
if [ $yn = "y" ] || [ $yn = "Y" ] ;
then
InstallCmake;
else
exit 0;
fi
3、这里的/usr/local/opencv是我自定义在系统中安装的路径
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local/opencv -D BUILD_PYTHON_SUPPORT=ON ..
4、配置后自然就是make 和make install 了
sudo make
sudo make install
当然你可以选择脚本的方法来安装,
#######################################################################
# File Name: LoadOpencv.sh
# Author: ma6174
# mail: ma6174@163.com
# Created Time: 2014年02月28日 星期五 10时46分11秒
#########################################################################
#!/bin/bash
#--------------------------------------------#
# FunctionName:echocolor
# Author: bush2582
# Role:the output will have color
# Created Time:
#--------------------------------------------#
echocolor( )
{
echo -e "\e[0;33m${@}\e[0m";
}
read -p "Do you want to download Opencv-2.3.0? (Y/N)" DownLoadOpencv
if [ $DownLoadOpencv = "Y" ];
then
echocolor "now Staring downLoad Opencv2.3.0 ";
wget http://nchc.dl.sourceforge.net/project/opencvlibrary/opencv-unix/2.3/OpenCV-2.3.0.tar.bz2;
echocolor "Staring tar OpenCV-2.3.0.tar.bz2 "
tar -xvf OpenCV-2.3.0.tar.bz2
else
tar -xvf OpenCV-2.3.0.tar.bz2
echocolor "Staring tar OpenCV-2.3.0.tar.bz2 "
fi
cd OpenCV-2.3.0
sudo mkdir relese
cd relese
sudo apt-get install libgtk2.0-dev
read -p "Please input Dir which you want to install " Dir
sudo cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=$Dir -D BUILD_PYTHON_SUPPORT=ON ..
read -p "now we will star opencv make&&install in $Dir .Do you want to continue?( Y/N ) " GoOn
if [ $GoOn = "Y" ] ;
then
sudo make
sudo make install
else
exit 0;
fi
这里,opencv的安装算是完成了,不过要怎么用它编译我自己的源代码呢?
需要做一些配置工作了。需要做如下的几步工作
1.添加库路径
sudo vim /etc/ld.so.conf.d/opencv.conf /usr/local//opencv/lib(添加内容)
2.更新系统库
$sudo ldconfig
3.设置环境变量
export PKG_CONFIG_PATH=/usr/local/opencv/lib/pkgconfig:$PKG_CONFIG_PATH
#########################################################################
# File Name: ConfigureOpencv.sh
# Author: bush2582
# mail: bush2582@163.com
# Created Time: 2014年03月02日 星期日 15时45分54秒
#########################################################################
#!/bin/bash
ConfFileDir="/etc/ld.so.conf.d/opencv.conf"
OpencvLibDir="/usr/local/opencv/lib"
BashDir="/etc/bash.bashrc"
PKG="PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/opencv/lib/pkgconfig"
PKGExport="export PKG_CONFIG_PATH"
sudo file $ConfFileDir;
if [ $? -eq 1 ] ;
then
sudo touch $ConfFileDir;
fi
sudo grep -x $OpencvLibDir $ConfFileDir
if [ $? -eq 1 ] ;
then
sudo echo $OpencvLibDir >> $ConfFileDir;
fi
sudo ldconfig
sudo grep -x $PKG $BashDir
if [ $? -eq 1 ] ;
then
sudo echo $PKG >> $BashDir;
sudo echo $PKGExport >> $BashDir;
fi
sudo grep -x $PKG $BashDir
source $BashDir
这里说明几点:1、使用>> 可以在文件的最后累加文字。2、touch可以用来创建一个0字节的文件。3、grep -x 可以用来严格匹配文件中的数据。4、file 命令用来查看文件是否存在。(这里需要做完系统环境的配置后,重新打开一个命令行。关闭原来的命令行)
好了现在opencv的系统环境配置好了,那么我们怎么编译呢?当然用g++ ,不过我觉得输入一大堆命令太麻烦,索性写了个makefile 文件,
OBJS= test.o
CC=g++
INCLUDE= -I/usr/local/opencv/include -I/usr/local/opencv/include/opencv -I/home/bush/prj/linuxchuankou/include
LIB= -L /usr/local/opencv/lib `pkg-config --libs opencv`
Test:$(OBJS)
$(CC) -o Test $(OBJS) $(LIB)
test.o:test.cpp
$(CC) -c -g test.cpp $(INCLUDE)
clean:
-rm Test $(OBJS)
#`pkg-config --libs opencv` -I/usr/local/OpenCV-2.4.2/build/include/opencv2-lopencv_highgui -lopencv_core
这是我的测试源码
/*************************************************************************
> File Name: test.cpp
> Author: bush2582
> Mail: bush2582@163.com
> Created Time: 2014年03月02日 星期日 15时23分01秒
************************************************************************/
#include<iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;
int main ( )
{
IplImage * img=cvLoadImage("1.png",0);
cvNamedWindow("img",1 );
cvShowImage("img",img );
cvWaitKey(0);
cvDestroyWindow("img");
cvReleaseImage(&img);
return 0;
}
在命令行中键入make 就可以编译完成了哈。
以上就是opencv2.3.0的编译安装和使用过程,希望对你有帮助哦。
http://blog.youkuaiyun.com/bush2582/article/details/20299615