#include
#include "opencv2/opencv.hpp"
int main (int argc, char* argv[])
{
cv::Mat h_img1 = cv::imread("images/cameraman.tif", 0);
cv::cuda::GpuMat d_result1,d_result2,d_result3,d_result4,d_result5, d_img1;
//Measure initial time ticks
int64 work_begin = cv::getTickCount();
d_img1.upload(h_img1);
cv::cuda::threshold(d_img1, d_result1, 128.0, 255.0, cv::THRESH_BINARY);
cv::cuda::threshold(d_img1, d_result2, 128.0, 255.0, cv::THRESH_BINARY_INV);
cv::cuda::threshold(d_img1, d_result3, 128.0, 255.0, cv::THRESH_TRUNC);
cv::cuda::threshold(d_img1, d_result4, 128.0, 255.0, cv::THRESH_TOZERO);
cv::cuda::threshold(d_img1, d_result5, 128.0, 255.0, cv::THRESH_TOZERO_INV);
cv::Mat h_result1,h_result2,h_result3,h_result4,h_result5;
d_result1.download(h_result1);
d_result2.download(h_result2);
d_result3.download(h_result3);
d_result4.download(h_result4);
d_result5.download(h_result5);
//Measure difference in time ticks
int64 delta = cv::getTickCount() - work_begin;
double freq = cv::getTickFrequency();
//Measure frames per second
double work_fps = freq / delta;
std::cout <<"Performance of Thresholding on GPU: " <<std::endl;
std::cout <<"Time: " << (1/work_fps) <<std::endl;
std::cout <<"FPS: " <<work_fps <<std::endl;
return 0;
}
#include
#include "opencv2/opencv.hpp"
#include
#include "opencv2/opencv.hpp"
int main ()
{
cv::Mat h_img1 = cv::imread("images/cameraman.tif",0);
cv::cuda::GpuMat d_img1,d_result1,d_result2;
d_img1.upload(h_img1);
int width= d_img1.cols;
int height = d_img1.size().height;
cv::cuda::resize(d_img1,d_result1,cv::Size(200, 200), cv::INTER_CUBIC);
cv::cuda::resize(d_img1,d_result2,cv::Size(0.5*width, 0.5*height), cv::INTER_LINEAR);
cv::Mat h_result1,h_result2;
d_result1.download(h_result1);
d_result2.download(h_result2);
cv::imshow("Original Image ", h_img1);
cv::imshow("Resized Image", h_result1);
cv::imshow("Resized Image 2", h_result2);
cv::imwrite("Resized1.png", h_result1);
cv::imwrite("Resized2.png", h_result2);
cv::waitKey();
return 0;
}
#include
#include
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0);
if (!cap.isOpened()) {
cerr << "Can not open video source";
return -1;
}
std::vector<cv::Rect> h_found;
cv::Ptr<cv::cuda::CascadeClassifier> cascade = cv::cuda::CascadeClassifier::create("haarcascade_frontalface_alt2.xml");
cv::cuda::GpuMat d_frame, d_gray, d_found;
while(1)
{
Mat frame;
if ( !cap.read(frame) ) {
cerr << "Can not read frame from webcam";
return -1;
}
d_frame.upload(frame);
cv::cuda::cvtColor(d_frame, d_gray, cv::COLOR_BGR2GRAY);
cascade->detectMultiScale(d_gray, d_found);
cascade->convert(d_found, h_found);
for(int i = 0; i < h_found.size(); ++i)
{
rectangle(frame, h_found[i], Scalar(0,255,255), 5);
}
imshow("Result", frame);
if (waitKey(1) == 'q') {
break;
}
}
return 0;
}
总结
本教程是自己学习CUDA所遇到的一些概念与总结,由于CUDA主要是一个应用,还是以代码为主,加速算法与硬件息息相关,干了很久深度学习了,对于硬件的知识已经遗忘很多,后续还是复习一些硬件知识后再继续深入吧。