windows 源码编译 opencv-4.5.4 + tbb + ipp + openmp

Posted Arabican

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows 源码编译 opencv-4.5.4 + tbb + ipp + openmp相关的知识,希望对你有一定的参考价值。

1. 下载opencv源码

  • 官网下载需要版本的opencv源码

  • 解压到本地文件夹,进入解压后的opencv-4.5.4文件夹,新建source文件夹,将解压的内容放入source文件夹中,新建build文件夹

  • 下载opencv_contrib,并解压

2. 安装cmake

3. 安装Intel oneAPI Base Toolkit

  • Intel将TBB、IPP等加速库集成到了oneAPI库中,所以可以直接安装oneAPI

*按提示安装,勾选TBB,IPP等库

4. 安装Visual Studio 2019

  • 省略

5. 编译opencv

  • 打开cmake,设置好opencv源码路径和build路径

  • 点击configure,设置generator

  • configure完成后,设置with tbb

  • 设置with ipp
    IPP的cmake路径一般为C:/Program Files (x86)/Intel/oneAPI/ipp/2021.4.0/lib/cmake/ipp

  • 设置with openmp

  • 设置 BUILD_opencv_world

  • 点击Generate,等待完成

  • 在build文件夹中找到Opencv.sln,使用VS打开

  • 选择Debug模式,右键点击CMakeTargets中的ALL_BUILD,点击"生成",完成后右键点击INSTALL,然后点击"生成";

  • 选择Release模式,重复以上操作

  • 完成后可在文件夹中看到编译好的opencv

  • 设置环境变量(省略)

6. 速度测试

测试的CPU为8核16线程, 在开满16个线程后对比测试有加速的OpenCV和没有加速库的OpenCV。
测试了以下算子:

cv::cvtColor()
cv::erode()
cv::dilate()
cv::findContours()

代码如下:

void testSpeed()

    cv::Mat image = cv::imread("C:\\\\Workbench\\\\Data\\\\lenna.png");
    cv::Mat cvtImage;
    cv::Mat kernel_1 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
    cv::Mat kernel_2 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
    float t = 0.0;
    for (int i = 0; i < 100; ++i)
       
        auto start_time = std::chrono::high_resolution_clock::now();
        cv::cvtColor(image, cvtImage, cv::COLOR_BGR2GRAY);
        cv::erode(cvtImage, cvtImage, kernel_1, cv::Point(-1, -1), 1);
        cv::dilate(cvtImage, cvtImage, kernel_2, cv::Point(-1, -1), 3);
        std::vector<std::vector<cv::Point>> _contours;
        std::vector<cv::Vec4i> hierarchy;
        cv::findContours(cvtImage, _contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_NONE);
        auto end_time = std::chrono::high_resolution_clock::now();
        auto ms = std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time).count();
        t = t + (ms * 1.0 / 1e6);
    
    std::cout << "time: " << t << std::endl;


int main()

    std::thread threads[16];
    for (int i = 0; i < 16; ++i)
    
        threads[i] = std::thread(testSpeed);
    
    for (auto &t : threads)
    
        t.join();
    
    return 0;

测试结果:

  • 有TBB加速的OpenCV
[ INFO:0] global C:\\Softwares\\opencv-4.5.4\\source\\modules\\core\\src\\parallel\\registry_parallel.impl.hpp (96) cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(2, sorted by priority): TBB(1000); OPENMP(990)
[ INFO:0] global C:\\Softwares\\opencv-4.5.4\\source\\modules\\core\\include\\opencv2/core/parallel/backend/parallel_for.tbb.hpp (54) cv::parallel::tbb::ParallelForBackend::ParallelForBackend Initializing TBB parallel backend: TBB_INTERFACE_VERSION=12040
[ INFO:0] global C:\\Softwares\\opencv-4.5.4\\source\\modules\\core\\src\\parallel\\parallel.cpp (77) cv::parallel::createParallelForAPI core(parallel): using backend: TBB (priority=1000)
time: 0.990235
time: 0.990842
time: 0.994129
time: 0.992949
time: 1.00523
time: 0.995566
time: 1.01266
time: 1.00874
time: 1.02145
time: 1.02373
time: 1.0271
time: 1.03198
time: 1.02038
time: 1.03185
time: 1.03868
time: 1.05034
  • 没有TBB加速的OpenCV
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\parallel\\registry_parallel.impl.hpp (96) cv::parallel::ParallelBackendRegistry::ParallelBackendRegistry core(parallel): Enabled backends(3, sorted by priority): ONETBB(1000); TBB(990); OPENMP(980)
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load C:\\Softwares\\opencv\\build\\x64\\vc15\\bin\\opencv_core_parallel_onetbb454_64d.dll => FAILED
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_onetbb454_64d.dll => FAILED
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load C:\\Softwares\\opencv\\build\\x64\\vc15\\bin\\opencv_core_parallel_tbb454_64d.dll => FAILED
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_tbb454_64d.dll => FAILED
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load C:\\Softwares\\opencv\\build\\x64\\vc15\\bin\\opencv_core_parallel_openmp454_64d.dll => FAILED
[ INFO:0] global c:\\build\\master_winpack-build-win64-vc15\\opencv\\modules\\core\\src\\utils\\plugin_loader.impl.hpp (67) cv::plugin::impl::DynamicLib::libraryLoad load opencv_core_parallel_openmp454_64d.dll => FAILED
time: 2.38442
time: 2.43082
time: 2.43363
time: 2.42781
time: 2.46706
time: 2.47458
time: 2.48732
time: 2.49592
time: 2.50646
time: 2.51118
time: 2.50738
time: 2.51676
time: 2.50897
time: 2.52977
time: 2.50874
time: 2.55397
  • 结论:在线程资源占满的情况下,以上算子测得有TBB加速的Opencv比没有加速的Opencv要快出约2.5倍。

以上是关于windows 源码编译 opencv-4.5.4 + tbb + ipp + openmp的主要内容,如果未能解决你的问题,请参考以下文章

VScode结合OPenCV-pyhton实现视频流人脸检测

libusb系列-002-Windows下libusb源码编译

经验分享windows 源码编译 opencv

Windows环境IDEA下Ranger1.2.0源码编译

windows 平台使用 VS2017 编译 libevent 源码

netcdf源码在windows上的编译