我需要哪个 opencv cuda 库来运行一个简单的 opencv cuda 程序?

Posted

技术标签:

【中文标题】我需要哪个 opencv cuda 库来运行一个简单的 opencv cuda 程序?【英文标题】:Which opencv cuda library do I need to run a simple opencv cuda program? 【发布时间】:2015-08-05 12:42:50 【问题描述】:

我在 win7 x64 上使用 Visual Studio 2010 构建了支持 cuda 的 opencv。为了运行一个简单的 opencv cuda 程序,我编写了这段代码并将输出发布的 exe 文件放在所有 lib 和 dll 文件附近的opencv\built\install\x64\vc10\bin

但是当我运行 exe 文件时,它会产生错误No CUDA support (The library is compiled without CUDA support)

这是我的代码:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <string>
#include <chrono>
#include <ctime>
#include <opencv2/core/cuda.hpp>

using namespace cv;
using namespace cv::cuda;
using namespace std;

string source_image_addr, gray_image_addr="", source_image_name="";

int convert2gray()

    Mat image;
    GpuMat d_image, d_gray_image;
    image = imread((char *)source_image_addr.c_str(), IMREAD_COLOR);

    if(image.empty())
    
        cout<<"Could not open or find the image"<<std::endl;
        return -1;
    

    CV_Assert(image.depth() != sizeof(unsigned char));
    std::chrono::time_point<chrono::system_clock> start, end;
    start = chrono::system_clock::now();
    d_image.upload(image);
    cuda::cvtColor(d_image, d_gray_image, CV_RGB2GRAY);
    Mat gray_image(d_gray_image);
    end = chrono::system_clock::now();
    chrono::duration<double> elapsed = end - start;
    cout<<"for image["<<image.cols<<"*"<<image.rows<<"] elapsed = "<<elapsed.count()<<endl;

    //namedWindow("Gray Window", WINDOW_AUTOSIZE);
    //imshow("Gray Window", gray_image);
    imwrite((char *)gray_image_addr.c_str(), gray_image);


int main(int argc, char** argv)

    if(argc >= 2)
    
        source_image_addr = argv[1];
        source_image_name = source_image_addr.substr(0, source_image_addr.find_last_of("."));

        if(argc >= 3)
            gray_image_addr = argv[2];
        else
            gray_image_addr = source_image_name + "-gray.jpg";

        if(!convert2gray())
            return -1;
    
    else
    
        cout<<"Usage: convert2gray ImageAddress [gray_image_addr]"<<endl;
        return -1;
    

    //waitKey(0);
    return 0;

我已将opencv_world300.dll 放在同一个文件夹中。

编辑: 这是std::cout &lt;&lt; cv::getBuildInformation() &lt;&lt; std::endl;的输出:

  videoio: Removing WinRT API headers by default

General configuration for OpenCV 3.0.0 =====================================
  Version control:               3.0.0

  Platform:
    Host:                        Windows 6.1 AMD64
    CMake:                       2.8.11.2
    CMake generator:             Visual Studio 11 Win64
    CMake build tool:            C:/Windows/Microsoft.NET/Framework/v4.0.30319/MSBuild.exe
    MSVC:                        1700

  C/C++:
    Built as dynamic libs?:      YES
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/x86_amd64/cl.exe  (ver 17.0.50727.1)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /MP8  /MD /O2 /Ob2 /D NDEBUG  /Zi
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR /EHa  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /wd4251 /wd4324 /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 11.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /MD /O2 /Ob2 /D NDEBUG  /Zi
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi    /MP8  /D_DEBUG /MDd /Zi /Ob0 /Od /RTC1 
    Linker flags (Release):      /machine:x64   /INCREMENTAL:NO  /debug
    Linker flags (Debug):        /machine:x64   /debug /INCREMENTAL 
    Precompiled headers:         YES
    Extra dependencies:
    3rdparty dependencies:       ippicv

  OpenCV modules:
    To be built:                 hal core flann imgproc ml photo video imgcodecs shape videoio highgui objdetect superres ts features2d calib3d stitching videostab world
    Disabled:                    -
    Disabled by dependency:      -
    Unavailable:                 cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev java python2 viz

  Windows RT support:            NO

  GUI: 
    QT:                          NO
    Win32 UI:                    YES
    OpenGL support:              NO
    VTK support:                 NO

  Media I/O: 
    ZLib:                        build (ver 1.2.8)
    JPEG:                        build (ver 90)
    WEBP:                        build (ver 0.3.1)
    PNG:                         build (ver 1.5.12)
    TIFF:                        build (ver 42 - 4.0.2)
    JPEG 2000:                   build (ver 1.900.1)
    OpenEXR:                     build (ver 1.7.1)
    GDAL:                        NO

  Video I/O:
    Video for Windows:           YES
    DC1394 1.x:                  NO
    DC1394 2.x:                  NO
    FFMPEG:                      YES (prebuilt binaries)
      codec:                     YES (ver 55.18.102)
      format:                    YES (ver 55.12.100)
      util:                      YES (ver 52.38.100)
      swscale:                   YES (ver 2.3.100)
      resample:                  NO
      gentoo-style:              YES
    OpenNI:                      NO
    OpenNI PrimeSensor Modules:  NO
    OpenNI2:                     NO
    PvAPI:                       NO
    GigEVisionSDK:               NO
    DirectShow:                  YES
    Media Foundation:            NO
    XIMEA:                       NO
    Intel PerC:                  NO

  Other third-party libraries:
    Use IPP:                     8.2.1 [8.2.1]
         at:                     C:/buildslave64/win64_amdocl/master_PackSlave-win64-vc11-shared/opencv/3rdparty/ippicv/unpack/ippicv_win
    Use IPP Async:               NO
    Use Eigen:                   NO
    Use TBB:                     NO
    Use OpenMP:                  NO
    Use GCD                      NO
    Use Concurrency              YES
    Use C=:                      NO
    Use pthreads for parallel for:
                                 NO
    Use Cuda:                    NO
    Use OpenCL:                  YES

  OpenCL:
    Version:                     dynamic
    Include path:                C:/buildslave64/win64_amdocl/master_PackSlave-win64-vc11-shared/opencv/3rdparty/include/opencl/1.2 C:/Program Files (x86)/AMD/clAmdFft/include C:/Program Files (x86)/AMD/clAmdBlas/include
    Use AMDFFT:                  YES
    Use AMDBLAS:                 YES

  Python 2:
    Interpreter:                 c:/python27_x64/python.exe (ver 2.7.5)

  Python 3:
    Interpreter:                 NO

  Python (for build):            c:/python27_x64/python.exe

  Java:
    ant:                         C:/Program Files (x86)/apache-ant/bin/ant.bat (ver 1.9.2)
    JNI:                         C:/Program Files/Java/jdk1.6.0_45/include C:/Program Files/Java/jdk1.6.0_45/include/win32 C:/Program Files/Java/jdk1.6.0_45/include
    Java wrappers:               NO
    Java tests:                  NO

  Matlab:
    mex:                         NO

  Tests and samples:
    Tests:                       NO
    Performance tests:           NO
    C/C++ Examples:              NO

  Install path:                  C:/buildslave64/win64_amdocl/master_PackSlave-win64-vc11-shared/build/install

  cvconfig.h is in:              C:/buildslave64/win64_amdocl/master_PackSlave-win64-vc11-shared/build/opencv_build
-----------------------------------------------------------------


Could not open or find the image

【问题讨论】:

【参考方案1】:

OpenCV 预计算二进制文件是在不支持 cuda/GPU 的情况下编译的(这也是错误消息告诉您的内容 => openCV 不支持 cuda 编译)。

您必须编译自己的 OpenCV(使用 CMake 很容易完成)以获得 cuda 支持(激活 WITH_CUDA=ON 并设置 cuda 工具包目录路径)!

看看: http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html#installation-by-making-your-own-libraries-from-the-source-files

您可能还需要处理不同的 cuda 平台: http://docs.opencv.org/modules/gpu/doc/introduction.html

如果您不确定您的库是如何编译的,则可以使用std::cout &lt;&lt; cv::getBuildInformation() &lt;&lt; std::endl; 在运行时打印您的 OpenCV 构建设置。

【讨论】:

我已经使用 CMake 从源文件编译并制作了 opencv,然后使用 Visual Studio 2010 编译了所有解决方案项目 您是否在编译 openCV 之前将 Cuda Toolkit 目录添加到您的 OpenCV CMake 设置中? 请在 main 函数的开头添加 std::cout &lt;&lt; cv::getBuildInformation() &lt;&lt; std::endl; 以查看您在 openCV 构建中选择了哪些选项。 是 CMake 已经自动检测到 Cuda Toolkit 目录 你在 CMake 中设置了WITH_CUDA=ON 吗?也看看docs.opencv.org/modules/gpu/doc/introduction.html

以上是关于我需要哪个 opencv cuda 库来运行一个简单的 opencv cuda 程序?的主要内容,如果未能解决你的问题,请参考以下文章

ubuntu20.04极简配置一个深度学习环境

ubuntu20.04极简配置一个深度学习环境

ubuntu20.04极简配置一个深度学习环境

我可以使用哪个算法或库来检测和测量一个或多个盒子尺寸

HYF-Linux8 环境配置躺坑记录 cuda8 cudnn6 virtualenv tensorflow opencv3.3.1

CUDA 相当于 OpenCV 3 中的 estimateRigidTransform