Ubuntu 16.04LTS搭建OpenCV3.3.1(含contrib)

    很久不写博客了,照例来一句诗。“青春几何时,黄鸟鸣不歇。——(唐)李白《江南春怀》”

    前几天编译相关代码,结果出现许多错误,经过仔细查找发现确实缺少一些东西。那还能怎么办呢?重新编译呗,OpenCV+Contrib走起!

    准备材料,当然是把opencv-3.3.1.tar.gz和opencv_contrib-3.3.1.tar.gz下载下来啦!之后进行解压,将contrib文件夹移动到opencv中,并新建build文件夹,结果如图所示。

Ubuntu 16.04LTS搭建OpenCV3.3.1(含contrib)_第1张图片

    安装相关依赖:

sudo apt-get installbuild-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

    进入build文件夹,进行编译:

sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/home/dong/Library/opencv-3.3.1/opencv_contrib-3.3.1/modules/ .. 

   其中/usr/local是指将要install的路径,一般默认为/usr/local。其中OPENCV_EXTRA_MODULES_PATH是指opencv_contrib-3.2.0中modules所在的路径,后面的两点不可省略。

    不出意外的话,会在下面这个地方卡住。由于网络原因,出现ippicv_linux_20151201.tgz无法在终端下载的情况。

Ubuntu 16.04LTS搭建OpenCV3.3.1(含contrib)_第2张图片

    解决方法:可以先单独下载ippicv_linux_20151201.tgz之后,修改opencv里相关配置文件opencv_source/opencv/3rdparty/ippicv/ippicv.cmake将47行的 "https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"改为手动下载的文件的本地路径:"file:///home/lc/下载/"。

    修改后再次运行代码,出现以下问题:

Ubuntu 16.04LTS搭建OpenCV3.3.1(含contrib)_第3张图片

    解决方法:离线下载该文件放置在/home/dong/Library/opencv-3.3.1/.cache/tiny_dnn文件夹,命名为:adb1c512e09ca2c7a6faef36f9c53e59-v1.0.0a3.tar.gz

    xfeatures2d/boostdesc: Download: boostdesc_bgm_hd.i等文件下载失败,Failed to connect to raw.githubusercontent.com port 443: Connection refused,解决方法:

sudo vim /etc/hosts

    添加如下内容:

199.232.28.133 raw.githubusercontent.com

    接下来:

sudo make -j4
sudo make install

     环境配置添加库路径:

sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

    更新系统库:

sudo ldconfig

    配置bash

sudo gedit /etc/bash.bashrc  
//在末尾添加
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH

     保存退出,然后执行如下命令使得配置生效

sudo -s
source /etc/profile
source /etc/bash.bashrc
//激活配置然后更新database
updatedb  

     到这一步,安装完成!最后当然要测试一下啦!

      main.cpp:

//模板匹配进行图像查找   使用灰度图进行操作时间更短
#include
#include

using namespace std;
using namespace cv;

void match(Mat &Image, Mat &templateImage, int method)
{
    int result_cols = Image.cols - templateImage.cols + 1;
    int result_rows = Image.rows - templateImage.rows + 1;

    Mat result = Mat(result_cols, result_rows, CV_32FC1);

    matchTemplate(Image, templateImage, result, method);
    normalize(result, result, 0, 1, NORM_MINMAX);

    double minVal, maxVal;
    Point minLoc, maxLoc, matchLoc;
    minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat());

    switch (method)
    {
        case CV_TM_SQDIFF:
        case CV_TM_SQDIFF_NORMED:
            matchLoc = minLoc;
            break;
        default:
            matchLoc = maxLoc;
            break;
    }

    rectangle(Image, Rect(matchLoc, Size(templateImage.cols, templateImage.rows)), Scalar(0, 0, 255), 2, 8, 0);

    imshow("Image", Image);

}

int main()
{
    clock_t cstart, cend;
    Mat Image = imread("/home/dong/Pictures/3.jpg");          //灰度图需要的识别时间更短
    Mat templateImage = imread("/home/dong/Pictures/4.jpg");

    imshow("Image", Image);
    imshow("temp", templateImage);

    cstart = clock();
    match(Image, templateImage, CV_TM_SQDIFF);
    cend = clock();
    cout << "CV_TM_SQDIFF=" << cend - cstart << "ms" << endl;

    //cstart = clock();
    //match(Image, templateImage, CV_TM_SQDIFF_NORMED);
    //cend = clock();
    //cout << "CV_TM_SQDIFF_NORMED=" << cend - cstart << "ms" << endl;

    //cstart = clock();
    //match(Image, templateImage, CV_TM_CCOEFF);
    //cend = clock();
    //cout << "CV_TM_CCOEFF=" << cend - cstart << "ms" << endl;

    //cstart = clock();
    //match(Image, templateImage, CV_TM_CCOEFF_NORMED);
    //cend = clock();
    //cout << "CV_TM_CCOEFF_NORMED=" << cend - cstart << "ms" << endl;

    //cstart = clock();
    //match(Image, templateImage, CV_TM_CCORR);
    //cend = clock();
    //cout << "CV_TM_CCORR=" << cend - cstart << "ms" << endl;

    //cstart = clock();
    //match(Image, templateImage, CV_TM_CCORR_NORMED);
    //cend = clock();
    //cout << "CV_TM_CCORR_NORMED=" << cend - cstart << "ms" << endl;

    waitKey(0);
    return 0;
}

       CMakeLists.txt:

cmake_minimum_required(VERSION 3.15)
project(testcv)

set(CMAKE_CXX_STANDARD 14)
SET(SRC_LIST main.cpp)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
ADD_EXECUTABLE(demo1 ${SRC_LIST})
target_link_libraries(demo1 ${OpenCV_LIBS})

      两个文件放在同一文件夹下,新建build文件夹,并在build文件夹中:

cmake ..
make
./demo1

      运行结果如图所示:

Ubuntu 16.04LTS搭建OpenCV3.3.1(含contrib)_第4张图片

      如果成功了的话,那就恭喜啦!

参考:

1、源码编译opencv卡在IPPICV: Download: ippicv_2017u3_lnx_intel64_general_20170822.tgz解决办法

2、OpenCV - Linux(Ubuntu 16.04)中安装OpenCV + OpenCV_Contrib

3、Failed to connect to raw.githubusercontent.com port 443: Connection refused

4、ubuntu16.04下opencv3.2和opencv_contrib编译安装

5、在ubuntu16.04下安装opencv3.4.5(超详细)

6、Ubuntu16.04安装配置opencv3.4.3+opencv_contrib3.4.3

7、高版本OpenCV KCF调用演示注意事项

8、OpenCV之特征点模板匹配

你可能感兴趣的:(Ubuntu)