ubuntu opencv开发环境配置介绍

一、源码包下载

  • 源码包从链接:https://opencv.org/releases.html 下载
  • 解压源码包
    源码下载下来后是zip包,这里本人下载的是3.4.3的版本,则使用unzip opencv-3.4.3.zip 解压出来成下面样子。
  • 源码包文件列表
armwind@armwind-X555LD:~/data/opencv/opencv-3.4.3$ tree -L 1 .
.
├── 3rdparty
├── apps
├── cmake
├── CMakeLists.txt
├── CONTRIBUTING.md
├── data
├── doc
├── include
├── LICENSE
├── modules
├── platforms
├── README.md
└── samples

二、工具集安装

由于opencv使用cmake自动生成Makefile,所以需要安装cmake工具。安装后可能cmake工具版本太低,无法解析通过。就需要安装最低版本之后的cmake

sudo apt-get update
sudo apt-get install cmake
sudo apt-get install build-essential libgtk2.0-dev libavcodec-dev libavformat-dev libjpeg.dev libtiff4.dev libswscale-dev libjasper-dev

本人在安装时就遇到cmake版本过低问题。从网上下载了一个更高版本的cmake(https://cmake.org/files/v3.6/cmake-3.6.3.tar.gz).当然也可以一上来就检查一下cmake/OpenCVMinDepVersions.cmake,该文件内容如下所示,可以看到最低cmake版本是2.8.12.2

set(MIN_VER_CMAKE 2.8.12.2)                                                                                                                    
set(MIN_VER_CUDA 6.5)
set(MIN_VER_PYTHON2 2.6)
set(MIN_VER_PYTHON3 3.2)
set(MIN_VER_ZLIB 1.2.3)
set(MIN_VER_GTK 2.18.0)

新版本的cmake下载下来后,请按着README.md的步骤来编译安装就行了,这里就不多说了。

三、编译-安装opencv

1.创建编译目录

在编译之前,一定要先创建一个编译文件,用来存放中间文件。名字可以随便取,这里在源码根目录下创建一个release目录。当然从根目录下的CMakeLists.txt文件也可以看到,最好不要在根目录下直接编译。

   1 # ----------------------------------------------------------------------------                                                            
   2 #  Root CMake file for OpenCV
   3 #
   4 #    From the off-tree build directory, invoke:
   5 #      $ cmake <PATH_TO_OPENCV_ROOT>
   6 #
   7 # ----------------------------------------------------------------------------
   8 
   9 # Disable in-source builds to prevent source tree corruption.
  10 if(" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
  11   message(FATAL_ERROR "
  12 FATAL: In-source builds are not allowed.
  13        You should create a separate directory for build files.
  14 ")
  15 endif()

创建release后的目录列表。

armwind@armwind-X555LD:~/data/opencv/opencv-3.4.3$ tree -L 1 .
.
├── 3rdparty
├── apps
├── cmake
├── CMakeLists.txt
├── CONTRIBUTING.md
├── data
├── doc
├── include
├── LICENSE
├── modules
├── platforms
├── README.md
├── release
└── samples

2.使用cmake命令生成Makefile

cd release
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
##CMAKE_BUILD_TYPE:编译的版本,这里编译发布版本(Release).如果想编译Debug版本,就把Release替换成Debug
##CMAKE_INSTALL_PREFIX:生成动态库安装路径,可以自定义,但是一定要在系统中指定。

3.遇到的麻烦

编译过程中遇到错误,会在之前(release)创建目录下生成一个错误日志文件(release/CMakeFiles$/CMakeError.log)。可以从错误log日志发现解析过程中错误,下面是本人本地解析过程遇到的错误。

  7 Run Build Command:"/usr/bin/make" "cmTC_822de/fast"
  8 /usr/bin/make -f CMakeFiles/cmTC_822de.dir/build.make CMakeFiles/cmTC_822de.dir/build
  9 make[1]: Entering directory `/home/armwind/data/opencv/opencv-3.4.3/release/CMakeFiles/CMakeTmp'
 10 Building CXX object CMakeFiles/cmTC_822de.dir/cxx11.cpp.o
 11 /usr/bin/c++     -fPIE   -o CMakeFiles/cmTC_822de.dir/cxx11.cpp.o -c /home/armwind/data/opencv/opencv-3.4.3/cmake/checks/cxx11.cpp
 12 /home/armwind/data/opencv/opencv-3.4.3/cmake/checks/cxx11.cpp:4:2: error: #error "C++11 is not supported"
 13  #error "C++11 is not supported"
 14   ^
 15 /home/armwind/data/opencv/opencv-3.4.3/cmake/checks/cxx11.cpp: In function ‘int main()’:
 16 /home/armwind/data/opencv/opencv-3.4.3/cmake/checks/cxx11.cpp:11:10: error: ‘res’ does not name a type
 17      auto res = test();
 18           ^
 19 /home/armwind/data/opencv/opencv-3.4.3/cmake/checks/cxx11.cpp:12:12: error: ‘res’ was not declared in this scope
 20      return res;
 21             ^
 22 make[1]: *** [CMakeFiles/cmTC_822de.dir/cxx11.cpp.o] Error 1
 23 make[1]: Leaving directory `/home/armwind/data/opencv/opencv-3.4.3/release/CMakeFiles/CMakeTmp'
 24 make: *** [cmTC_822de/fast] Error 2
 25 
 26 ===== END =====

上面编译错误#error "C++11 is not supported"可以很清晰的看到,报的是编译器不支持c++11,针对这个问题,要么我们通过命令sudo apt-get install build-essential直接更新编译器,如果不行的话,只能通过手动安装g++4.8以上的版本了,具体如下所示:

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install g++-4.8
##创建软链接
sudo rm /usr/bin/g++
sudo ln -s /usr/bin/g++-4.8 /usr/bin/g++

光更新到g++4.8还不行,g++4.8默认不支持c++11,需要手动指定c++11编译规则,如下所示:

alias g++='g++-4.8 -std=c++11'

一开始本人犯了个低级错误,由于之前已经创建过g++软连接,这里就直接g++来指定,如下所示,但是一直不生效。后来才发现。
alias g++='g++ -std=c++11'
如果上面配置没问题的话,那么此时执行如下命令,则会生成Makefile
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..

4.编译过程

上面如果准备工作如果ok的话,那么在release目录下执行make,就会启动编译。

.....................................
[ 98%] Built target opencv_visualisation
Scanning dependencies of target opencv_interactive-calibration
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibController.cpp.o
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/calibPipeline.cpp.o
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/frameProcessor.cpp.o
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/main.cpp.o
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/parametersController.cpp.o
[ 98%] Building CXX object apps/interactive-calibration/CMakeFiles/opencv_interactive-calibration.dir/rotationConverters.cpp.o
[100%] Linking CXX executable ../../bin/opencv_interactive-calibration
[100%] Built target opencv_interactive-calibration
Scanning dependencies of target opencv_version
[100%] Building CXX object apps/version/CMakeFiles/opencv_version.dir/opencv_version.cpp.o
[100%] Linking CXX executable ../../bin/opencv_version
[100%] Built target opencv_version
armwind@armwind-X555LD:~/data/opencv/opencv-3.4.3/release$

上面是编译成功时的log,如果编译报错,请查看error log.编译成功的话,则执行sudo make install

5.链接环境配置

  • linux链接路径
    使用如下命令,在开始添加/usr/local/lib,因为opencv库就存放在/usr/local/lib目录下面。
    sudo vim /etc/ld.so.conf.d/opencv.conf
    保存当前opencv.conf,执行sudo ldconfig这一步千万不要忘了),自此编译环境准备好了。

  • OPENCV配置库路径和头文件路径
    其实这个已经由cmake给程序猿们做好了,在编译opencv库时已经做好具体请查看opencv编译目录,这里我的编译目录是release,配置文件在:
    opencv-3.4.3/release/OpenCVConfig.cmake
    此文件定义编译时需要的宏,详情请查看文件内容。

四、demo演示

  • CmakeList.txt编写
    其实下面的CmakeList文件也是从opencv demo中拷贝出来的。
# cmake needs this line
cmake_minimum_required(VERSION 2.8)

# Define project name
project(displayimage_demo_test)

# 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
#这一句就是查找opencv头文件路径和库路径,也不用添加到系统环境变量中去了
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(displayimage_demo displayimage.cpp)

# Link your application with OpenCV libraries
target_link_libraries(displayimage_demo ${OpenCV_LIBS})

  • 编译小脚本
    这个小编译脚本,主要是方便cmake中间变量删除使用,要不然还要手动指定删除中间文件。
BUILD_DIR = ./obj

all: build
	    cd $(BUILD_DIR);  make -j4
clean:
	    rm $(BUILD_DIR) -rf
build:
	    mkdir $(BUILD_DIR);cd $(BUILD_DIR); cmake ..

make clean
make

使用小Makefile之后主目录比较干净

使用小Makefile前
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
├── CMakeLists.txt
├── displayimage.cpp
└── Makefile

使用小Makefile后
├── CMakeLists.txt
├── displayimage.cpp
├── Makefile
└── obj

  • Cmake文件编译过程

– The C compiler identification is GNU 4.8.5
– The CXX compiler identification is GNU 4.8.5
– Check for working C compiler: /usr/bin/cc
– Check for working C compiler: /usr/bin/cc – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Detecting C compile features
– Detecting C compile features - done
– Check for working CXX compiler: /usr/bin/c++
– Check for working CXX compiler: /usr/bin/c++ – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Detecting CXX compile features
– Detecting CXX compile features - done
– Found OpenCV: /usr/local (found version “3.4.3”)
– OpenCV library status:
– version: 3.4.3
– libraries: opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab
– include path: /usr/local/include;/usr/local/include/opencv
– Configuring done
– Generating done
– Build files have been written to: /home/armwind/data/opencv/project/helloword

  • 实例代码
    opencv 原生代码中的demo有一个打开linux 本地摄像头的案例(opencv-3.4.3/samples/cpp/example_cmake)。目前本地没有摄像头,打开失败。但是程序可以运行,不过可以参考一下。
#include <cv.h> 
#include <highgui.h> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 
using namespace cv; 

int main( int argc, char** argv ) { 
	Mat image; 
	char *image_path = "/home/armwind/data/opencv/opencv-3.4.3/samples/data/lena.jpg";
	char *path = NULL;
	if(argc != 2)
		path = image_path;
	else 
		path = argv[1];
        printf("image path is %s \n", path);
	image = imread( path, 1 ); 
	if( !image.data ) { 
		printf( "No image data \n" ); 
		return -1; 
	} 
	namedWindow( "hello world", CV_WINDOW_AUTOSIZE ); 
	imshow( "hello world", image ); 
	waitKey(0); 
	return 0; 
}

  • 运行结果
    默认运行结果
    ./obj/displayimage_demo
    在这里插入图片描述
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值