ubuntu安装并配置Clion+OpenCV

安装OpenCV3

Step 1:更新

1
2
sudo apt-get update
sudo apt-get upgrade

Step 2: 安装OS包

注意一定要确保都安装了,不然后面可能有问题

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
sudo apt-get remove x264 libx264-dev
# Install dependencies now
sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev
# Ubuntu 14.04
sudo apt-get install libtiff4-dev
# Ubuntu 16.04
sudo apt-get install libtiff5-dev
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils
# Optional dependencies
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

Step 3:下载OpenCV和OpenCV_contrib

直接从GitHub仓库上下载

1
2
3
4
5
6
7
8
9
10
11
# opencv
git clone https://github.com/opencv/opencv.git
cd opencv
git checkout 3.3.1
cd ..
# opencv_contrib
git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.1
cd ..

Step 4:编译和安装OpenCV及contrib模块

创建一个build的目录

1
2
3
cd opencv
mkdir build
cd build

cmake

1
2
3
4
5
6
7
8
9
10
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D WITH_TBB=ON \
-D WITH_V4L=ON \
-D WITH_QT=ON \
-D WITH_OPENGL=ON \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
-D BUILD_EXAMPLES=ON ..

编译

1
2
3
4
5
6
7
# find out number of CPU cores in your machine
nproc
# substitute 8 by output of nproc
make -j8
sudo make install
sudo sh -c 'echo "/usr/local/lib" >> /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig

Step 5:测试OpenCV3

这里运行一个用OpenCV写的红眼移除的程序来测试一下,下载RedEyeRemover.zip,并解压到任意目录下,记得切换到相应的目录下再进行编译和运行。

1
2
3
4
5
# compile
# There are backticks ( ` ) around pkg-config command not single quotes
g++ -std=c++11 removeRedEyes.cpp `pkg-config --libs --cflags opencv` -o removeRedEyes
# run
./removeRedEyes

在这一步如果前面的依赖没有装好,编译就会出错。我遇到了一个问题是这样的

1
2
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable

尝试了这个链接里的方法弄好了,有同样问题的可以尝试一下

1
apt-get install libopencv-dev

运行以后会弹出两个图片,一个是有红眼的,一个是去除以后的。这就代表安装完成了,接下来配置CLion的cmake

配置CLion

Step 1:新建工程

我新建了一个工程就叫opencv

Step2: 修改CMakeLists.txt文件

在CMakeLists里导入OpenCV相关的包,配置如下:

1
2
3
4
5
6
7
8
9
10
11
cmake_minimum_required(VERSION 3.6)
project(opencv)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(opencv main.cpp)
target_link_libraries(opencv ${OpenCV_LIBS})

最后两行是告诉Cmake,把main.cpp编译成了一个可执行文件opencv,然后让这个可以执行的文件去连接OpenCV的动态库。

Step 3:测试示例程序

在main.cpp里添加下面的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include "opencv2/core/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace std;
using namespace cv;
int main() {
Mat img = imread("../cat.jpg");
namedWindow("DisplayImage");
imshow("Cat", img);
waitKey();
return 0;
}

记得下载一个喜欢的猫咪图片放在刚才创建的project目录下,或者把路径换成绝对路径。运行就能看到一个萌萌的猫咪图片了,这时配置就完成了。