Raspbian Ubuntu下安装OpenCV2.4.9的详细过程及碰到的问题和第一次程序的编译调试

来源:互联网 发布:naim muso淘宝假货 编辑:程序博客网 时间:2024/06/04 19:17
1.下载若干依赖项
    在开始安装之前,最好更新树莓派软件源。如果更新时间太长,请参考博文修改软件源网络地址——树莓派学习笔记——修改树莓派软件源
sudo apt-get update

    请依次安装以下依赖项,这些必须安装的依赖项来自于opencv官网的说明,在多数debian系统中都可以采用这种方式进行安装。其实在树莓派中build-essential已经默认安装,但是cmake等其他内容仍需要手动安装。
sudo apt-get install build-essential
sudo apt-get install cmake
sudo apt-get install libgtk2.0-dev
sudo apt-get install pkg-config
sudo apt-get install python-dev python-numpy
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev

2.获得源代码
    在树莓派用户目录中建立opencv文件夹
mkdir opencv

    通过sourceforge网站获取opencv源代码,本文选择2.4.9版本。下载过程可能比较缓慢,请耐心等待。
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.9/opencv-2.4.9.zip/download opencv-2.4.9.zip

    解压源代码,获得一个名为opencv-2.4.9的文件夹
unzip opencv-2.4.9.zip

3.编译源代码
    进入opencv源代码目录,在该目录中新建一个名为release的文件夹。
cd ~/opencv/opencv-2.4.9
mkdir release
cd release
    使用cmake工具在该文件夹中生成makefile文件,指定opencv的安装目录为/usr/local,opencv的源代码位于上一层目录。
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
    编译源代码并安装(编译源代码的时间非常的长,make之后我便睡去,第二天早上才执行install)
make(这个过程至少半小时,需要耐心等待)
sudo make install
    更新搜索动态链接库

sudo ldconfig

****cmake 当中遇到的问题*****************

Ubuntu 上cmake编译OpenCV,遇到一个错误:


usr/include/c++/6/cstdlib:75:25: fatal error: stdlib.h: 没有那个文件或目录

很是奇怪,百度搜索了下,得知是GCC6的一个改动造成的。

自己的gcc是6.3.0


解决办法:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. -DENABLE_PRECOMPILED_HEADERS=OFF



****make 当中遇到的问题*****************

1、/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘voidcv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&,cv::Mat&,cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:30:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]

if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:34:error: the compiler can assume that the address of ‘annotate_img’ will never be NULL [-Werror=address]if (&annotate_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘cv::ChamferMatcher::Match*cv::ChamferMatcher::Matching::localChamferDistance(cv::Point,cv::Mat&,cv::Mat&,cv::ChamferMatcher::Template*, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:25:error: the compiler can assume that the address of ‘orientation_img’ will never be NULL [-Werror=address]if (&orientation_img!=NULL) { ^/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1111:5:warning: nonnull argument ‘orientation_img’ compared to NULL [-Wnonnull-compare]if (&orientation_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:In member function ‘voidcv::ChamferMatcher::Matching::computeDistanceTransform(cv::Mat&,cv::Mat&,cv::Mat&, float, float, float)’:/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:1016:17:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare]if (&annotate_img!=NULL) { ^~/home/pikachu/dev/opencv-2.4.9/modules/contrib/src/chamfermatching.cpp:969:13:warning: nonnull argument ‘annotate_img’ compared to NULL [-Wnonnull-compare]if (&annotate_img!=NULL) { ^~cc1plus: some warnings being treated as errorsmodules/contrib/CMakeFiles/opencv_contrib.dir/build.make:158: recipe for target'modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o' failedmake[2]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/src/chamfermatching.cpp.o]Error1CMakeFiles/Makefile2:3195: recipe for target'modules/contrib/CMakeFiles/opencv_contrib.dir/all' failedmake[1]: *** [modules/contrib/CMakeFiles/opencv_contrib.dir/all]Error2

解决办法:

 release/modules/contrib/CMakeFiles/opencv_contrib.dir/flags.make文件 ,
删掉-Werror=address,然后重新
$make


解决完上面的问题就完成了OpenCV的交叉编译了。


接下来编译第一个程序:

首先:配置环境

将opencv的库加入到路径,从而让系统可以找到

sudo gedit /etc/ld.so.conf.d/opencv.conf

末尾加入/usr/local/lib,保存退出

sudo ldconfig    使配置生效

sudo gedit /etc/bash.bashrc 

末尾加入

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig

export PKG_CONFIG_PATH

保存退出

sudo source /etc/bash.bashrc  #使配置生效
(该步骤可能会报错找不到命令,原因是source为root命令

sudo passwd root(设置root密码,如果之前没有设置过)

输入新密码

su(进入root权限)

输入密码

source /etc/bash.bashrc

exit(退出root)


sudo updatedb #更新database



采取直接编译的方式

g++ helloworld.cpp -o helloworld `pkg-config --cflags --libs opencv` (注意ESC下面的`,不是单引号)(注意后面的依赖库)

编译通过后再运行

./helloworld hello.jpg

即可看到图片显示,安装完成。



阅读全文
0 0