Ubuntu:编译安装OpenCV4

1. 进入官网,下载opencv4源码、contrib源码

各个版本链接:

contrib:https://github.com/opencv/opencv_contrib/releases

oepncv: https://opencv.org/releases.html


安装依赖

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的版本是否太低。



2. 按下面链接安装cuda和cudnn;

https://blog.csdn.net/sss_369/article/details/94591280

https://blog.csdn.net/sss_369/article/details/94592268

安装和Nvidia驱动相匹配的cuda和cudnn


Ubuntu:编译安装OpenCV4_第1张图片


3. 安装cmake:

https://blog.csdn.net/sss_369/article/details/94666494

4. cmake编译opencv:

选择opencv源文件所在路径;

选择输出build文件所在路径;

选择contrib_modues的路径;

勾选opencv_enable_nonfree;不然nonfree用不起来;

勾选cuda选项;


Ubuntu:编译安装OpenCV4_第2张图片


Ubuntu:编译安装OpenCV4_第3张图片


点击config,如无错误再勾选generate;

opencv4在编译时会下载一个名为ippicv_2019_lnx_intel64_general_20180723的文件;

导致编译时间长;


Ubuntu:编译安装OpenCV4_第4张图片

我采用先离线下载下来,编译时直接从本地加载;

5. 从本地加载 ippicv_2019_lnx_intel64_general_20180723:

在opencv4源码包里找到ippic.cmake文件;

/opencv_source/opencv/3rdparty/ippicv/ippicv.cmake


将47行的如下指令改为:

修改前:

"https://raw.githubusercontent.com/opencv/opencv_3rdparty/${IPPICV_COMMIT}/ippicv/"

修改后:file后是文件路径

Ubuntu:编译安装OpenCV4_第5张图片

6. 进入编译的build目录,进行安装:

make

sudo make install


7. opencv环境配置:

首先将OpenCV的库添加到路径,从而可以让系统找到

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

执行此命令后打开的可能是一个空白的文件,不用管,只需要在文件末尾添加

/usr/local/lib


Ubuntu:编译安装OpenCV4_第6张图片

执行如下命令使得刚才的配置路径生效

sudo ldconfig

配置bash

sudo gedit /etc/bash.bashrc

在末尾追加:

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

export PKG_CONFIG_PATH

保存,执行如下命令使得配置生效:

source /etc/bash.bashrc

更新:

sudo updatedb


至此,所有配置都已经完成。



8. opencv测试:

surf.cpp

#include

#include

#include

using namespace cv;

using namespace cv::xfeatures2d;        // 不要忘了导入扩展模块

using namespace std;

Mat src_img, gray_img;

const string output_name = "SURF特征检测";

int minHessian = 100;                // 定义SURF中的hessian阈值特征点检测算子

int max_value = 500;

void SURF_detect_func(int, void *)

{

// SURF特征检测

Ptr detector = SURF::create(minHessian);

vector keypoints;

detector->detect(gray_img, keypoints, Mat());      // 检测src_img图像中的SURF特征

  // 绘制关键点

Mat keypoint_img;

drawKeypoints(gray_img, keypoints, keypoint_img, Scalar::all(-1), DrawMatchesFlags::DEFAULT);  // Scalar::all(-1)这是一种技巧,就是当用一个负数作为关键点颜色,表示每次随机选取颜色。

imshow(output_name, keypoint_img);

}

int main()

{

src_img = imread("1.png");

if (src_img.empty())

{

printf("could not load the image...\n");

return -1;

}

namedWindow("原图", WINDOW_AUTOSIZE);

imshow("原图", src_img);

cvtColor(src_img, gray_img, COLOR_BGR2GRAY);

namedWindow(output_name, WINDOW_AUTOSIZE);

createTrackbar("hessian阈值", output_name,&minHessian, max_value, SURF_detect_func);

SURF_detect_func(0,0);

waitKey(0);

return 0;

}



CMakeLists.txt:

# 声明要求的 cmake 最低版本

cmake_minimum_required( VERSION 2.8 )

# 声明一个 cmake 工程

project( opencv_test )

# 设置编译模式

set( CMAKE_BUILD_TYPE "Debug" )

set(CMAKE_CXX_FLAGS  "-std=c++11")

find_package(OpenCV 4.1 REQUIRED)

# 添加一个可执行程序

# 语法:add_executable( 程序名 源代码文件 )

add_executable( test surf.cpp )

# 将库文件链接到可执行程序上

target_link_libraries( test ${OpenCV_LIBS} )



Ubuntu:编译安装OpenCV4_第7张图片

9. opencv_cuda_test测试是否可用gpu加速

main.cpp

//main.cpp

#include

#include

#include

#include

int main (int argc, char* argv[])

{

    try

    {

        /// 读取图片

        cv::Mat src_host = cv::imread("1.jpg", cv::IMREAD_GRAYSCALE);

        /// 定义GpuMat

        cv::cuda::GpuMat dst, src;

        /// 将主机内存的图像数据上传到GPU内存

        src.upload(src_host);

        /// 调用GPU的阈值函数(很多使用GPU加速的函数都和CPU版本的函数相同)

        cv::cuda::threshold(src, dst, 120, 255, cv::THRESH_BINARY);

        cv::Mat result_host;

        /// 从GPU上下载阈值化完成的图片

        dst.download(result_host);

        /// 显示

        cv::imshow("Result", result_host);

        cv::waitKey();

    }

    catch(const cv::Exception& ex)

    {

        std::cout << "Error: " << ex.what() << std::endl;

    }

    return 0;

}



CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

project(cuda_test)

#这一句解决 cannot find -lopencv_dep_cudart

set(CUDA_USE_STATIC_CUDA_RUNTIME ON)

set(CMAKE_CXX_STANDARD 11)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})

find_package(CUDA REQUIRED)

message(STATUS "CUDA版本: ${CUDA_VERSION}")

message(STATUS "    头文件目录:${CUDA_INCLUDE_DIRS}")

message(STATUS "    库文件列表:${CUDA_LIBRARIES}")

set(CUDA_NVCC_FLAGS -G;-g;-std=c++11) # nvcc flags

include_directories(${CUDA_INCLUDE_DIRS})

# 指定OpenCV安装路径来区分不同的OpenCV版本

set(OpenCV_DIR "/usr/local/share/OpenCV")

find_package(OpenCV REQUIRED)

set(OpenCV_LIB_DIR ${OpenCV_INSTALL_PATH}/lib)

message(STATUS "OpenCV版本: ${OpenCV_VERSION}")

message(STATUS "    头文件目录:${OpenCV_INCLUDE_DIRS}")

message(STATUS "    库文件目录:${OpenCV_LIB_DIR}")

message(STATUS "    库文件列表:${OpenCV_LIBS}")

include_directories(${OpenCV_INCLUDE_DIRS})

link_directories(${OpenCV_LIB_DIR})

CUDA_ADD_EXECUTABLE(main main.cpp)

target_link_libraries(main ${OpenCV_LIBS} ${CUDA_LIBRARIES})


参考

1.https://blog.csdn.net/sss_369/article/details/94755824

2.https://blog.csdn.net/xykenny/article/details/91956986

3. https://blog.csdn.net/whut54/article/details/88012854

4. https://www.jianshu.com/p/f646448da265

5. https://blog.csdn.net/wang3141128/article/details/80483459

6. https://www.douban.com/note/717360543/

7.https://blog.csdn.net/DumpDoctorWang/article/details/81032914

你可能感兴趣的:(Ubuntu:编译安装OpenCV4)