ubuntu下编译 安装opencv和demo
[TOC]
编译
上官网:http://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
注意:cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
- 后面的两个小点,以及-D后加空格有问题就去掉
- make的过程可能会提示lib*** not found 这个时候可以apt-cache search来搜索关键字,然后sudo apt-get install lib(搜到的);详情参看https://huanglu.me/2016/12/29/ubuntu-opencv-complie/#make
设置环境
如果你的opencv不是安装在/usr/local/下,而是在自己的home目录下的话,还需要设置环境变量
vim ~/.bashrc
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:XXXX/lib/pkgconfig
export OpenCV_INCLUDE_DIRS=XXXX/inlcude:$OpenCV_INCLUDE_DIRS
其中XXXX代表opencv的安装路径
source ~/.bashrc使其生效
验证安装成功
在build目录下,进入../samples/cpp/example_cmake
执行以下三步:
cmake .
make
./opencv_example
eclipse配置opencv
新建c++工程
复制一下代码
#include "highgui.h"
int main(int argc, char** argv) {
IplImage* img = cvLoadImage("/home/liouville/devres/闲乐/切图/main_new.jpg");
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE);
cvShowImage( "Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow ("Example1");
}
配置
property -> c/c++ build ->Settings-> ToolSettings
选择c++ compile tab,include -> include
添加三个值:
<opencv_home>/include
<opencv_home>/include/opencv
<opencv_home>/include/opencv2
再选择c++ Linker -> Libraies,在search path中添加
<opencv_home/lib>
在Libraies中添加
opencv_highgui
opencv_core
编译,运行
-
编译过程中可能会出现 /usr/bin/ld: ./src/DI.o: undefined reference to symbol 的错误
尝试:
添加#include "opencv2/imgcodecs.hpp"
add linker -lopencv_imgcodecs -
运行过程中如果出现error while loading shared libraries: libopencv_core.so.2.4: cannot open shared obje
cd ld.so.conf.d/
vim opencv.conf
add path of libopencv_core.so.2.4 file in that file (without "libopencv_core.so.2.4").
sudo ldconfig -v
最后终于还是看到图像了
参考:
http://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
https://huanglu.me/2016/12/29/ubuntu-opencv-complie/#make
http://answers.opencv.org/question/46755/first-example-code-error/
https://ubuntuforums.org/showthread.php?t=2109967
http://blog.csdn.net/tingyuanss/article/details/45021159