OpenCV CUDA图像处理

目录

    • 图像相加
    • 图像相减
    • 图像加权合成
    • 图像取反
    • 图像颜色空间变换
    • 图像阈值处理
    • 图像直方图均衡化
    • 图像大小调整
    • 图像平移旋转
    • 方框滤波
    • 高斯滤波
    • Sobel算子
    • Scharr算子
    • Laplacian算子
    • 图像形态学操作

图像相加

#include 
#include 
#include 

int main (int argc, char* argv[])
{
    //Read Two Images 
    cv::Mat h_img1 = cv::imread("images/cameraman.tif");
    cv::Mat h_img2 = cv::imread("images/circles.png");
    //Create Memory for storing Images on device
    cv::cuda::GpuMat d_result1,d_img1, d_img2;
    cv::Mat h_result1;
    //Upload Images to device     
    d_img1.upload(h_img1);
    d_img2.upload(h_img2);

    cv::cuda::add(d_img1,d_img2, d_result1);
    //Download Result back to host
    d_result1.download(h_result1);
    
    cv::imshow("Image1 ", h_img1);
    cv::imshow("Image2 ", h_img2);
    cv::imshow("Result addition ", h_result1);
    cv::imwrite("images/result_add.png", h_result1);
    cv::waitKey();
    return 0;
}

任何要用到GPU的计算机视觉操作,都必须将图像数据先载入到设备的显存上,可使用gpumat 关键字来先行分配显存,与用于主机内存的Mat类型相似。用前面读取图像的相同方式,读取两个要作加成处理的图像,并存放在主机内存中,然后用upload方法将图像复制到设备显存,以主机上的图像变量作为参数传递给此方法。
GPU CUDA模块中的函数都定义在cv::cuda命名空间中,将设备上配置给图像数据用的显存块作为其参数。来自CUDA模块的add功能用于图像加成,它需要三个参数:前两个参数是要加成的两个图像,最后一个参数是存储加成结果的目标。这三个变量都须用gpumat定义。
使用download方法将设备加成的结果图像回存到主机上。主机的img变量作为download方法的参数,将图像加成的结果复制过来,然后用上一节中所用的相同功能,显示该图像并存储到磁盘上。

图像相减

#include 
#include 
#include 

int main (int argc, char* argv[])
{
    //Read Two Images 
    cv::Mat h_img1 = cv::imread("images/cameraman.tif");
    cv::Mat h_img2 = cv::imread("images/circles.png");
    //Create Memory for storing Images on device
    cv::cuda::GpuMat d_result1,d_img1, d_img2;
    cv::Mat h_result1;
    //Upload Images to device     
    d_img1.upload(h_img1);
    d_img2.upload(h_img2);
    cv::cuda::subtract(d_img1, d_img2,d_result1);
    //Download Result back to host
    d_result1.download(h_result1);
    
    cv::imshow("Image1 ", h_img1);
    cv::imshow("Image2 ", h_img2);
    cv::imshow("Result Subtraction ", h_result1);
    cv::imwrite("images/result_add.png", h_result1);
    cv::waitKey();
    return 0;
}

图像加权合成

#include 
#include 
#include 

int main (int argc, char* argv[])
{
    //Read Two Images 
    cv::Mat h_img1 = cv::imread("images/cameraman.tif");
    cv::Mat h_img2 = cv::imread("images/circles.png");
    //Create Memory for storing Images on device
    cv::cuda::GpuMat d_result1,d_img1, d_img2;
    cv::Mat h_result1;
    //Upload Images to device     
    d_img1.upload(h_img1);
    d_img2.upload(h_img2);
    cv::cuda::addWeighted(d_img1,0.7,d_img2,0.3,0,d_result1);
    //Download Result back to host
    d_result1.download(h_result1);
    
    cv::imshow("Image1 ", h_img1);
    cv::imshow("Image2 ", h_img2);
    cv::imshow("Result blending ", h_result1);
    cv::imwrite("images/result_add.png", h_result1);
    cv::waitKey();
    return 0;
}

图像取反

#include 
#include 
#include 

int main (int argc, char* argv[])
{
    cv::Mat h_img1 = cv::imread("images/circles.png");
    //Create Device variables
    cv::cuda::GpuMat d_result1,d_img1;
    cv::Mat h_result1;     
    //Upload Image to device
    d_img1.upload(h_img1);
    cv::cuda::bitwise_not(d_img1,d_result1);    
    //Download result back  to host
    d_result1.download(h_result1);
    
    cv::imshow("Result inversion ", h_result1);
    cv::imwrite("images/result_inversion.png", h_result1);
    cv::waitKey();
    return 0;
}

图像颜色空间变换

#include 
#include 
#include 

int main(int argc, char* argv[])
{
	cv::Mat h_img1 = cv::imread("images/autumn.tif");
	//Define device variables
	cv::cuda::GpuMat d_result1, d_result2, d_result3, d_result4, d_img1;
	//Upload Image to device
	d_img1.upload(h_img1);
	//Convert image to different color spaces
	cv::cuda::cvtColor(d_img1, d_result1, cv::COLOR_BGR2GRAY);
	cv::cuda::cvtColor(d_img1, d_result2, cv::COLOR_BGR2RGB);
	cv::cuda::cvtColor(d_img1, d_result3, cv::COLOR_BGR2HSV);
	cv::cuda::cvtColor(d_img1, d_result4, cv::COLOR_BGR2YCrCb);
	cv::Mat h_result1, h_result2, h_result3, h_result4;
	//Download results back to host
	d_result1.download(h_result1);
	d_result2.download(h_result2);
	d_result3.download(h_result3);
	d_result4.download(h_result4);

	cv::imshow("Result in Gray ", h_result1);
	cv::imshow("Result in RGB", h_result2);
	cv::imshow("Result in HSV ", h_result3);
	cv::imshow("Result in YCrCb ", h_result4);
	cv::waitKey();
	return 0;
}

图像阈值处理

#include 
#include 
#include 

int main(int argc, char* argv[])
{
	cv::Mat h_img1 = cv::imread("images/cameraman.tif", 0);
	//Define device variables
	cv::cuda::GpuMat d_result1, d_result2, d_result3, d_result4, d_result5, d_img1;
	//Upload image on device
	d_img1.upload(h_img1);
	//Perform different thresholding techniques on device
	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;
	//Copy results back to host
	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);
	
	cv::imshow("Result Threshhold binary ", h_result1);
	cv::imshow("Result Threshhold binary inverse ", h_result2);
	cv::imshow("Result Threshhold truncated ", h_result3);
	cv::imshow("Result Threshhold truncated to zero ", h_result4);
	cv::imshow("Result Threshhold truncated to zero inverse ", h_result5);
	cv::waitKey();
	return 0;
}

图像直方图均衡化

#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/cameraman.tif",0);
    cv::cuda::GpuMat d_img1,d_result1;
    d_img1.upload(h_img1);
	cv::cuda::equalizeHist(d_img1, d_result1);
    cv::Mat h_result1;
    d_result1.download(h_result1);
    
    cv::imshow("Original Image ", h_img1);
	cv::imshow("Histogram Equalized Image", h_result1);
	cv::imwrite("images/result_inversion.png", h_img1);
	cv::imwrite("images/result_inversion.png", h_result1);
    cv::waitKey();
    return 0;
}

图像大小调整

#include 
#include 
#include 

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 
#include 

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 cols = d_img1.cols;
	int rows = d_img1.size().height;
	//Translation
	cv::Mat trans_mat = (cv::Mat_<double>(2, 3) << 1, 0, 70, 0, 1, 50);
	cv::cuda::warpAffine(d_img1, d_result1, trans_mat, d_img1.size());
	//Rotation
	cv::Point2f pt(d_img1.cols / 2., d_img1.rows / 2.);
	cv::Mat r = cv::getRotationMatrix2D(pt, 45, 1.0);
	cv::cuda::warpAffine(d_img1, d_result2, r, cv::Size(d_img1.cols, d_img1.rows));
	cv::Mat h_result1, h_result2;
	d_result1.download(h_result1);
	d_result2.download(h_result2);
	
	cv::imshow("Original Image ", h_img1);
	cv::imshow("Translated Image", h_result1);
	cv::imshow("Rotated Image", h_result2);
	cv::imwrite("Translated.png", h_result1);
	cv::imwrite("Rotated.png", h_result2);
	cv::waitKey();
	return 0;
}

方框滤波

#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/cameraman.tif",0);
    cv::cuda::GpuMat d_img1,d_result3x3,d_result5x5,d_result7x7;
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filter3x3,filter5x5,filter7x7;
   	filter3x3 = cv::cuda::createBoxFilter(CV_8UC1,CV_8UC1,cv::Size(3,3));
	filter3x3->apply(d_img1, d_result3x3);
	filter5x5 = cv::cuda::createBoxFilter(CV_8UC1,CV_8UC1,cv::Size(5,5));
	filter5x5->apply(d_img1, d_result5x5);
	filter7x7 = cv::cuda::createBoxFilter(CV_8UC1,CV_8UC1,cv::Size(7,7));
	filter7x7->apply(d_img1, d_result7x7);
    cv::Mat h_result3x3,h_result5x5,h_result7x7;
    d_result3x3.download(h_result3x3);
	d_result5x5.download(h_result5x5);
	d_result7x7.download(h_result7x7);

    cv::imshow("Original Image ", h_img1);
	cv::imshow("Blurred with kernel size 3x3", h_result3x3);
	cv::imshow("Blurred with kernel size 5x5", h_result5x5);
	cv::imshow("Blurred with kernel size 7x7", h_result7x7);
	cv::imwrite("Blurred3x3.png", h_result3x3);
	cv::imwrite("Blurred5x5.png", h_result5x5);
	cv::imwrite("Blurred7x7.png", h_result7x7);
	cv::waitKey();
    return 0;
}

高斯滤波

#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/cameraman.tif",0);
    cv::cuda::GpuMat d_img1,d_result3x3,d_result5x5,d_result7x7;      
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filter3x3,filter5x5,filter7x7;
   	filter3x3 = cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(3,3),1);
	filter3x3->apply(d_img1, d_result3x3);
	filter5x5 = cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(5,5),1);
	filter5x5->apply(d_img1, d_result5x5);
	filter7x7 = cv::cuda::createGaussianFilter(CV_8UC1,CV_8UC1,cv::Size(7,7),1);
	filter7x7->apply(d_img1, d_result7x7);
    cv::Mat h_result3x3,h_result5x5,h_result7x7;
    d_result3x3.download(h_result3x3);
	d_result5x5.download(h_result5x5);
	d_result7x7.download(h_result7x7);

	cv::imshow("Original Image ", h_img1);
	cv::imshow("Blurred with kernel size 3x3", h_result3x3);
	cv::imshow("Blurred with kernel size 5x5", h_result5x5);
	cv::imshow("Blurred with kernel size 7x7", h_result7x7);
	cv::imwrite("gBlurred3x3.png", h_result3x3);
	cv::imwrite("gBlurred5x5.png", h_result5x5);
	cv::imwrite("gBlurred7x7.png", h_result7x7);
	cv::waitKey();
    return 0;
}

Sobel算子

#include 
#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/blobs.png",0);
    cv::cuda::GpuMat d_img1,d_resultx,d_resulty,d_resultxy;
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filterx,filtery,filterxy;
   	filterx = cv::cuda::createSobelFilter(CV_8UC1,CV_8UC1,1,0);
	filterx->apply(d_img1, d_resultx);
	filtery = cv::cuda::createSobelFilter(CV_8UC1,CV_8UC1,0,1);
	filtery->apply(d_img1, d_resulty);
	cv::cuda::add(d_resultx,d_resulty,d_resultxy);        
	cv::Mat h_resultx,h_resulty,h_resultxy;
    d_resultx.download(h_resultx);
	d_resulty.download(h_resulty);
	d_resultxy.download(h_resultxy);

    cv::imshow("Original Image ", h_img1);
	cv::imshow("Sobel-x derivative", h_resultx);
	cv::imshow("Sobel-y derivative", h_resulty);
	cv::imshow("Sobel-xy derivative", h_resultxy);
	cv::imwrite("sobelx.png", h_resultx);
	cv::imwrite("sobely.png", h_resulty);
	cv::imwrite("sobelxy.png", h_resultxy);
	cv::waitKey();
    return 0;
}

Scharr算子

#include 
#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/blobs.png",0);
    cv::cuda::GpuMat d_img1,d_resultx,d_resulty,d_resultxy;
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filterx,filtery;
   	filterx = cv::cuda::createScharrFilter(CV_8UC1,CV_8UC1,1,0);
	filterx->apply(d_img1, d_resultx);
	filtery = cv::cuda::createScharrFilter(CV_8UC1,CV_8UC1,0,1);
	filtery->apply(d_img1, d_resulty);
	cv::cuda::add(d_resultx,d_resulty,d_resultxy);        
	cv::Mat h_resultx,h_resulty,h_resultxy;
    d_resultx.download(h_resultx);
	d_resulty.download(h_resulty);
	d_resultxy.download(h_resultxy);

	cv::imshow("Original Image ", h_img1);
	cv::imshow("Scharr-x derivative", h_resultx);
	cv::imshow("Scharr-y derivative", h_resulty);
	cv::imshow("Scharr-xy derivative", h_resultxy);
	cv::imwrite("scharrx.png", h_resultx);
	cv::imwrite("scharry.png", h_resulty);
	cv::imwrite("scharrxy.png", h_resultxy);
	cv::waitKey();
    return 0;
}

Laplacian算子

#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/blobs.png",0);
    cv::cuda::GpuMat d_img1,d_result1,d_result3;
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filter1,filter3;
   	filter1 = cv::cuda::createLaplacianFilter(CV_8UC1,CV_8UC1,1);
	filter1->apply(d_img1, d_result1);
	filter3 = cv::cuda::createLaplacianFilter(CV_8UC1,CV_8UC1,3);
	filter3->apply(d_img1, d_result3);
	cv::Mat h_result1,h_result3;
    d_result1.download(h_result1);
	d_result3.download(h_result3);

	cv::imshow("Original Image ", h_img1);
	cv::imshow("Laplacian Filter 1", h_result1);
	cv::imshow("Laplacian Filter 3", h_result3);
	cv::imwrite("laplacian1.png", h_result1);
	cv::imwrite("laplacian3.png", h_result3);
	cv::waitKey();
    return 0;
}

图像形态学操作

#include 
#include 
#include 

int main ()
{
    cv::Mat h_img1 = cv::imread("images/blobs.png",0);
    cv::cuda::GpuMat d_img1,d_resulte,d_resultd,d_resulto, d_resultc;
	cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT,cv::Size(5,5));	
	d_img1.upload(h_img1);
	cv::Ptr<cv::cuda::Filter> filtere,filterd,filtero,filterc;
   	filtere = cv::cuda::createMorphologyFilter(cv::MORPH_ERODE,CV_8UC1,element);
	filtere->apply(d_img1, d_resulte);
	filterd = cv::cuda::createMorphologyFilter(cv::MORPH_DILATE,CV_8UC1,element);
	filterd->apply(d_img1, d_resultd);
	filtero = cv::cuda::createMorphologyFilter(cv::MORPH_OPEN,CV_8UC1,element);
	filtero->apply(d_img1, d_resulto);
	filterc = cv::cuda::createMorphologyFilter(cv::MORPH_CLOSE,CV_8UC1,element);
	filterc->apply(d_img1, d_resultc);        
	cv::Mat h_resulte,h_resultd,h_resulto,h_resultc;
    d_resulte.download(h_resulte);
	d_resultd.download(h_resultd);
	d_resulto.download(h_resulto);
	d_resultc.download(h_resultc);

    cv::imshow("Original Image ", h_img1);
	cv::imshow("Erosion", h_resulte);
	cv::imshow("Dilation", h_resultd);
	cv::imshow("Opening", h_resulto);
	cv::imshow("closing", h_resultc);
	cv::imwrite("erosion7.png", h_resulte);
	cv::imwrite("dilation7.png", h_resultd);
	cv::imwrite("opening7.png", h_resulto);
	cv::imwrite("closing7.png", h_resultc);
	cv::waitKey();
    return 0;
}

你可能感兴趣的:(CUDA,opencv,图像处理,计算机视觉,cuda)