Ubuntu16.04 编译安装opencv

1. 下载源码

git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git

2. 下载依赖包

sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

sudo apt-get install build-essential qt5-default ccache libv4l-dev libavresample-dev libgphoto2-dev libopenblas-base libopenblas-dev doxygen pylint libvtk6-dev

3. 准备编译

cd opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/yy/opencv/opencv_contrib-master/modules -D OPENCV_ENABLE_NONFREE=ON ..
make -j4
sudo make install

编译中错误解决 https://blog.csdn.net/AlexWang30/article/details/99612188 https://blog.csdn.net/qq_38527408/article/details/97002419

4. 配置编译环境

~/.bashrc 文件中添加下面一行内容

export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:$PKG_CONFIG_PATH"

使环境变量生效 输入以下内容

source ~/.bashrc
sudo ldconfig

使用 pkg-config 进行测试验证

pkg-config --libs --cflags opencv4

5. 编写代码测试

mkdir opencv_test && cd opencv_test

创建opencv_test.cpp,并输入

include 

include "opencv2/xfeatures2d.hpp"


//#include

include "opencv2/core.hpp"

include "opencv2/imgproc.hpp"

include "opencv2/highgui.hpp"

include "opencv2/videoio.hpp"

include

using namespace std;
using namespace cv;
int main(){
Mat frame=imread("1.jpeg");
if(frame.empty())
{
cout<<" frame is empty"< return -1;
}
imshow("input",frame);
waitKey(0);
return 0;
}

你可能感兴趣的:(Ubuntu16.04 编译安装opencv)