【高性能计算】CUDA编程之OpenCV的应用(教程与代码-4)
2022/3/6 17:15:22
本文主要是介绍【高性能计算】CUDA编程之OpenCV的应用(教程与代码-4),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- imread命令将返回以蓝色、绿色和红色(BGR格式)开头的三个通道
- 处理视频的main函数中需要做的第一件事是创建VideoCapture对象。 GPU
- CUDA模块中的函数都定义在cv::cuda命名空间中,将设备上配置给图像数据用的显存块作为其参数。
- gettickcount函数返回启动系统后经过的时间(以毫秒为单位)
- 使用具有CUDA的opencv进行阈值滤波
#include <iostream> #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; }
- 使用cuda+opencv修改图像大小
#include <iostream> #include "opencv2/opencv.hpp" #include <iostream> #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; }
- 使用HARR进行人脸检测
#include <iostream> #include <opencv2/opencv.hpp> 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主要是一个应用,还是以代码为主,加速算法与硬件息息相关,干了很久深度学习了,对于硬件的知识已经遗忘很多,后续还是复习一些硬件知识后再继续深入吧。
这篇关于【高性能计算】CUDA编程之OpenCV的应用(教程与代码-4)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15PingCAP 黄东旭参与 CCF 秀湖会议,共探开源教育未来
- 2024-05-13PingCAP 戴涛:构建面向未来的金融核心系统
- 2024-05-09flutter3.x_macos桌面os实战
- 2024-05-09Rust中的并发性:Sync 和 Send Traits
- 2024-05-08使用Ollama和OpenWebUI在CPU上玩转Meta Llama3-8B
- 2024-05-08完工标准(DoD)与验收条件(AC)究竟有什么不同?
- 2024-05-084万 star 的 NocoDB 在 sealos 上一键起,轻松把数据库编程智能表格
- 2024-05-08Mac 版Stable Diffusion WebUI的安装
- 2024-05-08解锁CodeGeeX智能问答中3项独有的隐藏技能
- 2024-05-08RAG算法优化+新增代码仓库支持,CodeGeeX的@repo功能效果提升