浙江省建設(shè)通網(wǎng)站迅雷磁力
imread命令將返回以藍(lán)色、綠色和紅色(BGR格式)開頭的三個通道
處理視頻的main函數(shù)中需要做的第一件事是創(chuàng)建VideoCapture對象。 GPU
CUDA模塊中的函數(shù)都定義在cv::cuda命名空間中,將設(shè)備上配置給圖像數(shù)據(jù)用的顯存塊作為其參數(shù)。
gettickcount函數(shù)返回啟動系統(tǒng)后經(jīng)過的時間(以毫秒為單位)
使用具有CUDA的opencv進(jìn)行閾值濾波
?
#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進(jìn)行人臉檢測
#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;
}
總結(jié)
本教程是自己學(xué)習(xí)CUDA所遇到的一些概念與總結(jié),由于CUDA主要是一個應(yīng)用,還是以代碼為主,加速算法與硬件息息相關(guān),干了很久深度學(xué)習(xí)了,對于硬件的知識已經(jīng)遺忘很多,后續(xù)還是復(fù)習(xí)一些硬件知識后再繼續(xù)深入吧。