1、安装依赖项
sudo apt-get install build-essential
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
若libgtk2.0-dev安装报错,提示附加依赖项 > 2.0等类似问题,请检查ubuntu源是否有问题,可以试试国内的阿里源或其它高校的ubuntu源。
2、下载opencv3.4.0源代码
在opencv官网下载src源码,https://opencv.org/releases.html,选择3.4.0版本的Sources,下载完成后,解压至本地目录。
3、编译源码
cd ~/opencv
mkdir release
cd release
sudo 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 ON ..
sudo make -j8
sudo make install
(下面这一步添加环境变量可以不用操作,不加也可以编译,已验证过)
sudo gedit /etc/ld.so.conf.d/opencv.conf
加入/usr/local/lib(有的是/usr/local/lib/x86_64-linux-gnu)
sudo ldconfig
sudo gedit /etc/bash.bashrc
在最后一行添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
sudo source /etc/bash.bashrc
可以用 pkg-config opencv --cflags --libs
查看安装结果
opencv -I/usr/local/include -L/usr/local/lib/x86_64-linux-gnu -lopencv_dnn -lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc -lopencv_flann -lopencv_core
4、测试
include <stdio.h>
include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
Mat image;
image = imread( "lena.jpg", 1 );
if ( !image.data )
{
printf("No image data \n");
return -1;
}
namedWindow("Display Image", WINDOW_AUTOSIZE );
imshow("Display Image", image);
waitKey(0);
return 0;
}
文件名保存为DisplayImage.cpp。
创建CMakeLists.txt文件,
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
保存,执行
cmake .
make
./DisplayImage
显示结果
Screenshot from 2018-01-06 19:48:35.png
网友评论