[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;
}
相关推荐
Lee川2 小时前
Milvus 实战:当 RAG 遇上向量数据库,从"玩具 Demo"到"生产可用的"那一步
前端·数据库·人工智能
小a彤3 小时前
elec-ops-inspection:电力巡检缺陷检测,NPU推理速度提升3倍
人工智能·cann
ZhengEnCi4 小时前
09aaa-LayerNorm是什么?
人工智能
这是谁的博客?4 小时前
AI Agent 安全架构设计:漏洞分析与防护策略深度解析
人工智能·安全·网络安全·ai·agent·安全架构·架构设计
人月神话-Lee4 小时前
【图像处理】Sobel 边缘检测——让机器“看见“轮廓
图像处理·人工智能·计算机视觉·ios·ai编程·swift
冬奇Lab5 小时前
Agent系列(四):工具调用深度解析——Agent 的手和眼
人工智能·llm
Black蜡笔小新5 小时前
自动化AI算法训练服务器DLTM助力医学影像分析进入AI智能分析新时代
人工智能·算法·自动化
冬奇Lab5 小时前
一天一个开源项目(第111篇):Understand Anything - 把代码库变成可探索知识图谱的 AI 引擎
人工智能·开源·llm
猿饵块5 小时前
git--github
人工智能
黎阳之光5 小时前
黎阳之光:以视频孪生重构智慧防火,打造“天空地人智”一体化森林防火新范式
大数据·运维·人工智能·物联网·安全