ubuntu 16.0.4 opencv 3.4.1 + opencv_contrib 3.4.1 cmake-gui 安装

https://blog.csdn.net/jindunwan7388/article/details/80397700

下载和添加依赖包

1、首先更新 apt-get,在安装前最好先更新一下系统,不然有可能会安装失败。在终端输入:

sudo apt-get update
sudo apt-get upgrade

2、接着安装官方给的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

3、接下来安装CMake-gui,在终端输入

sudo apt-get install cmake-qt-gui

下载安装包:
opencv 3.4.1 Source code(zip)
https://github.com/opencv/opencv/releases
23

opencv_contrib 3.4.1
https://github.com/opencv/opencv_contrib/releases 3.4.1.zip

配置OpenCV
解压 两个压缩包,然后将 opencv_contrib 放到 opencv 3.4.1 里面,双击进入解压出来的OpenCV3.2.0文件夹,右键打开终端(或者在别处打开终端,通过输入cd opencv3.2.0进入当前目录下),然后依次输入(不要忘了第三行的最后的空格和两个点):

mkdir build
cd build 
cmake-gui .. 

3、然后会弹出CMake的图形化界面,在上方的两个路径里面,选择好代码所在文件夹的路径和要安装的路径

4 点击左下方的Configure按钮,选择Unix Makefiles,选择Use default native compilers(默认),然后点击Finish

需要下载一些文件,要等待一段时间。然后CMake即载入默认配置,如下图所示:
窗口的中间部分即配置列表,这里和使用cmake命令直接生成makefile文件一致的。正如上文所述,这里通过图形界面的方式来进行配置,更加直观方便。
6、这里需要对两个地方进行修改:

(1)在CMAKE_BUILD_TYPE 值处输入RELEASE,其他保持不变(如果已经存在就不必修改)。
下方的CMAKE_INSTALL_PREFIX显示了默认的安装目录,这里我修改为: /usr/local/opencv3 (安装成功后,这个文件夹下面是有东西的,如果没有东西,安装没成功)
生成makefile文件最后执行 make install时就会安装到这个目录,这里可以根据个人需求更改。

在OPENCV_EXTRA_MODULES_PATH处,选择输入目录

然后选择opencv_contrib-3.4.1文件夹中的modules文件夹,注意,不是只选中opencv_contrib-3.4.1文件夹就好了,需要选中里面的modules文件夹! 如下图所示,然后点Choose。
注:OPENCV_EXTRA_MODULES_PATH 是用来指定要编译的扩展模块,其中包括OpenCV_contrib模块。

OPENCV_ENABLE_NONFREE 选中(SURF 专利问题)

点击Generate生成配置文件,这一步应该比较快就完成了。

8、接着,在build目录下打开终端,输入

make
sudo make install

配置环境变量
1、安装成功后还需要设置opencv的环境变量。打开文件:

sudo gedit /etc/ld.so.conf.d/opencv.conf

2、将以下内容添加到最后:

/usr/local/lib

3、接下来配置库:

sudo ldconfig

4、更改环境变量:

sudo gedit /etc/bash.bashrc

5、在文件后添加:

PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig 
export PKG_CONFIG_PATH

6、保存退出,在运行下面的例程之前,需要重新开启终端来使配置生效。

下面是 SURF 特征点检测 测试代码



#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

int main()
{
    Mat img1 = imread("/home/zhangjun/Pictures/bookobject.jpg",1);
    Mat img2 = imread("/home/zhangjun/Pictures/bookscene.jpg",1);
    if ((img1.data == NULL)||(img2.data ==NULL))
    {
        cout << "No exist" << endl;
        return -1;
    }
    Ptr surf = xfeatures2d::SURF::create(1000);

    vector keypoints_1, keypoints_2;
    Mat descriptors_1, descriptors_2;

    surf->detectAndCompute(img1, Mat(), keypoints_1, descriptors_1 );
    surf->detectAndCompute(img2, Mat(), keypoints_2, descriptors_2 );
    drawKeypoints(img1, keypoints_1, img1);
    drawKeypoints(img2, keypoints_2, img2);

    namedWindow("img1",0);
    resizeWindow("img1",500,500);
    imshow("img1", img1);

    namedWindow("img2",0);
    resizeWindow("img2",500,500);
    imshow("img2", img2);

    FlannBasedMatcher matcher;
    std::vector< DMatch > matches;
    matcher.match( descriptors_1, descriptors_2, matches );
    double max_dist = 0; double min_dist = 100;

    for( int i = 0; i < descriptors_1.rows; i++ )
    { double dist = matches[i].distance;
      if( dist < min_dist ) min_dist = dist;
      if( dist > max_dist ) max_dist = dist;
    }
    printf("-- Max dist : %f \n", max_dist );
    printf("-- Min dist : %f \n", min_dist );

    std::vector< DMatch > good_matches;
    for( int i = 0; i < descriptors_1.rows; i++ )
    { if( matches[i].distance <= max(2*min_dist, 0.02) )
      { good_matches.push_back( matches[i]); }
    }

    Mat img_matches;
    drawMatches( img1, keypoints_1, img2, keypoints_2,
                 good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
                 vector(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

    namedWindow("Good Matches",0);
    resizeWindow("Good Matches",800,800);
    imshow( "Good Matches", img_matches );

    for( int i = 0; i < (int)good_matches.size(); i++ )
    { printf( "-- Good Match [%d] Keypoint 1: %d  -- Keypoint 2: %d  \n",
              i, good_matches[i].queryIdx, good_matches[i].trainIdx ); }

    waitKey(0);
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.4)


project( SURF_test )

set(OpenCV_DIR "/home/zhangjun/SoftWare/opencv-3.4.1/build")

find_package( OpenCV REQUIRED )

add_executable( SURF_test surf_test.cpp )

target_link_libraries( SURF_test ${OpenCV_LIBS} )

编译运行:
cmake .
make
./SURF_test

你可能感兴趣的:(opencv)