OpenCVSharp入门教程 基础篇⑤——GaussianBlur高斯模糊算法

一、前文

Gaussian Blur,高斯模糊
减少图像噪声以及降低细节层次
高斯平滑也用于计算机视觉算法中的预先处理阶段,以增强图像在不同比例大小下的图像效果(参见尺度空间表示以及尺度空间实现)。 从数学的角度来看,图像的高斯模糊过程就是图像与正态分布做卷积。由于正态分布又叫作高斯分布,所以这项技术就叫作高斯模糊。

二、GaussianBlur高斯模糊算法流程

在这里插入图片描述

其中r模糊半径σ是正态分布的标准偏差

在二维空间中,这个公式生成的曲面的等高线是从中心开始呈正态分布的同心圆。
分布不为零的像素组成的卷积矩阵与原始图像做变换。每个像素的值都是周围相邻像素值的加权平均。
原始像素的值有最大的高斯分布值,所以有最大的权重,相邻像素随着距离原始像素越来越远,其权重也越来越小。

三、界面布局

  • 一个Label
  • N个Button
  • 三个Picture
    在这里插入图片描述

四、功能实现

4.1 打开图片

 private void openFileBtn_Click(object sender, EventArgs e)
 {
     OpenFileDialog openfiledialog = new OpenFileDialog();
     openfiledialog.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
     openfiledialog.RestoreDirectory = true;
     if (openfiledialog.ShowDialog() == DialogResult.OK)
     {
         Console.WriteLine(openfiledialog.FileName);
         fileName = openfiledialog.FileName;

         //Mat src = new Mat("foo.png", LoadMode.Color);
         Mat src = new Mat(fileName);
         //Mat src = new Mat(fileName, ImreadModes.Color);
         var frameBitmap = BitmapConverter.ToBitmap(src);

         pictureBox1.Image?.Dispose();
         pictureBox1.Image = frameBitmap;
     }
 }

4.2 GaussianBlur高斯模糊—源码

private void GaussianBlurBtn_Click(object sender, EventArgs e)
{
    mInput = new Mat(fileName);
    blur = new Mat(mInput.Rows, mInput.Cols, MatType.CV_8UC4);

    Size ksize = new OpenCvSharp.Size(5, 5);
    Point anchor = new Point(3, 3);
    BorderTypes borderType = BorderTypes.Constant;

    //Cv2.Blur(mInput, blur, ksize, anchor, borderType);  //模糊
    Cv2.GaussianBlur(mInput, blur, ksize, 0);  //高斯模糊

    srcPictureBox.Image = BitmapConverter.ToBitmap(mInput);
    grayPictureBox.Image = BitmapConverter.ToBitmap(blur);
}

4.3 GaussianBlur高斯模糊—参数讲解

//
// 摘要:
//     Blurs an image using a Gaussian filter.
//
// 参数:
//   src:
//     input image; the image can have any number of channels, which are processed independently,
//     but the depth should be CV_8U, CV_16U, CV_16S, CV_32F or CV_64F.
//
//   dst:
//     output image of the same size and type as src.
//
//   ksize:
//     Gaussian kernel size. ksize.width and ksize.height can differ but they both must
//     be positive and odd. Or, they can be zero’s and then they are computed from sigma*
//     .
//
//   sigmaX:
//     Gaussian kernel standard deviation in X direction.
//
//   sigmaY:
//     Gaussian kernel standard deviation in Y direction; if sigmaY is zero, it is set
//     to be equal to sigmaX, if both sigmas are zeros, they are computed from ksize.width
//     and ksize.height, respectively (see getGaussianKernel() for details); to fully
//     control the result regardless of possible future modifications of all this semantics,
//     it is recommended to specify all of ksize, sigmaX, and sigmaY.
//
//   borderType:
//     pixel extrapolation method
public static void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY = 0, BorderTypes borderType = BorderTypes.Reflect101);
  • ksize,高斯内核大小,ksize.width和ksize.height必须是正奇数,两者可以不相同,值越大越模糊
  • sigmaX,Y轴方向的标准差,值越大越模糊
  • sigmaY,X轴方向的标准差,值越大越模糊

五、运行效果图

  • 从左到右
  • 第一张是原图
  • 第二张是GaussianBlur高斯模糊结果图

Size ksize = new OpenCvSharp.Size(3, 3);
在这里插入图片描述

Size ksize = new OpenCvSharp.Size(5, 5);
在这里插入图片描述

Size ksize = new OpenCvSharp.Size(15, 15);
在这里插入图片描述

觉得好,就一键三连呗(点赞+收藏+关注)

  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
Lua并没有内置的图形处理库,但你可以使用一些外部库来实现高斯模糊后期特效。下面是一种基于Lua和LÖVE游戏引擎的实现方法: 1. 首先,在你的Lua脚本中引入LÖVE游戏引擎的库: ```lua local love = require("love") ``` 2. 创建一个新的LÖVE游戏窗口并设置其渲染函数: ```lua function love.load() love.window.setMode(800, 600) end function love.draw() -- 在这里绘制你的场景 end ``` 3. 在渲染函数中,使用LÖVE的`love.graphics`模块来进行高斯模糊后期特效的处理。你可以使用`love.graphics.captureScreenshot()`函数来获取当前屏幕的截图,然后使用滤波算法对图像进行高斯模糊处理,最后再将处理后的图像绘制到屏幕上。 ```lua function love.draw() -- 获取屏幕截图 local screenshot = love.graphics.captureScreenshot() -- 对截图进行高斯模糊处理 local blurryScreenshot = applyGaussianBlur(screenshot) -- 绘制模糊后的截图 love.graphics.draw(blurryScreenshot, 0, 0) end ``` 4. 实现`applyGaussianBlur()`函数来执行高斯模糊算法。你可以使用一些图像处理库,比如`lua-gd`或者`LuaJIT-FFI-Image`来进行图像处理。以下是一个使用`lua-gd`库的示例: ```lua function applyGaussianBlur(image) local gd = require("gd") -- 将截图转换为gd库中的图像对象 local imageData = gd.createFromPngStr(image:encode("png"):getString()) -- 对图像应用高斯模糊滤波器 imageData:gaussianBlur(5) -- 调整模糊程度,这里使用半径为5的高斯模糊 -- 将处理后的图像转换回LÖVE的图像对象 local blurryImage = love.graphics.newImage(love.image.newImageData(imageData:string())) return blurryImage end ``` 请注意,以上代码只是一个示例,具体的实现方式和库可能会因你的项目环境和需求而有所不同。你可以根据自己的需要选择适合的图像处理库和参数来实现高斯模糊后期特效。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小康师兄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值