Ubuntu16.04 中,OpenCV 多版本的升级与安装.
1. OpenCV 编译安装
1.1. 查看 Ubuntu 当前的 OpenCV 版本
pkg-config --modversion opencv
# 2.4.9.1
在 /usr/bin
和 /usr/include
路径里包含了默认 OpenCV 版本的相关文件.
1.2. 依赖库安装
[1] - 官方必须依赖库
sudo apt-get update
sudo apt-get install cmake git
sudo apt-get install build-essential \
libgtk2.0-dev \
pkg-config \
libavcodec-dev \
libavformat-dev
[2] - 官方建议依赖库
sudo apt-get install python-dev \
libtbb2 \
libtbb-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
libjasper-dev \
libdc1394-22-dev
[3] - OPENGL 支持依赖库
sudo apt-get install freeglut3-dev \
mesa-common-dev \
libgtkglext1 \
libgtkglext1-dev
[4] - 视频解码支持依赖库
sudo apt-get install checkinstall \
yasm \
libgstreamer0.10-dev \
libgstreamer-plugins-base0.10-dev \
libv4l-dev \
libtbb-dev \
libqt4-dev \
libgtk2.0-dev \
libmp3lame-dev \
libtheora-dev \
libvorbis-dev \
libxvidcore-dev \
x264 \
v4l-utils
[5] - 其它可能依赖项
sudo apt-get install libgphoto2-dev \
libavresample-dev \
liblapacke-dev \
gtk+-3.0 \
libgtk-3-dev \
libeigen3-dev \
tesseract-ocr \
liblept5 \
leptonica-progs \
libleptonica-dev
1.3. OpenCV 源码下载
下载路径:
https://github.com/opencv/opencv/releases
1.4. OpenCV 3.4 编译安装
假设路径为 ROOT_DIR = /opt/opencv3.4/
cd $ROOT_DIR
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv34 ..
# 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 ..
make -j4
sudo make install
在编译安装时,ippicv
库的下载文件 - ippicv_2019_lnx_intel64_general_20180723.tgz
速度特别慢.
因此,采用离线下载文件,再进行编译.
ippicv_2019_lnx_intel64_general_20180723.tgz 文件下载:
百度网盘链接: https://pan.baidu.com/s/1mVTSdn6hTgo8P3ASKauyDg 提取码: hiux
假设 ippicv_2019_lnx_intel64_general_20180723.tgz
放置于 ROOT_DIR/3rdparty/ippicv/
路径,
并修改 ROOT_DIR/3rdparty/ippicv/ippicv.cmake
文件(47 行):
修改前:
41 set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
42 ocv_download(FILENAME ${OPENCV_ICV_NAME}
43 HASH ${OPENCV_ICV_HASH}
44 URL
45 "${OPENCV_IPPICV_URL}"
46 "$ENV{OPENCV_IPPICV_URL}"
47 "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"
48 DESTINATION_DIR "${THE_ROOT}"
49 ID IPPICV
50 STATUS res
51 UNPACK RELATIVE_URL)
修改后:
41 set(THE_ROOT "${OpenCV_BINARY_DIR}/3rdparty/ippicv")
42 ocv_download(FILENAME ${OPENCV_ICV_NAME}
43 HASH ${OPENCV_ICV_HASH}
44 URL
45 "${OPENCV_IPPICV_URL}"
46 "$ENV{OPENCV_IPPICV_URL}"
47 "file: ROOT_DIR/3rdparty/ippicv/ippicv_2019_lnx_intel64_general_20180723.tgz"
48 DESTINATION_DIR "${THE_ROOT}"
49 ID IPPICV
50 STATUS res
51 UNPACK RELATIVE_URL)
修改后,下载 ippicv
文件时,会自动从本地文件加载.
1.5. OpenCV 4.0.1 (最新版本) 编译安装
假设路径为 ROOT_DIR = /opt/opencv-4.0.1/
cd $ROOT_DIR
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D BUILD_NEW_PYTHON_SUPPORT=ON \
-D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.0.1 \
-D OPENCV_GENERATE_PKGCONFIG=ON ..
#cmake -D CMAKE_BUILD_TYPE=RELEASE \
# -D CMAKE_INSTALL_PREFIX=/usr/local/opencv-4.0.1 \
# -D WITH_CUDA=ON \
# -D OPENCV_EXTRA_MODULES_PATH=/opt/opencv_contrib/modules
# -D WITH_CUBLAS=1 \
# -D ENABLE_FAST_MATH=1 \
# -D CUDA_FAST_MATH=1 \
# -D WITH_CUFFT=ON \
# -D WITH_NVCUVID=ON \
# -D WITH_V4L=ON \
# -D WITH_LIBV4L=ON \
# -D WITH_OPENGL=ON \
# -D WITH_FFMPEG=ON \
# -D BUILD_TIFF=ON \
# -D WITH_LAPACK=OFF..
# -D INSTALL_C_EXAMPLES=ON \
# -D BUILD_EXAMPLES=ON ..
make
sudo make install
1.5.1 OpenCV 4.0.1 CUDA 编译问题
[1] - -DWITH_CUDA=ON
需要设置 -DOPENCV_EXTRA_MODULES_PATH=<opencv_contrib>/modules
[2] - 如果没有contrib
, make
的时候会出现错误:gpu_mat.cu:47:2: error: #error "opencv_cudev is required".
[3] - 在 opencv_contrib/modules/face
中会要下载 face_landmark_model.dat
文件(69M),也会遇到下载慢的问题,可以采用类似于 ippicv
库的处理方式.
face_landmark_model.mat 下载路径:
https://github.com/opencv/opencv_3rdparty/tree/contrib_face_alignment_20170818
在opencv_contrib/modules/face/CMakeLists.txt
文件:
set(__commit_hash "8afa57abc8229d611c4937165d20e2a2d9fc5a12")
set(__file_hash "7505c44ca4eb54b4ab1e4777cb96ac05")
ocv_download(
FILENAME face_landmark_model.dat
HASH ${__file_hash}
URL
"${OPENCV_FACE_ALIGNMENT_URL}"
"$ENV{OPENCV_FACE_ALIGNMENT_URL}"
"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${__commit_hash}/"
DESTINATION_DIR "${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/cv/face/"
ID "data"
RELATIVE_URL
STATUS res
)
if(NOT res)
message(WARNING "Face: Can't get model file for face alignment.")
endif()
将离线下载的 face_landmark_model.dat
放到 /opt/opencv-4.0.1/build/share/opencv4/testdata/cv/face/
路径中.
1.6. 环境变量设置
配置 OpenCV 编译环境:
编辑或新建 /etc/ld.so.conf.d/opencv.conf
:
sudo gedit /etc/ld.so.conf.d/opencv.conf
文件尾部添加如下内容:
/usr/local/opencv-4.0.1/lib
生效配置文件:
sudo ldconfig
sudo gedit ~/.bashrc
在该文件尾部添加如下行内容:
export PKG_CONFIG_PATH=/usr/local/opencv-4.0.1/lib/pkgconfig:$PKG_CONFIG_PATH
export LD_LIBRARY_PATH=/usr/local/opencv-4.0.1/lib:$LD_LIBRARY_PATH
环境变量生效:
source ~/.bashrc
查看opencv版本及库信息:
pkg-config --modversion opencv4
# 4.0.1
pkg-config --libs opencv4
pkg-config --cflags opencv4
1.7. OpenCV 的 cmake
使用
使用 cmake
工具时,在 CMakeLists.txt
文件中添加一下内容:
set(OpenCV_DIR /usr/local/opencv-4.0.1/share/OpenCV)
find_package(OpenCV 4.0.1 REQUIRED)
include_directories(SYSTEM ${OpenCV_INCLUDE_DIRS} /usr/local/opencv-4.0.1/include)
1.8. Python 接口
sudo ln -s \
/usr/local/opencv-4.0.1/lib/python2.7/dist-packages/cv2/python-2.7/cv2.so \
/usr/local/lib/python2.7/dist-packages/cv2/cv2.so
sudo ln -s \
/usr/local/opencv-4.0.1/lib/python3.5/dist-packages/cv2.cpython-35m-x86_64-linux-gnu.so \
/usr/local/lib/python3.5/dist-packages/cv2/cv2.cpython-35m-x86_64-linux-gnu.so
2. OpenCV 测试
以 opencv-4.0.1
为例.
假设项目路径为:/path/to/opencv_demo/
:
2.1. 例 - 显示图片
新建 opencv_test.cpp
文件:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
if ( argc != 2 )
{
cout << "Need to load an image ..." << endl;
return -1;
}
Mat image;
image = imread( argv[1], 1 );
if ( image.empty() )
{
cout<<"No image data!"<< endl;
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
新建 CMakeLists.txt
文件:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /usr/local/opencv-4.0.1/lib/cmake/opencv4)
find_package(OpenCV REQUIRED )
add_executable( opencv_test opencv_test.cpp )
target_link_libraries( opencv_test ${OpenCV_LIBS} )
执行:
cmake . -DCMAKE_CXX_FLAGS="-std=c++11" # opencv4.0 需要 c++ 11 支持
make
./opencv_test test.jpg
显示图片.
2.2. 例 - SURF 特征点检测
新建 surf_test.cpp
文件:
#include <iostream>
#include <vector>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat img1 = imread(argv[1], 1);
Mat img2 = imread(argv[2], 1);
if ((img1.data == NULL)||(img2.data == NULL))
{
cout << "No images exist" << endl;
return -1;
}
Ptr<Feature2D> surf = xfeatures2d::SURF::create(1000);
vector<KeyPoint> keypoints_1, keypoints_2;
Mat descriptors_1, descriptors_2;
surf->detectAndCompute(img1, Mat(), keypoints_1, descriptors_1 );
surf->detectAndCompute(img2, Mat(), keypoints_2, descriptors_2 );
drawKeypoints(img1, keypoints_1, img1);
drawKeypoints(img2, keypoints_2, img2);
namedWindow("img1",0);
resizeWindow("img1",500,500);
imshow("img1", img1);
namedWindow("img2",0);
resizeWindow("img2",500,500);
imshow("img2", img2);
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors_1, descriptors_2, matches );
double max_dist = 0; double min_dist = 100;
for( int i = 0; i < descriptors_1.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance <= max(2*min_dist, 0.02) )
{
good_matches.push_back( matches[i]);
}
}
Mat img_matches;
drawMatches( img1, keypoints_1, img2, keypoints_2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
namedWindow("Good Matches",0);
resizeWindow("Good Matches",800,800);
imshow( "Good Matches", img_matches );
for( int i = 0; i < (int)good_matches.size(); i++ )
{
printf( "-- Good Match [%d] Keypoint 1: %d -- Keypoint 2: %d \n",
i, good_matches[i].queryIdx, good_matches[i].trainIdx );
}
waitKey(0);
return 0;
}
新建 CMakeLists.txt
文件:
set(CMAKE_CXX_FLAGS "-std=c++11")
cmake_minimum_required(VERSION 2.8)
project( SURF_test )
set(OpenCV_DIR /usr/local/opencv-4.0.1/lib/cmake/opencv4)
find_package( OpenCV REQUIRED )
add_executable( SURF_test surf_test.cpp )
target_link_libraries( SURF_test ${OpenCV_LIBS} )
执行:
cmake .
make
./surf_test c1.jpg c2.jpg