[17] 使用Opencv_CUDA 进行滤波操作

使用Opencv_CUDA 进行滤波操作

  • 邻域处理操作 ==> 滤波操作,拒绝或者允许某特定频段通过
  • 如果图像某处的灰度级变化缓慢,那么就是低频区域,如果灰度级变化剧烈,就是高频区域
  • 邻域滤波即卷积操作
  • 形态学处理:膨胀,腐蚀,开、闭运算

1. 低通滤波

  • 低通滤波可以从图像中删除高频内容。噪声通常被视为高频内容,因此低通滤波器能从图像中消除噪声。
  • 噪声类型有:高斯噪声,均匀噪声,指数噪声与椒盐噪声

1.1 均值滤波

  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>

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_3x3", h_result3x3);
	cv::imshow("Blurred_5x5", h_result5x5);
	cv::imshow("Blurred_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;
}

1.2 高斯滤波

  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>

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;
}

1.3 中值滤波

  • opencv_cuda提供了中值滤波功能,但是比cpu函数要慢
  • cpu代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"

int main()
{
	cv::Mat h_img1 = cv::imread("images/saltpepper.png", 0);
	cv::Mat h_result;
	cv::medianBlur(h_img1, h_result, 3);
	cv::imshow("Original Image ", h_img1);
	cv::imshow("Median Blur Result", h_result);
	cv::waitKey();
	return 0;
}
  • gpu实现:
cpp 复制代码
filter3x3 = cv::cuda::createMedianFilter(CV_8UC1, 3);
filter3x3->apply(d_img1, d_result3x3);

2. 高通滤波

  • 高通滤波器可去除图像中的低频成分并增强高频成分,它可以去除低频范围内的背景并且增强属于高频成分的边缘
  • 常用高频滤波器:Sobel、Scharr、Laplacian

2.1 Sobel滤波器

  • 两个检测水平边缘与垂直边缘的核
  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>
#include<opencv2/cudaarithm.hpp>

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;
}

2.2 Scharr 滤波器

  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>
#include<opencv2/cudaarithm.hpp>

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;
}

2.3 Laplacian 滤波

  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>
#include<opencv2/cudaarithm.hpp>

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;
}

3. 形态学处理

  • 代码实现:
cpp 复制代码
#include <iostream>
#include "opencv2/opencv.hpp"
#include<opencv2/cudafilters.hpp>
#include<opencv2/cudaarithm.hpp>



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;
}
相关推荐
池央17 分钟前
AI性能极致体验:通过阿里云平台高效调用满血版DeepSeek-R1模型
人工智能·阿里云·云计算
我们的五年18 分钟前
DeepSeek 和 ChatGPT 在特定任务中的表现:逻辑推理与创意生成
人工智能·chatgpt·ai作画·deepseek
Yan-英杰19 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
Fuweizn21 分钟前
富唯智能可重构柔性装配产线:以智能协同赋能制造业升级
人工智能·智能机器人·复合机器人
taoqick2 小时前
对PosWiseFFN的改进: MoE、PKM、UltraMem
人工智能·pytorch·深度学习
suibian52352 小时前
AI时代:前端开发的职业发展路径拓宽
前端·人工智能
预测模型的开发与应用研究3 小时前
数据分析的AI+流程(个人经验)
人工智能·数据挖掘·数据分析
源大模型3 小时前
OS-Genesis:基于逆向任务合成的 GUI 代理轨迹自动化生成
人工智能·gpt·智能体
PowerBI学谦5 小时前
Python in Excel高级分析:一键RFM分析
大数据·人工智能·pandas
运维开发王义杰5 小时前
AI: Unsloth + Llama 3 微调实践,基于Colab
人工智能·llama