• C# OpenCvSharp 图片模糊检测(拉普拉斯算子)


    目录

    效果

    项目

    代码

    下载 

    参考 


    效果

    项目

    VS2022

    .net framework 4.8

    OpenCvSharp 4.8

    代码

    //
    // 摘要:
    //     Calculates the Laplacian of an image
    //
    // 参数:
    //   src:
    //     Source image
    //
    //   dst:
    //     Destination image; will have the same size and the same number of channels as
    //     src
    //
    //   ddepth:
    //     The desired depth of the destination image
    //
    //   ksize:
    //     The aperture size used to compute the second-derivative filters
    //
    //   scale:
    //     The optional scale factor for the computed Laplacian values (by default, no scaling
    //     is applied
    //
    //   delta:
    //     The optional delta value, added to the results prior to storing them in dst
    //
    //   borderType:
    //     The pixel extrapolation method
    public static void Laplacian(InputArray src, OutputArray dst, MatType ddepth, int ksize = 1, double scale = 1.0, double delta = 0.0, BorderTypes borderType = BorderTypes.Reflect101)

    //
    // 摘要:
    //     computes mean value and standard deviation of all or selected array elements
    //
    // 参数:
    //   src:
    //     The source array; it should have 1 to 4 channels (so that the results can be
    //     stored in Scalar's)
    //
    //   mean:
    //     The output parameter: computed mean value
    //
    //   stddev:
    //     The output parameter: computed standard deviation
    //
    //   mask:
    //     The optional operation mask
    public static void MeanStdDev(InputArray src, out Scalar mean, out Scalar stddev, InputArray? mask = null)

    1. using OpenCvSharp;
    2. using System;
    3. using System.Collections.Generic;
    4. using System.ComponentModel;
    5. using System.Data;
    6. using System.Drawing;
    7. using System.Linq;
    8. using System.Text;
    9. using System.Windows.Forms;
    10. using System.Windows.Forms.VisualStyles;
    11. using static System.Net.Mime.MediaTypeNames;
    12. namespace OpenCvSharp_模糊检测_拉普拉斯算子_
    13. {
    14. public partial class Form1 : Form
    15. {
    16. public Form1()
    17. {
    18. InitializeComponent();
    19. }
    20. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
    21. string image_path = "";
    22. Mat image = new Mat();
    23. Mat gray = new Mat();
    24. Mat laplacian = new Mat();
    25. double threshold = 100;
    26. double measure = 0;
    27. private void button1_Click(object sender, EventArgs e)
    28. {
    29. OpenFileDialog ofd = new OpenFileDialog();
    30. ofd.Filter = fileFilter;
    31. if (ofd.ShowDialog() != DialogResult.OK) return;
    32. pictureBox1.Image = null;
    33. image_path = ofd.FileName;
    34. pictureBox1.Image = new Bitmap(image_path);
    35. textBox1.Text = "";
    36. image = new Mat(image_path);
    37. }
    38. private void button3_Click(object sender, EventArgs e)
    39. {
    40. if (image_path == "")
    41. {
    42. return;
    43. }
    44. Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
    45. Cv2.Laplacian(gray, laplacian, gray.Type(), 3, 1, 0, BorderTypes.Default);
    46. Cv2.MeanStdDev(laplacian, out var mean, out var stddev);
    47. measure = stddev.Val0 * stddev.Val0;
    48. if (measure > threshold)
    49. {
    50. textBox1.Text = "不模糊,拉普拉斯算子方差: " + measure.ToString();
    51. }
    52. else
    53. {
    54. textBox1.Text = "模糊,拉普拉斯算子方差: " + measure.ToString();
    55. }
    56. }
    57. }
    58. }

    下载 

    Demo下载

    参考 

    模糊度检测算法来自 :https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/

  • 相关阅读:
    LeetCode #104.二叉树的最大深度
    react如何使用echars图标
    uniapp中修改placeholder属性
    MacOS升级后命令行出现xcrun: error: invalid active developer path报错信息
    1-1 开源许可证GPL, BSD, MIT, Mozilla, Apache, LGPL的介绍
    【python与数据分析】Matplotlib数据可视化
    三面阿里,被Java面试官虐哭!现场还原真实的“被虐”场景
    Linux:今天学vim编辑器,gdb调试器,makefile项目自动化构建工具
    业务架构、技术架构、项目管理的有机结合
    Go语言 context包源码学习
  • 原文地址:https://blog.csdn.net/lw112190/article/details/133012536