[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;
}
相关推荐
我爱一条柴ya6 分钟前
【AI大模型】线性回归:经典算法的深度解析与实战指南
人工智能·python·算法·ai·ai编程
Qiuner11 分钟前
【源力觉醒 创作者计划】开源、易用、强中文:文心一言4.5或是 普通人/非AI程序员 的第一款中文AI?
人工智能·百度·开源·文心一言·gitcode
未来之窗软件服务23 分钟前
chrome webdrive异常处理-session not created falled opening key——仙盟创梦IDE
前端·人工智能·chrome·仙盟创梦ide·东方仙盟·数据调式
AI街潜水的八角40 分钟前
深度学习图像分类数据集—蘑菇识别分类
人工智能·深度学习·分类
飞睿科技1 小时前
乐鑫代理商飞睿科技,2025年AI智能语音助手市场发展趋势与乐鑫芯片解决方案分析
人工智能
许泽宇的技术分享1 小时前
从新闻到知识图谱:用大模型和知识工程“八步成诗”打造科技并购大脑
人工智能·科技·知识图谱
坤坤爱学习2.01 小时前
求医十年,病因不明,ChatGPT:你看起来有基因突变
人工智能·ai·chatgpt·程序员·大模型·ai编程·大模型学
蹦蹦跳跳真可爱5892 小时前
Python----循环神经网络(Transformer ----注意力机制)
人工智能·深度学习·nlp·transformer·循环神经网络
空中湖4 小时前
tensorflow武林志第二卷第九章:玄功九转
人工智能·python·tensorflow
lishaoan774 小时前
使用tensorflow的线性回归的例子(七)
人工智能·tensorflow·线性回归