ubuntu16.04下配置Qt+OpenCV开发环境

系统:Ubuntu16.04 64位

Qt:Qt5.7.0+Qt Creator4.0.2

OpenCV:opencv-2.4.13


按照上面的地址和版本下载相应Linux 64位的安装包后,就可以开始了。

装系统就不说了。

一、安装qt

1、将下载的qt-opensource-linux-x64-5.7.0.run直接运行安装即可。

2、接下来CMake的时候需要qt4,所以还需要执行

$sudo apt-get install qt-sdk
二、编译安装opencv

1、安装cmake及一些依赖库

sudo apt-get install cmake
sudo apt-get install build-essential libavcodec-dev libavformat-dev libjpeg-dev libtiff4-dev libswscale-dev libjasper-dev

2、编译安装opencv

解压下载的opencv压缩包,然后在opencv目录下新建Release文件夹

原文:

You need to build the files once again.Locate your OpenCV folder. Create a new folder and name it as release. Enter into this folder. For example

cd /home/user_name/OpenCv
mkdir Release
cd Release

Now build using cmake with following command:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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_GTK=ON -D WITH_OPENGL=ON ..

Remember to put WITH_GTK=ON during cmake. After this step enter the command,

make
sudo make install

This should resolve your problem.If you have broken dependencies for libgtk2.0-dev, then install a fresh copy of libgtk2.0-dev using aptitude.

sudo aptitude install libgtk2.0-dev

如果没有aptitude,就先安装aptitude,再重新执行

sudo aptitude install libgtk2.0-dev

三、测试

1、打开qt->创建工程->Non-Qi Project->Plain C++ Applicaton->完成

2、在.pro文件中加入如下

INCLUDEPATH += /usr/local/include/opencv
LIBS += -lopencv_core \
-lopencv_imgproc \
-lopencv_highgui \
-lopencv_ml \
-lopencv_video \
-lopencv_features2d \
-lopencv_calib3d \
-lopencv_objdetect \
-lopencv_contrib \
-lopencv_legacy \
-lopencv_flann


3、测试代码如下

#include 
#include 
int main()
{
    IplImage *img = 0;
    img = cvLoadImage("pic.jpg",-1);    //pic.jpg在所建工程的目录下
    cvNamedWindow("test", 1);
    cvShowImage("test", img);
    cvWaitKey(0);
    return 0;
}

运行。

在ubuntu 下用Qt写opencv的程序运行出现error while loading shared libraries: libopencv_highgui.so.2.4 错误,是opencv的库路径没有设置好。


参考:http://wenku.baidu.com/link?url=3GwCKjVbDJn4o8jpPWG4Uljqplh_rKVF6-a6SIrSR7axoq-cimkx7L8ZZUnfLzyURYTYhh7XxSLSM6UTvXOK7of9P7Bp_Ht2R5mgAhaKpZG


 接下来要给系统加入opencv库的环境变量(也就是库存放的路径,注意不是解压路径)
     用vim打开/etc/ld.so.conf,注意要用sudo打开获得权限,不然无法修改,如:sudo vim /etc/ld.so.conf,在文件中加上一行 /usr/local/lib,/usr/local是opencv安装路径 就是makefile中指定的安装路径
     再运行sudo ldconfig,
     修改bash.bashrc文件,sudo gedit /etc/bash.bashrc
     在文件末尾加入:
     PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
     export PKG_CONFIG_PATH


然后source /etc/bash.bashrc


可以正常运行了。

成功!

ubuntu16.04下配置Qt+OpenCV开发环境_第1张图片



参考:

http://stackoverflow.com/questions/28776053/opencv-gtk2-x-error


你可能感兴趣的:(OpenCV)