OpenCV官网:https://opencv.org/
github地址: https://github.com/opencv/opencv/releases/tag/3.4.0
首先看官方的安装说明,这个非常有必要,官方写的比较好,基本按照官方说明进行操作就可以了。
官方编译安装说明:https://docs.opencv.org/3.4.1/d7/d9f/tutorial_linux_install.html
OpenCV必要的工具包,如下图
安装依赖
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
检查自己安装的gcc、cmake的版本是否太低。
解压源码
tar xvzf opencv-3.4.0.tar.gz
在源码外创建一个编译文件夹,不要直接在源码文件夹里面操作,以防乱了源码,对后续开发不利
mkdir build
进入创建的文件夹
cd build/
执行cmake,生成makefile
cmake ../opencv-3.4.0 -DWITH_GTK_2_X=ON -DCMAKE_INSTALL_PREFIX=/usr/local
编译
make
安装
sudo make install
查看安装结果
pkg-config --cflags --libs opencv
安装完后配置
sudo gedit /etc/ld.so.conf.d/opencv.conf
打开文件可能是空白的,没有关系,继续下一步,在文件末尾添加
/usr/local/lib
使配置生效
sudo ldconfig
添加环境变量
sudo gedit /etc/bash.bashrc
在文件最后添加
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
使环境变量生效
source /etc/bash.bashrc
写一个简单的测试程序 displayImage.cpp
#include
using namespace cv;
int main( int argc, char** argv )
{
Mat image;
image = imread( argv[1], 1 );
if( argc != 2 || !image.data )
{
printf( "No image data \n" );
return -1;
}
namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
imshow( "Display Image", image );
waitKey(0);
return 0;
}
编写 CMakeLists.txt
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Define project name
project(display_image)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(CMAKE_VERSION VERSION_LESS "2.8.11")
# Add OpenCV headers location to your include paths
include_directories(${OpenCV_INCLUDE_DIRS})
endif()
# Declare the executable target built from your sources
add_executable(display_image displayImage.cpp)
# Link your application with OpenCV libraries
target_link_libraries(display_image ${OpenCV_LIBS})
执行cmake,生成makefile
cmake .
编译
make
执行
./display_image yichan.jpeg
如下图