OpenCV中配置CUDA,實現GPU加速


OpenCV中配置CUDA,實現GPU加速



按語:首先感謝http://blog.csdn.net/fengbingchun/article/details/9831837這個博主的原創方法,在這個基礎上編譯之后發現了很多問題,所以進行了改正,有了以下方法:


1、 查看本機配置,查看顯卡類型是否支持NVIDIA GPU,本機顯卡為NVIDIA GeForce  GT630;

2、 從http://www.nvidia.cn/Download/index.aspx?lang=cn下載最新驅動並安裝;

3、 從https://developer.nvidia.com/cuda-toolkit根據本機類型下載相應最新版的CUDA Toolkit6.0 64位,安裝,並通過樣本程序驗證其安裝正確;

4、 將C:\ProgramFiles\NVIDIAGPU Computing Toolkit\CUDA\v6.0\bin添加到環境變量中(檢查是否已經默認添加);

5、 從http://threadingbuildingblocks.org/下載最新版的TBB4.2,解壓縮,並將其bin目錄D:\soft\OpenCV2.4.8\TBB\tbb41_20140122oss\bin\ia64\vc10添加到環境變量中,注銷或重啟;

6、 從http://opencv.org/downloads.html 下載最新版本的OpenCV2.4.6,並解壓縮到D:\soft\OpenCV2.4.8文件夾中;

7、 從http://www.cmake.org/cmake/resources/software.html下載最新版本的CMake2.8.11.2並安裝;

8、打開CMake,在Where isthesource code:中選擇D:\soft\OpenCV2.4.6\opencv文件夾,在Where to buildthe binaries:中選擇D:/soft/OpenCV2.4.6/vs2010_GPU文件夾,此文件夾為手動創建;

9、點擊Configure按鈕,在彈出的對話框中選擇VisualStudio 10,然后點擊Finish;

10、 如果有紅色框出現,勾選BUILD_EXAMPLES、WITH_TBB、WITH_CUBLAS、WITH_CUDA、WITH_CUFFT,然后再次點擊Configure按鈕;

11、如果還有紅色框出現,TBB_INCLUDE_DIRS,將其值改為D:\soft\OpenCV2.4.6\TBB\tbb41_20130613oss\include為TBB中include所在的目錄,然后再次點擊Configure按鈕;

12、 如何還有紅色框出現,TBB_LIB_DIR、TBB_STDDEF_PATH,再次點擊Configure按鈕;

13、如果在下方信息框中有:Use TBB: YES(ver 4.1 interface 6105),Use Cuda: YES(ver5.0),證明我們已經將inteltbb和CUDA正確配置;

14、點擊Generate按鈕,此時會在D:\soft\OpenCV2.4.6\vs2010_GPU文件夾下生成OpenCV.sln文件;

15、以管理員身份,使用vs2010打開OpenCV.sln文件,選擇View--> Properties Manager-->分別選中ALL_BUILD中的Debug和Release上的Microsoft.Cpp.Win64.user,依次添加inteltbb和CUDA 的Executable Directories、IncludeDirectories和Library Directories,點擊右鍵-->Properties:

(需要說明的是:opencv里面要選擇build文件夾下面的路徑才對)

VC++ Directories,IncludeDirectories:

D:\soft\OpenCV2.4.6\TBB\tbb41_20130613oss\include 

C:\Program Files\NVIDIAGPU ComputingToolkit\CUDA\v5.0\include

D:\opencv\build\include

D:\opencv\build\include\opencv

D:\opencv\build\include\opencv2

 

Library Directories:

D:\soft\OpenCV2.4.6\TBB\tbb41_20130613oss\lib\ia64\vc10 

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v5.0\lib\Win64

D:\opencv\build\x64\vc10\lib

 

Executable Directories:

D:\soft\OpenCV2.4.6\TBB\tbb41_20130613oss\bin\ia64\vc10  

C:\Program Files\NVIDIA GPU ComputingToolkit\CUDA\v5.0\bin

D:\opencv\build\x64\vc10\bin

 

16、選中modules中的opencv_gpu,點擊右鍵,選擇Properties-->Linker-->Input-->IgnoreSpecificDefault Libraries加入libcmtlibcmtd;(可忽視這條)

17、 分別在Debug和Release下,選擇SolutionExplorer里的Solution OpenCV,點擊右鍵,運行”Rebuild Solution”。(這個過程比較漫長,win7 64位,4GB內存,大概需要3小時)

18、點擊CMakeTargets下的INSTALL—右鍵—僅本工程build,之后會在D:\soft\OpenCV2.4.3\vs2010\install文件夾下的bin文件夾中存放相應的動態庫,在lib文件夾下存放相應的靜態庫;

 

補充說明

(1)、安裝CUDA Toolkit時最好不要修改其默認的安裝目錄;

(2)、第17步的重建過程中,如果出現了很異常詭異的行為,有一種調試方式是刪除原先配置的文件夾,重新cmake,清空VS2010里面的工程,重新編譯,可能會改善。

(3)、為了縮短編譯時間,可以通過CMake中改變CUDA_ARCH_BIN、CUDA_ARCH_PTX值進行設置,將BUILD_EXAMPLES的勾選去掉(沒有這么做,因為是為了全部編譯)

(4)、在編譯前最好把360安全衛士和殺毒軟件關閉

(5)、也可不用管理員身份打開OpenCV.sln,第15步的屬性配置在不同的電腦上有的必須的,有的可以不需要,最好都加上,免得返工;

(6)、也可以不用修改opencv_gpu模塊的屬性配置,默認即可,即第16步也不是必須的;

(7)、編譯過程中會彈出數次對話框,如出現宏對話框則點擊Cancel選項,其它則選擇Yes選項;

(8)、編譯成功后的庫也可以直接應用於OpenCV中的OpenCL模塊。

 

 

Debug過程

編譯錯誤解決:

1、無法加載宏: C:/Users/XXX/Documents/Visual Studio 2010Projects/VSMacros80/ Samples/ Samples.vsmacros

解決方法:

把這個附件放在上述目錄里面:附件

 

2、錯誤:

CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: Infunction `destroyContexts()':

driver_api_multi.cpp:(.text._Z15destroyContextsv+0xc): undefined referenceto `cuCtxDestroy_v2'

driver_api_multi.cpp:(.text._Z15destroyContextsv+0x1c): undefinedreference to `cuCtxDestroy_v2'

CMakeFiles/example_gpu_driver_api_multi.dir/driver_api_multi.cpp.o: Infunction `Worker::operator()(int) const':

driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x19): undefined reference to`cuCtxPushCurrent_v2'

driver_api_multi.cpp:(.text._ZNK6WorkerclEi+0x6bf): undefined reference to`cuCtxPopCurrent_v2'

 

解決方法:

Only two example will affect by this bug,"example_gpu_driver_api_multi" and"example_gpu_driver_api_stereo_multi" 

Modify line 39

target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS}${OPENCV_GPU_SAMPLES_REQUIRED_DEPS})

to

target_link_libraries(${the_target} ${OPENCV_LINKER_LIBS}${OPENCV_GPU_SAMPLES_REQUIRED_DEPS} cuda)

in "opencv-2.4.6.1/samples/gpu/CMakeLists.txt" can easily fixthis problem

 

 

3、錯誤:dllmain重復定義,opencv_world246.dll不存在等錯誤

解決方法:cmake的時候不要勾選build_world選項,這個重建也不是必須的

 

4、錯誤37374error LNK1104:無法打開文件“..\..\lib\Debug\opencv_superres248d.lib”D:\opencv\opencv248GPU\modules\superres\LINKopencv_test_superres
分析:是鏈接庫鏈接不上造成的,添上這些庫就可以了。

解決辦法:第15步中務必把opencv的include\bin\lib目標都加進去,而且是build目錄下的,如果還有問題,那么重新cmake,或者清除VS2010的緩存就可以了。

5、錯誤

Unable to cast COM object of type'System.__ComObject' to interface type 'EnvDTE.DTE'.

 

解決辦法:可能是這個或者可以放在這里不解決,繼續編譯可以沒有問題

http://blogs.msdn.com/b/smondal/archive/2012/10/02/unable-to-cast-com-object-of-type-system-comobject-to-interface-type-microsoft-visualstudio-ole-interop-iserviceprovider.aspx

 

The exception seems to indicate the crashwas caused due to an interface not being found.  This indicates thatsomething's wrong with proxy/stubs on the machine. At leastIServiceProvider is failing. IE is usually the owner of the IServiceProviderproxy. 
From the dump, it showed devenv.exe process crashed shortly afterloading:  C:\Windows\SysWOW64\actxprxy.dll andC:\Windows\System32\mssprxy.dll

The issue turned out to be brokenregistration for IServiceProvider. The proxy-stub CLSID was wrong (it wasreferring to actxprxy.dll instead of ieproxy.dll on Windows 7 x64). Registeringieproxy.dll from an elevated cmd prompt resolved the issue.  regsvr32 "C:\Program Files (x86)\Internet Explorer\ieproxy.dll" 

On x86 systems,  regsvr32"C:\Program Files\Internet Explorer\ieproxy.dll"

 

 

新編譯的opencv使用方法

1、打開vs2010,新建一個控制台應用程序,為vs2010配置OpenCV環境:選擇View-->Properties Manager-->分別選中Debug和Release上的Microsoft.Cpp.Win64.user,點擊右鍵-->Properties:VC++ Directories,Include Directories:D:\Soft\OpenCV2.4.6\vs2010_GPU\install\include;D:\Soft\OpenCV2.4.6\vs2010_GPU\install\include\opencv;D:\Soft\OpenCV2.4.6\vs2010_GPU\install\include\opencv2;Library Directories:D:\Soft\OpenCV2.4.6\vs2010_GPU\install\lib;

2、選中工程-->Properties-->Configuration Properties-->Linker-->Input-->AdditionalDependencies:Debug和Release,添加相應的.lib庫;

3、將D:\soft\OpenCV2.4.6\vs2010_GPU\install\bin加入到windows系統環境變量Path中,重啟。(也可以將install文件夾全部拷貝出來,作為一個新的opencv版本存在,里面應該包括include\bin\lib這個幾個主要目錄)

4、示例如下,如果iDevicesNum結果非0,說明安裝配置正確。

1. #include "stdafx.h"  

2. #include <opencv2/opencv.hpp>  

3. #include <opencv2/gpu/gpu.hpp>  

4.    

5. using namespace cv;  

6. using namespace cv::gpu;  

7.    

8. int_tmain(int argc, _TCHAR* argv[])  

9. {  

10.   int iDevicesNum = getCudaEnabledDeviceCount();  

11.   

12.   cout<<iDevicesNum<<endl;  

13.   

14.   return 0;  

15.}  

 

參考文獻

1、  http://opencv.willowgarage.com/wiki/OpenCV_GPU

2、  http://docs.opencv.org/modules/gpu/doc/introduction.html

3、  http://wenku.baidu.com/view/81e29c6f011ca300a6c390dd.html

4、  http://blog.cuvilib.com/2011/03/22/how-to-build-opencv-2-2-with-gpu-cuda-on-windows-7/

5、  http://docs.nvidia.com/cuda/cuda-getting-started-guide-for-microsoft-windows/index.html

 



 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM