OpenCV 加速圖像處理


  • OpenCV庫包括了對OpenCL和CUDA GPU架構的支持。
  • OpenCL(Open Computing Language):開放計算語言,可以附加在主機處理器的CPU或GPU上執行。
  • OpenCV有一個新的統一數據結構UMat,用於在必要和可能的時候,負責將數據傳輸到GPU。
  • 目前,有5個可用的OpenCL SDK:AMD APP SDK、Intel SDK、IBM OpenCL開發工具包、IBM OpenCL公共運行庫、Nvidia OpenCL驅動程序和工具。

 

檢查你的OpenCL是否可用

 1 #include <iostream>
 2 #include <opencv2/core/ocl.hpp>
 3 #include <opencv2/opencv.hpp>
 4  
 5 using namespace std;  6 using namespace cv;  7 using namespace cv::ocl;  8  
 9 int main() 10 { 11     vector<ocl::PlatformInfo> info; 12  getPlatfomsInfo(info); 13     PlatformInfo sdk = info.at(0); 14  
15     if (sdk.deviceNumber() < 1) 16         return -1; 17  
18     cout << "***********SDK************" << endl; 19     cout << "Name:" << sdk.name() << endl; 20     cout << "Vendor:" << sdk.vendor() << endl; 21     cout << "Version:" << sdk.version() << endl; 22     cout << "Version:" << sdk.version() << endl; 23     cout << "Number of devices:" << sdk.deviceNumber() << endl; 24  
25     for (int i = 0; i < sdk.deviceNumber(); i++) { 26         cout << endl; 27  Device device; 28  sdk.getDevice(device, i); 29         cout << "************* Device " << i + 1 << endl; 30  
31         cout << "Vendor Id:" << device.vendorID() << endl; 32         cout << "Vendor name:" << device.vendorName() << endl; 33         cout << "Name:" << device.name() << endl; 34         cout << "Driver version:" << device.vendorID() << endl; 35         if (device.isAMD()) cout << "Is AMD device" << endl; 36         if (device.isIntel()) cout << "Is Intel device" << endl; 37         if (device.isNVidia()) cout << "Is NVidia device" << endl; 38  
39         cout << "Global Memory size:" << device.globalMemSize() << endl; 40         cout << "Memory cache size:" << device.globalMemCacheSize() << endl; 41         cout << "Memory cache type:" << device.globalMemCacheType() << endl; 42         cout << "Local Memory size:" << device.localMemSize() << endl; 43         cout << "Local Memory type:" << device.localMemType() << endl; 44         cout << "Max Clock frequency:" << device.maxClockFrequency() << endl; 45  } 46  getchar(); 47  
48     return 0; 49 }

 

CPU和GPU處理對比

 1 #include <iostream>
 2 #include <opencv2/core/ocl.hpp>
 3 #include <opencv2/opencv.hpp>
 4  
 5 using namespace std;  6 using namespace cv;  7 using namespace cv::ocl;  8  
 9 void calEdgesCPU(void); 10 void calEdgesGPU(void); 11  
12 int main() 13 { 14  calEdgesCPU(); 15  calEdgesGPU(); 16  getchar(); 17  
18     return 0; 19 } 20  
21 void calEdgesCPU() { 22     double start=getTickCount(); 23  Mat cpuBw, cpuBlur, cpuEdges; 24     Mat cpuFrame = imread("test.jpg"); 25     //namedWindow("Canny Edges CPU", 1);
26  cvtColor(cpuFrame, cpuBw, COLOR_BGR2GRAY); 27     GaussianBlur(cpuBw, cpuBlur, Size(1, 1), 1.5, 1.5); 28     Canny(cpuBlur, cpuEdges, 50, 100, 3); 29     //imshow("Canny Edges CPU", cpuEdges);
30     cout << "CPU cost time:" << ((getTickCount() - start) / getTickFrequency()) << endl; 31 } 32  
33 void calEdgesGPU() { 34     setUseOpenCL(true); 35     double start = getTickCount(); 36  UMat gpuBw, gpuBlur, gpuEdges,gpuFrame; 37     Mat cpuFrame = imread("test.jpg"); 38  cpuFrame.copyTo(gpuFrame); 39  
40     //namedWindow("Canny Edges GPU", 1);
41  cvtColor(gpuFrame, gpuBw, COLOR_BGR2GRAY); 42     GaussianBlur(gpuBw, gpuBlur, Size(1, 1), 1.5, 1.5); 43     Canny(gpuBlur, gpuEdges, 50, 100, 3); 44     //imshow("Canny Edges CPU", gpuEdges);
45     cout << "GPU cost time:" << ((getTickCount() - start) / getTickFrequency()) << endl; 46 }

 

人臉辨認、GPU和CPU處理區別

 1 #include <iostream>
 2 #include <opencv2/core/core.hpp>
 3 #include <opencv2/core/ocl.hpp>
 4 #include <opencv2/objdetect.hpp>
 5 #include <opencv2/videoio.hpp>
 6 #include <opencv2/highgui.hpp>
 7 #include <opencv2/imgproc.hpp>
 8  
 9  
10 using namespace std; 11 using namespace cv; 12 using namespace cv::ocl; 13  
14 int main() 15 { 16     //1-設置初始參數 17     //用來存儲人臉的向量
18     vector<Rect> faces; 19  CascadeClassifier face_cascade; 20     String face_cascade_name = "D:\\OpenCV\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_frontalface_alt.xml"; 21     int face_size = 30; 22     double scale_factor = 1.1; 23     int min_neighbours = 2; 24  
25     VideoCapture cap(0); 26  UMat frame, frameGray; 27     bool finish = false; 28  
29     //2-加載xml文件,以使用分類器
30     if (!face_cascade.load(face_cascade_name)) { 31         cout << "Cannot load the face xml" << endl; 32         return -1; 33  } 34     namedWindow("Video Capture"); 35  
36     //3-選擇用CPU處理還是GPU處理
37     bool cpu_gpu = false; 38  setUseOpenCL(cpu_gpu); 39  
40  Rect r; 41     double start_time, finish_time, start_total_time, finish_total_time; 42     int counter = 0; 43  
44     //4-為每幅拍攝圖像檢測人臉
45     start_total_time = getTickCount(); 46     while (!finish) { 47         start_time = getTickCount(); 48         cap >> frame; 49         if (frame.empty()) { 50             cout << "No capture frame" << endl; 51             break; 52  } 53  cvtColor(frame, frameGray, COLOR_BGR2GRAY); 54  equalizeHist(frameGray, frameGray); 55         //檢測人臉
56         face_cascade.detectMultiScale(frameGray, faces, scale_factor, min_neighbours, 0 | CASCADE_SCALE_IMAGE, Size(face_size, face_size)); 57         //對每個檢測到的人臉
58         for (int f = 0; f < faces.size(); f++) { 59             r = faces[f]; 60             //在人臉上畫框
61             rectangle(frame, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(0, 255, 0), 3); 62  } 63         imshow("Video Capture", frame); 64         //計算處理時間
65         finish_time = getTickCount(); 66         //cout << "Time per frame:" << (finish_time - start_time) / getTickFrequency() << "sec" << endl;
67         counter++; 68             //按下Esc結束
69             if (waitKey(1) == 27) finish = true; 70             
71  } 72     finish_total_time = getTickCount(); 73     cout << "Average time per frame:" << ((finish_total_time - start_total_time) / getTickFrequency() / counter) <<"sec"<< endl; 74  
75  getchar(); 76  
77     return 0; 78 }

GPU平均時間:

CPU平均時間:


免責聲明!

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



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