OpenCV2學習筆記(二十一):GPU模組小記,opencv2gpu

來源:互聯網
上載者:User

OpenCV2學習筆記(二十一):GPU模組小記,opencv2gpu

接觸一下OpenCV裡一個之前沒有接觸的模組:GPU。這裡只是根據教程和大家簡單交流一下,歡迎大家批評指正。

註:在使用GPU模組之前,需要確認在使用CMake編譯OpenCV時,勾選了選項WITH_CUDA和WITH_TBB使其生效生效。

若以上配置已經完成,在使用GPU模組的函數之前,還做一下檢查:調用函數gpu::getCudaEnabledDeviceCount,如果你在使用的OpenCV模組編譯時間不支援GPU,這個函數傳回值為0;否則傳回值為已安裝的CUDA裝置的數量。

OpenCV的GPU模組只支援NVIDIA的顯卡,原因是該部分是基於NVIDIA的CUDA和NVIDIA的NPP模組實現的。而該模組的好處在於使用GPU模組無需安裝CUDA工具,也無需學習GPU編程,因為不需要編寫GPU相關的代碼。但如果你想重新編譯OpenCV的GPU模組的話,還是需要CUDA的toolkit。

由於GPU模組的發展,使大部分函數使用起來和之前在CPU下開發非常類似。首先,就是把GPU模組連結到你的工程中,並包含必要的標頭檔gpu.hpp。其次,就是GPU模組下的資料結構,原本在cv名字空間中的現在都在gpu名字空間中,使用時可以gpu::和cv::來防止混淆。

在GPU模組中,矩陣的類型為:GpuMat而不是OpenCV中使用的cv::Mat,其他的函數命名和CPU模組中相同。OpenCV中GPU模組函數的使用步驟如下:

1.驗證OpenCV是否已啟用GPU模組。2.上傳待處理資料到GPU (Mat --> GpuMat)。3.調用OpenCV支援的GPU的處理函數。4.下載處理結果到CPU (GpuMat ---> Mat)。

根據http://blog.csdn.net/yang_xian521/article/details/7249532 所提到的,一個問題是對於2.0的GPU模組,多通道的函數支援的並不好,推薦使用GPU模組處理灰階的映像。有些情況下,使用GPU模組的運行速度還不及CPU模組下的效能,所以可以認為,GPU模組相對而言還不夠成熟,需要進一步最佳化。很重要的一個原因就是記憶體管理部分和資料轉換部分對於GPU模組而言消耗了大量的時間。

一段內建的範例程式碼如下,實現求矩陣轉置的功能:

#include <iostream>#include "cvconfig.h"#include "opencv2/core/core.hpp"#include "opencv2/gpu/gpu.hpp"#include "opencv2/core/internal.hpp" // For TBB wrappersusing namespace std;using namespace cv;using namespace cv::gpu;struct Worker { void operator()(int device_id) const; };int main(){    int num_devices = getCudaEnabledDeviceCount();    if (num_devices < 2)    {        std::cout << "Two or more GPUs are required\n";        return -1;    }    for (int i = 0; i < num_devices; ++i)    {        DeviceInfo dev_info(i);        if (!dev_info.isCompatible())        {            std::cout << "GPU module isn't built for GPU #" << i << " ("                 << dev_info.name() << ", CC " << dev_info.majorVersion()                 << dev_info.minorVersion() << "\n";            return -1;        }    }    // Execute calculation in two threads using two GPUs    int devices[] = {0, 1};    parallel_do(devices, devices + 2, Worker());    return 0;}void Worker::operator()(int device_id) const{    setDevice(device_id);    Mat src(1000, 1000, CV_32F);    Mat dst;    RNG rng(0);    rng.fill(src, RNG::UNIFORM, 0, 1);    // CPU works    transpose(src, dst);    // GPU works    GpuMat d_src(src);    GpuMat d_dst;    transpose(d_src, d_dst);    // Check results    bool passed = norm(dst - Mat(d_dst), NORM_INF) < 1e-3;    std::cout << "GPU #" << device_id << " (" << DeviceInfo().name() << "): "        << (passed ? "passed" : "FAILED") << endl;    // Deallocate data here, otherwise deallocation will be performed    // after context is extracted from the stack    d_src.release();    d_dst.release();}

CUDA的基本使用方法:http://www.cnblogs.com/dwdxdy/archive/2013/08/07/3244508.html

參考原文:http://blog.csdn.net/yang_xian521/article/details/7249532

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.