首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐
2025年7月23日 星期三 2:48am

C# OpenCvSharp 图像滤波技巧

  • 25-02-19 03:20
  • 4573
  • 12281
blog.csdn.net

C#  OpenCvSharp 图像滤波技巧

目录

效果

平滑滤波

锐化滤波

高通滤波

中值滤波

双边滤波

自定义滤波

项目

代码

下载


效果

平滑滤波

平滑图像,减少噪点,让画面更加柔和。

锐化滤波

让细节更明显

高通滤波

高通滤波器,比如拉普拉斯滤波器,可以帮助我们识别边缘,让主体脱颖而出

中值滤波

对抗椒盐噪声

双边滤波

保持边缘的同时平滑

自定义滤波

项目

代码

using OpenCvSharp;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace OpenCvSharp_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string startupPath;
        string image_path;

        Stopwatch stopwatch = new Stopwatch();

        Mat image;
        Mat result_image;

        private void Form1_Load(object sender, EventArgs e)
        {
            startupPath = System.Windows.Forms.Application.StartupPath;

            image_path = "1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path,ImreadModes.Grayscale);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        ///


        /// 保存
        ///

        ///
        ///
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            var sdf = new SaveFileDialog();
            sdf.Title = "保存图片";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }

        ///


        /// 平滑滤波
        /// 平滑图像,减少噪点,让画面更加柔和。
        ///

        ///
        ///
        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();

            result_image = image.Clone();
            

            //平均模糊
            //Cv2.Blur(result_image, result_image, new OpenCvSharp.Size(5, 5));//(5, 5)是核大小,越大越模糊

            //高斯模糊
            Cv2.GaussianBlur(result_image, result_image, new OpenCvSharp.Size(5, 5), 0);//0表示自动选择sigma值


            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

        }

        ///


        /// 锐化滤波
        /// 让细节更明显
        ///

        ///
        ///
        private void button4_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();
            result_image = new Mat();

            //定义锐化卷积核
            InputArray kernel = InputArray.Create(new float[3, 3] {
            { -1, -1, -1 },
            { -1, 9, -1 },
            { -1, -1, -1 } });

            Cv2.Filter2D(image, result_image, image.Type(), kernel);


            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        }

        ///


        /// 高通滤波
        /// 高通滤波器,比如拉普拉斯滤波器,可以帮助我们识别边缘,让主体脱颖而出
        ///

        ///
        ///
        private void button5_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();


            result_image = new Mat();
            Cv2.Laplacian(image, result_image, MatType.CV_64F);
            Cv2.ConvertScaleAbs(result_image, result_image);
            result_image.ConvertTo(result_image, MatType.CV_8U);


            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        }

        ///


        /// 中值滤波
        /// 对抗椒盐噪声
        ///

        ///
        ///
        private void button6_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();


            result_image = new Mat();
            Cv2.MedianBlur(image, result_image, 5);


            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

        }

        ///


        /// 双边滤波
        /// 保持边缘的同时平滑
        ///

        ///
        ///
        private void button7_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();

            result_image = new Mat();
            Cv2.BilateralFilter(image, result_image, 9,75,75);

            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
        }

        ///


        /// 自定义滤波
        ///

        ///
        ///
        private void button8_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }

            stopwatch.Restart();
            result_image = new Mat();

            //定义卷积核
            InputArray kernel = InputArray.Create(new float[3, 3] {
            { 0, -1,0},
            { -1, 5, -1 },
            { 0, -1, 0 } });

            Cv2.Filter2D(image, result_image, image.Type(), kernel);


            double costTime = stopwatch.Elapsed.TotalMilliseconds;
            textBox1.Text = $"耗时:{costTime:F2}ms";
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

        }
    }
}

  1. using OpenCvSharp;
  2. using System;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.Windows.Forms;
  7. namespace OpenCvSharp_Demo
  8. {
  9. public partial class Form1 : Form
  10. {
  11. public Form1()
  12. {
  13. InitializeComponent();
  14. }
  15. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  16. string startupPath;
  17. string image_path;
  18. Stopwatch stopwatch = new Stopwatch();
  19. Mat image;
  20. Mat result_image;
  21. private void Form1_Load(object sender, EventArgs e)
  22. {
  23. startupPath = System.Windows.Forms.Application.StartupPath;
  24. image_path = "1.jpg";
  25. pictureBox1.Image = new Bitmap(image_path);
  26. image = new Mat(image_path,ImreadModes.Grayscale);
  27. }
  28. private void button1_Click(object sender, EventArgs e)
  29. {
  30. OpenFileDialog ofd = new OpenFileDialog();
  31. ofd.Filter = fileFilter;
  32. if (ofd.ShowDialog() != DialogResult.OK) return;
  33. pictureBox1.Image = null;
  34. pictureBox2.Image = null;
  35. textBox1.Text = "";
  36. image_path = ofd.FileName;
  37. pictureBox1.Image = new Bitmap(image_path);
  38. image = new Mat(image_path);
  39. }
  40. /// <summary>
  41. /// 保存
  42. /// </summary>
  43. /// <param name="sender"></param>
  44. /// <param name="e"></param>
  45. private void button3_Click(object sender, EventArgs e)
  46. {
  47. if (pictureBox2.Image == null)
  48. {
  49. return;
  50. }
  51. Bitmap output = new Bitmap(pictureBox2.Image);
  52. var sdf = new SaveFileDialog();
  53. sdf.Title = "保存图片";
  54. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
  55. if (sdf.ShowDialog() == DialogResult.OK)
  56. {
  57. switch (sdf.FilterIndex)
  58. {
  59. case 1:
  60. {
  61. output.Save(sdf.FileName, ImageFormat.Jpeg);
  62. break;
  63. }
  64. case 2:
  65. {
  66. output.Save(sdf.FileName, ImageFormat.Png);
  67. break;
  68. }
  69. case 3:
  70. {
  71. output.Save(sdf.FileName, ImageFormat.Bmp);
  72. break;
  73. }
  74. }
  75. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  76. }
  77. }
  78. /// <summary>
  79. /// 平滑滤波
  80. /// 平滑图像,减少噪点,让画面更加柔和。
  81. /// </summary>
  82. /// <param name="sender"></param>
  83. /// <param name="e"></param>
  84. private void button2_Click(object sender, EventArgs e)
  85. {
  86. if (image_path == "")
  87. {
  88. return;
  89. }
  90. stopwatch.Restart();
  91. result_image = image.Clone();
  92. //平均模糊
  93. //Cv2.Blur(result_image, result_image, new OpenCvSharp.Size(5, 5));//(5, 5)是核大小,越大越模糊
  94. //高斯模糊
  95. Cv2.GaussianBlur(result_image, result_image, new OpenCvSharp.Size(5, 5), 0);//0表示自动选择sigma值
  96. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  97. textBox1.Text = $"耗时:{costTime:F2}ms";
  98. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  99. }
  100. /// <summary>
  101. /// 锐化滤波
  102. /// 让细节更明显
  103. /// </summary>
  104. /// <param name="sender"></param>
  105. /// <param name="e"></param>
  106. private void button4_Click(object sender, EventArgs e)
  107. {
  108. if (image_path == "")
  109. {
  110. return;
  111. }
  112. stopwatch.Restart();
  113. result_image = new Mat();
  114. //定义锐化卷积核
  115. InputArray kernel = InputArray.Create<float>(new float[3, 3] {
  116. { -1, -1, -1 },
  117. { -1, 9, -1 },
  118. { -1, -1, -1 } });
  119. Cv2.Filter2D(image, result_image, image.Type(), kernel);
  120. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  121. textBox1.Text = $"耗时:{costTime:F2}ms";
  122. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  123. }
  124. /// <summary>
  125. /// 高通滤波
  126. /// 高通滤波器,比如拉普拉斯滤波器,可以帮助我们识别边缘,让主体脱颖而出
  127. /// </summary>
  128. /// <param name="sender"></param>
  129. /// <param name="e"></param>
  130. private void button5_Click(object sender, EventArgs e)
  131. {
  132. if (image_path == "")
  133. {
  134. return;
  135. }
  136. stopwatch.Restart();
  137. result_image = new Mat();
  138. Cv2.Laplacian(image, result_image, MatType.CV_64F);
  139. Cv2.ConvertScaleAbs(result_image, result_image);
  140. result_image.ConvertTo(result_image, MatType.CV_8U);
  141. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  142. textBox1.Text = $"耗时:{costTime:F2}ms";
  143. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  144. }
  145. /// <summary>
  146. /// 中值滤波
  147. /// 对抗椒盐噪声
  148. /// </summary>
  149. /// <param name="sender"></param>
  150. /// <param name="e"></param>
  151. private void button6_Click(object sender, EventArgs e)
  152. {
  153. if (image_path == "")
  154. {
  155. return;
  156. }
  157. stopwatch.Restart();
  158. result_image = new Mat();
  159. Cv2.MedianBlur(image, result_image, 5);
  160. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  161. textBox1.Text = $"耗时:{costTime:F2}ms";
  162. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  163. }
  164. /// <summary>
  165. /// 双边滤波
  166. /// 保持边缘的同时平滑
  167. /// </summary>
  168. /// <param name="sender"></param>
  169. /// <param name="e"></param>
  170. private void button7_Click(object sender, EventArgs e)
  171. {
  172. if (image_path == "")
  173. {
  174. return;
  175. }
  176. stopwatch.Restart();
  177. result_image = new Mat();
  178. Cv2.BilateralFilter(image, result_image, 9,75,75);
  179. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  180. textBox1.Text = $"耗时:{costTime:F2}ms";
  181. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  182. }
  183. /// <summary>
  184. /// 自定义滤波
  185. /// </summary>
  186. /// <param name="sender"></param>
  187. /// <param name="e"></param>
  188. private void button8_Click(object sender, EventArgs e)
  189. {
  190. if (image_path == "")
  191. {
  192. return;
  193. }
  194. stopwatch.Restart();
  195. result_image = new Mat();
  196. //定义卷积核
  197. InputArray kernel = InputArray.Create<float>(new float[3, 3] {
  198. { 0, -1,0},
  199. { -1, 5, -1 },
  200. { 0, -1, 0 } });
  201. Cv2.Filter2D(image, result_image, image.Type(), kernel);
  202. double costTime = stopwatch.Elapsed.TotalMilliseconds;
  203. textBox1.Text = $"耗时:{costTime:F2}ms";
  204. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  205. }
  206. }
  207. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/139436781"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top