《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是否可用
#include <iostream>
#include <opencv2/core/ocl.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;
using namespace cv::ocl;

int main()
{
	vector<ocl::PlatformInfo> info;
	getPlatfomsInfo(info);
	PlatformInfo sdk = info.at(0);

	if (sdk.deviceNumber() < 1)
		return -1;

	cout << "***********SDK************" << endl;
	cout << "Name:" << sdk.name() << endl;
	cout << "Vendor:" << sdk.vendor() << endl;
	cout << "Version:" << sdk.version() << endl;
	cout << "Version:" << sdk.version() << endl;
	cout << "Number of devices:" << sdk.deviceNumber() << endl;

	for (int i = 0; i < sdk.deviceNumber(); i++) {
		cout << endl;
		Device device;
		sdk.getDevice(device, i);
		cout << "************* Device " << i + 1 << endl;

		cout << "Vendor Id:" << device.vendorID() << endl;
		cout << "Vendor name:" << device.vendorName() << endl;
		cout << "Name:" << device.name() << endl;
		cout << "Driver version:" << device.vendorID() << endl;
		if (device.isAMD()) cout << "Is AMD device" << endl;
		if (device.isIntel()) cout << "Is Intel device" << endl;
		if (device.isNVidia()) cout << "Is NVidia device" << endl;

		cout << "Global Memory size:" << device.globalMemSize() << endl;
		cout << "Memory cache size:" << device.globalMemCacheSize() << endl;
		cout << "Memory cache type:" << device.globalMemCacheType() << endl;
		cout << "Local Memory size:" << device.localMemSize() << endl;
		cout << "Local Memory type:" << device.localMemType() << endl;
		cout << "Max Clock frequency:" << device.maxClockFrequency() << endl;
	}
	getchar();

	return 0;
}

  • CPU和GPU处理对比
#include <iostream>
#include <opencv2/core/ocl.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;
using namespace cv::ocl;

void calEdgesCPU(void);
void calEdgesGPU(void);

int main()
{
	calEdgesCPU();
	calEdgesGPU();
	getchar();

	return 0;
}

void calEdgesCPU() {
	double start=getTickCount();
	Mat cpuBw, cpuBlur, cpuEdges;
	Mat cpuFrame = imread("test.jpg");
	//namedWindow("Canny Edges CPU", 1);
	cvtColor(cpuFrame, cpuBw, COLOR_BGR2GRAY);
	GaussianBlur(cpuBw, cpuBlur, Size(1, 1), 1.5, 1.5);
	Canny(cpuBlur, cpuEdges, 50, 100, 3);
	//imshow("Canny Edges CPU", cpuEdges);
	cout << "CPU cost time:" << ((getTickCount() - start) / getTickFrequency()) << endl;
}

void calEdgesGPU() {
	setUseOpenCL(true);
	double start = getTickCount();
	UMat gpuBw, gpuBlur, gpuEdges,gpuFrame;
	Mat cpuFrame = imread("test.jpg");
	cpuFrame.copyTo(gpuFrame);

	//namedWindow("Canny Edges GPU", 1);
	cvtColor(gpuFrame, gpuBw, COLOR_BGR2GRAY);
	GaussianBlur(gpuBw, gpuBlur, Size(1, 1), 1.5, 1.5);
	Canny(gpuBlur, gpuEdges, 50, 100, 3);
	//imshow("Canny Edges CPU", gpuEdges);
	cout << "GPU cost time:" << ((getTickCount() - start) / getTickFrequency()) << endl;
}

  • 人脸辨认、GPU和CPU处理区别
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/core/ocl.hpp>
#include <opencv2/objdetect.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>


using namespace std;
using namespace cv;
using namespace cv::ocl;

int main()
{
	//1-设置初始参数
	//用来存储人脸的向量
	vector<Rect> faces;
	CascadeClassifier face_cascade;
	String face_cascade_name = "D:\\OpenCV\\opencv\\sources\\data\\haarcascades_cuda\\haarcascade_frontalface_alt.xml";
	int face_size = 30;
	double scale_factor = 1.1;
	int min_neighbours = 2;

	VideoCapture cap(0);
	UMat frame, frameGray;
	bool finish = false;

	//2-加载xml文件,以使用分类器
	if (!face_cascade.load(face_cascade_name)) {
		cout << "Cannot load the face xml" << endl;
		return -1;
	}
	namedWindow("Video Capture");

	//3-选择用CPU处理还是GPU处理
	bool cpu_gpu = false;
	setUseOpenCL(cpu_gpu);

	Rect r;
	double start_time, finish_time, start_total_time, finish_total_time;
	int counter = 0;

	//4-为每幅拍摄图像检测人脸
	start_total_time = getTickCount();
	while (!finish) {
		start_time = getTickCount();
		cap >> frame;
		if (frame.empty()) {
			cout << "No capture frame" << endl;
			break;
		}
		cvtColor(frame, frameGray, COLOR_BGR2GRAY);
		equalizeHist(frameGray, frameGray);
		//检测人脸
		face_cascade.detectMultiScale(frameGray, faces, scale_factor, min_neighbours, 0 | CASCADE_SCALE_IMAGE, Size(face_size, face_size));
		//对每个检测到的人脸
		for (int f = 0; f < faces.size(); f++) {
			r = faces[f];
			//在人脸上画框
			rectangle(frame, Point(r.x, r.y), Point(r.x + r.width, r.y + r.height), Scalar(0, 255, 0), 3);
		}
		imshow("Video Capture", frame);
		//计算处理时间
		finish_time = getTickCount();
		//cout << "Time per frame:" << (finish_time - start_time) / getTickFrequency() << "sec" << endl;
		counter++;
			//按下Esc结束
			if (waitKey(1) == 27) finish = true;
			
	}
	finish_total_time = getTickCount();
	cout << "Average time per frame:" << ((finish_total_time - start_total_time) / getTickFrequency() / counter) <<"sec"<< endl;

	getchar();

	return 0;
}

GPU平均时间:

CPU平均时间:

猜你喜欢

转载自blog.csdn.net/weixin_39731083/article/details/81276230