经过几天的努力,初步完成了对opencv2.3.0的安装和使用。记录下过程希望对他人有帮助。
首先,的步骤当然是下载和编译opencv,根据这位大神提供的思路,安装和编译还是比较轻松。主要有以下几步:
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 ..
推荐阅读:
Ubuntu 12.04 安装 OpenCV2.4.2 http://www.linuxidc.com/Linux/2012-09/70158.htm
CentOS下OpenCV无法读取视频文件 http://www.linuxidc.com/Linux/2011-07/39295.htm
Ubuntu 12.04下安装OpenCV 2.4.5总结 http://www.linuxidc.com/Linux/2013-06/86704.htm
Ubuntu 10.04中安装OpenCv2.1九步曲 http://www.linuxidc.com/Linux/2010-09/28678.htm
基于QT和OpenCV的人脸识别系统 http://www.linuxidc.com/Linux/2011-11/47806.htm
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 你也可以选择脚本,
这里说明几点: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的编译安装和使用过程,希望对你有帮助哦。
更多Ubuntu相关信息见Ubuntu 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=2
The latest Long Term Support version of Ubuntu(12.04 LTS) is out and a new version of OpenCV was released as well. This means that now is a great opportunity to update my OpenCV installation guide to the latest versions, Ubuntu 12.04 LTS and OpenCV 2.4.1.
We are going to setup OpenCV to use the new Qt highgui interface, which is much better than the simple highgui interface. Also, we will install OpenCV with support for OpenGL, as well as reading and writing videos, access to a webcam, Python, C and C++ interfaces, and Intel Threading Building Blocks (TBB).
OK, so the first step is to make sure that everything in the system is updated and upgraded:
sudo apt-get update sudo apt-get upgrade
Now, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal:
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev
Time to get the OpenCV 2.4.1 source code:
cd ~ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.1/OpenCV-2.4.1.tar.bz2 tar -xvf OpenCV-2.4.1.tar.bz2 cd OpenCV-2.4.1
Now we have to generate the Makefile by using cmake. In here we can define which parts of OpenCV we want to compile. Since we want to use Python, TBB, OpenGL, Qt, work with videos, etc, here is where we need to set that. Just execute the following line at the terminal to create the appropriate Makefile. Note that there are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent).
mkdir build cd build cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Also, check that Python, TBB, OpenGL, V4L, OpenGL and Qt are detected.
If anything is wrong, go back, correct the errors by maybe installing extra packages and then run cmake again. You should see something similar to this:
Now, you are ready to compile and install OpenCV 2.4.1:
make sudo make install
Now you have to configure OpenCV. First, open the opencv.conf file with the following code:
sudo gedit /etc/ld.so.conf.d/opencv.conf
Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:
/usr/local/lib
Run the following code to configure the library:
sudo ldconfig
Now you have to open another file:
sudo gedit /etc/bash.bashrc
Add these two lines at the end of the file and save it:
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig export PKG_CONFIG_PATH
Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.
Now you have OpenCV 2.4.1 installed in your computer with Python, TBB, OpenGL, video, and Qt support.
Check out the cool Qt interface which provides image viewing capabilities with zoom, as well as the ability to save the current image with just one click.
If you zoom in enough, you can see the RGB (or intensity) values for each pixel.
Now let’s build some samples included in OpenCV:
cd ~/OpenCV-2.4.1/samples/c chmod +x build_all.sh ./build_all.sh
Now we are ready to run the examples:
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg
./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg
~/OpenCV-2.4.1/build/bin/grabcut ~/OpenCV-2.4.1/samples/cpp/lena.jpg
~/OpenCV-2.4.1/build/bin/calibration_artificial
python ~/OpenCV-2.4.1/samples/python2/turing.py