- 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平均时间: