#include #include #include #include #include #include #include #include #include using namespace std; using namespace cv; Mat output; cuda::GpuMat imgGpu, Cimg, CimgGray, CimgBlur, CimgCanny, CimgDil, CimgErode; //************************************************************************************ // Preprocessing //************************************************************************************ // With CUDA void Preprocessing(Mat input) { imgGpu.upload(input); cuda::cvtColor(imgGpu, CimgGray, COLOR_BGR2GRAY); Ptr gauss = cuda::createGaussianFilter(CimgGray.type(), -1, cv::Size(3, 3), 1); gauss -> apply(CimgGray, CimgBlur); Ptr canny = cuda::createCannyEdgeDetector(25.0, 75.0); canny -> detect(CimgBlur, CimgCanny); Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3)); Ptr dilato = cuda::createMorphologyFilter(MORPH_DILATE, CimgCanny.type(), kernel); dilato -> apply(CimgCanny, CimgDil); CimgDil.download(output); } // Without CUDA void SPreprocessing(Mat input) { Mat img, imgGray, imgBlur, imgCanny, imgDil, imgErode, imgChica, imgRoiD, imgRoiT; cvtColor(input, imgGray, COLOR_BGR2GRAY); GaussianBlur(imgGray, imgBlur, Size(3, 3), 3, 0); Canny(imgBlur, imgCanny, 25, 75); Mat kerneld = getStructuringElement(MORPH_RECT, Size(3, 3)); dilate(imgCanny, output, kerneld); } //************************************************************************************ // Open up the webcam VideoCapture cap(1); void cpuSpeedTest() { while (cap.isOpened()) { Mat image; bool isSuccess = cap.read(image); if (image.empty()) { cout << "Could not load in image!" << endl; } resize(image, image, Size(),0.3,0.3); auto start = getTickCount(); SPreprocessing(image); auto end = getTickCount(); auto totalTime = (end - start) / getTickFrequency(); auto fps = 1 / totalTime; cout << "FPS: " << fps << endl; putText(output, "FPS: " + to_string(int(fps)), Point(50, 50), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 255, 0), 2, false); imshow("Image", output); int k = waitKey(1); if (k == 27) { break; } } cap.release(); destroyAllWindows(); } void gpuSpeedTest() { Mat image; while (cap.isOpened()) { bool isSuccess = cap.read(image); if (image.empty()) { cout << "Could not load in image!" << endl; } resize(image, image, Size(),0.3,0.3); auto start = getTickCount(); Preprocessing(image); auto end = getTickCount(); auto totalTime = (end - start) / getTickFrequency(); auto fps = 1 / totalTime; cout << "FPS: " << fps << endl; putText(output, "FPS: " + to_string(int(fps)), Point(50, 50), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 255, 0), 2, false); imshow("Image", output); int k = waitKey(1); if (k == 27) { break; } } cap.release(); destroyAllWindows(); } int main(int, char**) { // cpuSpeedTest(); gpuSpeedTest(); return 0; }