前言
本文主要介绍如何在linux系统安装使用opencv.
具体步骤可参考opencv官网here.
步骤
编译源码之前需要安装相关依赖库;
1.下载源码;
2.解压源码;
3.配置cmake;
注意将配置的错误文件删除,以及cmake目录;
4.编译链接;
编译过程涉及到opencv的编译选项;
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so -D CUDA_ARCH_BIN=6.1 -D CUDA_ARCH_PTX="" -D WITH_CUDA=ON -D WITH_TBB=ON -D WITH_FFMPEG=ON -D BUILD_PYTHON_SUPPORT=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_OPENCV_PYTHON3=ON -D PYTHON_INCLUDE_DIR=/home/xxx/anaconda3/include/python3.8 -D PYTHON_PACKAGES_PATH=/home/xxx/anaconda3/lib/python3.8/site-packages -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GSTREAMER=ON -D WITH_OPENGL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda -D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs -D WITH_CUBLAS=ON -D WITH_NVCUVID=ON -D BUILD_opencv_cudacodec=ON -D OPENCV_DNN_CUDA=ON -D WITH_CUDNN=ON -D OPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=ON -D BUILD_EXAMPLES=ON ..
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -D CUDA_CUDA_LIBRARY=/usr/local/cuda/lib64/stubs/libcuda.so -D CUDA_ARCH_BIN=8.6 -D CUDA_ARCH_PTX="" -D WITH_CUDA=ON -D WITH_TBB=ON -D WITH_FFMPEG=ON -D BUILD_PYTHON_SUPPORT=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D BUILD_OPENCV_PYTHON3=ON -D PYTHON_INCLUDE_DIR=/usr/include/python3.8 -D PYTHON_LIBRARY=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_GSTREAMER=ON -D WITH_OPENGL=ON -D ENABLE_FAST_MATH=1 -D CUDA_FAST_MATH=1 -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_PC_FILE_NAME=opencv.pc -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda -D CMAKE_LIBRARY_PATH=/usr/local/cuda/lib64/stubs -D WITH_CUBLAS=ON -D WITH_NVCUVID=ON -D BUILD_opencv_cudacodec=ON -D OPENCV_DNN_CUDA=ON -D WITH_CUDNN=ON -D OPENCV_ENABLE_NONFREE=ON -D WITH_GSTREAMER=ON -D BUILD_EXAMPLES=ON ..
cmake -DPYTHON_INCLUDE_DIR=/usr/include/python3.8 -DPYTHON_LIBRARY=/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.so
5.安装库;
Required Packages
[compiler] sudo apt-get install build-essential
[required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
[optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
Bash
/opt$ sudo unzip opencv-3.3.1.zip
/opt$cd opencv-3.3.1
/opt/opencv-3.3.1$sudo mkdir build
/opt/opencv-3.3.1$cd build
/opt/opencv-3.3.1/build$sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
/opt/opencv-3.3.1/build$sudo make -j8(wait long time)
/opt/opencv-3.3.1/build$sudo make install
注意事项
1. 注意你要安装的opencv的版本以及cv12、cv14等;
2.首先需要安装opencv的依赖库,而且必须是在安装opencv之前优先安装依赖库,可参考here;
3.测试程序完成之后,需要CMakeLists.txt进行编译,可参考here;
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(test_opencv)
SET(OpenCV_DIR /lib/opencv2413/opencv-2.4.13.4/build)
INCLUDE_DIRECTORIES(/lib/opencv2413/opencv-2.4.13.4/build/include)
FIND_PACKAGE(OpenCV REQUIRED)
ADD_EXECUTABLE(test_opencv test_opencv.cpp)
TARGET_LINK_LIBRARIES(test_opencv ${OpenCV_LIBS})
FIND_PACKAGE可以设置为多个opencv版本之间的切换;
FIND_PACKAGE(OpenCV 3.3.1
example
find_package( OpenCV 3.1.0 REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
message("OpenCV_INCLUDE_DIRS: ${OpenCV_INCLUDE_DIRS}")
message("OpenCV_LIBS: ${OpenCV_LIBS}")
target_link_libraries( demo ${OpenCV_LIBS} )
4.其他多是调试代码;
如何卸载
cd build
sudo make uninstall
cd ..
sudo rm -r build
sudo rm -r /usr/local/include/opencv2 /usr/local/include/opencv /usr/include/opencv /usr/include/opencv2 /usr/local/share/opencv /usr/local/share/OpenCV /usr/share/opencv /usr/share/OpenCV /usr/local/bin/opencv* /usr/local/lib/libopencv*
cd ..
rm -r opencv3.3.1
添加opencv_contrib
to build with modules from opencv_contrib set OPENCV_EXTRA_MODULES_PATH to <path to opencv_contrib/modules/>
command
/opt/opencv-3.3.1/build$ sudo
error
CMake Error at cmake/OpenCVModule.cmake:296 (add_subdirectory):
The binary directory
/opt/opencv-3.3.1/build/modules/cudaarithm/.cudaarithm
is already used to build a source directory. It cannot be used to build
source directory
/opt/opencv-3.3.1/opencv_contrib/modules/cudaarithm
Specify a unique binary directory name.
Call Stack (most recent call first):
modules/CMakeLists.txt:7
都是类似以上的错误,过程参考here和here,还没有解决。
最后发现是系统有些文件不合适,删除之后再编译就ok啦。。。。发现这个不能解决根本问题。
编译通过但是编译测试程序的时候还是有问题,没有找到对应的文件,个人觉得应该注意opencv_contrib的版本here问题,正在测试。
最后还是两者的版本问题,两个版本一定要一致。
还有就是cmake那个步骤,这次使用的是绝对路径,不知道使用相对路径是否会出问题,试过的童鞋可以回答一下这个问题。
后来使用相同的版本安装编译之后,使用opencv_contrib中的samples中的例程进行测试,可以正常运行,即说明安装成功。
如果还有问题,可能是本身程序有什么BUG引起的,可以进行调试。
test code
/*----------------------------------------------
* Usage:
* ./test 0
*--------------------------------------------------*/
#include <opencv2/core/utility.hpp>
#include <opencv2/tracking.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <cstring>
#include "samples_utility.hpp"
using namespace std;
using namespace cv;
int main( int argc, char** argv ){
// show help
if(argc<2){
cout<<
" Usage: example_tracking_kcf <video_name>\n"
" examples:\n"
" example_tracking_kcf Bolt/img/%04.jpg\n"
" example_tracking_kcf faceocc2.webm\n"
<< endl;
return 0;
}
// create the tracker
Ptr<Tracker> tracker = TrackerKCF::create();
// set input video
std::string video = argv[1];
VideoCapture cap(0);
Mat frame;
// get bounding box
cap >> frame;
Rect2d roi= selectROI("tracker", frame, true, false);
//quit if ROI was not selected
if(roi.width==0 || roi.height==0)
return 0;
// initialize the tracker
tracker->init(frame,roi);
// do the tracking
printf("Start the tracking process, press ESC to quit.\n");
for ( ;; ){
// get frame from the video
cap >> frame;
// stop the program if no more images
if(frame.rows==0 || frame.cols==0)
break;
// update the tracking result
bool isfound = tracker->update(frame,roi);
if(!isfound)
{
cout << "The target has been lost...\n";
waitKey(0);
return 0;
}
// draw the tracked object
rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
// show image with the tracked object
imshow("tracker",frame);
//quit on ESC button
if(waitKey(1)==27)break;
}
}
View Code
总结
1.准备安装的opencv和opencv_contrib的版本一定要一致,切记!!!
2.按照官网的步骤一步步执行相应的操作,一定要注意cmake那个步骤。
3.安装完成之后可以使用源码中的一些samples代码进行测试。
4.测试的程序一定要没有bug哈~
参考
1.opencv官网;
2.CMakeLists编写;
4.opencv多个版本的cmake切换;
完