环境:VMwareWorkstation15.5.1+Ubuntu16.04+OpenCV-4.4.0+CMake3.5

1.下载OpenCV源码包

首先去OpenCV官网https://opencv.org/下载源码包,我这里选择的是OpenCV-4.4.0

opencv 掩膜调试 opencv mask_linux


opencv 掩膜调试 opencv mask_vim_02

2.将下载的压缩包放到虚拟机中并进行解压

opencv 掩膜调试 opencv mask_opencv 掩膜调试_03


直接右击opencv-4.4.0,提取到此处(Extract Here)

opencv 掩膜调试 opencv mask_opencv 掩膜调试_04

3.新建build文件夹并进行cmake编译

然后打开解压后的opencv-4.4.0文件夹,新建一个build文件夹,进入到build文件夹下,打开终端,运行如下命令:

sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/user/local ..

opencv 掩膜调试 opencv mask_linux_05


运行后如图:

opencv 掩膜调试 opencv mask_vim_06


然后接着输入sudo make命令,

sudo make

opencv 掩膜调试 opencv mask_opencv 掩膜调试_07


opencv 掩膜调试 opencv mask_OpenCV_08


大概过去了半个小时。。。

opencv 掩膜调试 opencv mask_OpenCV_09


之后接着在终端输入如下命令:

sudo make install

opencv 掩膜调试 opencv mask_linux_10


opencv 掩膜调试 opencv mask_opencv 掩膜调试_11

4.进行验证

至此,Ubuntu下的OpenCV已安装成功,可以通过查看OpenCV版本来验证一下,命令行输入如下指令:

pkg-config --modversion opencv

opencv 掩膜调试 opencv mask_opencv_12

如果报如下这个错误

opencv 掩膜调试 opencv mask_OpenCV_13

  • 网上的解决方法如下(虽然当然解决了问题,但在最后进行demo测试的时候还是报错了,不推荐!):
cd /usr/local/lib

sudo mkdir pkgconfig

cd pkgconfig

sudo touch opencv.pc

sudo vim opencv.pc

首先建一个新的文件夹:pkgconfig
然后cd到这个文件夹下,新建opencv.pc文件;
再打开opencv.pc文件,文件内输入:

prefix=/usr/local
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: opencv
Description: The opencv library
Version:4.4.0
Cflags: -I${includedir}/opencv4
Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann  -lopencv_core

其中version是你的opencv版本(这里我的是4.4.0)。
最后:wq保存退出。

vim中的保存退出命令是wq。具体方法:首先按i键进入插入模式;然后编辑文件;接着按ESC键进入命令行模式;最后输入【:wq】即可保存文件并退出。

opencv 掩膜调试 opencv mask_opencv_14

  • 出现上面那个错误,(那可能是当你安装opencv的时候,有一些依赖包可能没有安装,但是没有报错,opencv也安装成功了,但是某些功能没法实现。)
    具体解决方法:
sudo apt-get install libgtk2.0-dev

sudo apt-get install pkg-config

执行完上述两步之后,再重新按照之前的步骤进行安装OpenCV,把解压后的opencv-4.4.0文件夹删除,重新进行解压,然后新建build文件夹,在build文件夹目录下重新进行make。
之后再次输入pkg-config --modversion opencv指令,就解决了,后面的demo测试也能顺利运行。

参考:http://linuxkiss.com/linux/162.html

5.demo测试

Using OpenCV with gcc and CMake:https://docs.opencv.org/4.4.0/db/df5/tutorial_linux_gcc_cmake.html

  • 首先新建一个DisplayImage文件夹,然后新建DisplayImage.cpp文件,
mkdir DisplayImage

touch DisplayImage.cpp

vim DisplayImage.cpp
  • 文件中输入如下代码:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 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文件夹文件夹下新建CMakeLists.txt文件,
touch CMakeLists.txt

vim CMakeLists.txt
  • 在文件中输入如下内容:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
set(OpenCV_DIR /home/aubo/opencv-4.4.0/build)
find_package( OpenCV REQUIRED PATHS ${OPENCV_DIR}/build/ NO_DEFAULT_PATH)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

这里和官网写的不一样,否则会报如下的错误:

参考:

opencv 掩膜调试 opencv mask_linux_15

  • 之后接着在命令行输入(自己在当前工程项目文件夹中准备一张图片):
cmake .

make

./DisplayImage 11.jpg
  • demo测试成功运行!

opencv 掩膜调试 opencv mask_opencv_16


opencv 掩膜调试 opencv mask_linux_17

如果需要linux下安装opencv4.4.0(带opencv_contrib-4.4.0)
参考:

Installation in Linux:
https://docs.opencv.org/4.4.0/d7/d9f/tutorial_linux_install.html