[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;
}
相关推荐
白葵新几秒前
C#案例实战
c++·python·算法·计算机视觉·c#
上海云盾商务经理杨杨16 分钟前
2025年服务器僵尸攻防战:从AI勒索到量子免疫,构建下一代“数字抗体”
运维·服务器·人工智能
FL162386312917 分钟前
电线杆损坏倒塌断裂分割数据集labelme格式2597张1类别
人工智能·深度学习
天若有情67336 分钟前
AI应用UX设计:让技术更懂用户
人工智能·ux
unicrom_深圳市由你创科技1 小时前
用 PyTorch 实现一个简单的神经网络:从数据到预测
人工智能·pytorch·神经网络
汀丶人工智能1 小时前
AI Compass趣味AI应用分享:Quin-AI塔罗占卜、FateTellAI命理分析、爱宠信箱宠物情绪陪伴应用
人工智能
汀丶人工智能1 小时前
文生绘动 Agent:从词语到动态影像,言出即成,你的AI动画创作伙伴
人工智能
人工智能转人机1 小时前
16day-人工智能-机器学习-特征工程
人工智能·学习·机器学习·特征工程
这张生成的图像能检测吗1 小时前
(论文速读)探索多模式大型语言模型的视觉缺陷
人工智能·深度学习·算法·计算机视觉·语言模型·自然语言处理
小蜜蜂爱编程1 小时前
opencv 阈值分割函数
人工智能·opencv·计算机视觉