目录


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)
- using OpenCvSharp;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Windows.Forms.VisualStyles;
- using static System.Net.Mime.MediaTypeNames;
-
- namespace OpenCvSharp_模糊检测_拉普拉斯算子_
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
- string image_path = "";
- Mat image = new Mat();
- Mat gray = new Mat();
- Mat laplacian = new Mat();
-
- double threshold = 100;
- double measure = 0;
-
- private void button1_Click(object sender, EventArgs e)
- {
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = fileFilter;
- if (ofd.ShowDialog() != DialogResult.OK) return;
- pictureBox1.Image = null;
- image_path = ofd.FileName;
- pictureBox1.Image = new Bitmap(image_path);
- textBox1.Text = "";
- image = new Mat(image_path);
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- if (image_path == "")
- {
- return;
- }
-
- Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
-
- Cv2.Laplacian(gray, laplacian, gray.Type(), 3, 1, 0, BorderTypes.Default);
-
- Cv2.MeanStdDev(laplacian, out var mean, out var stddev);
-
- measure = stddev.Val0 * stddev.Val0;
-
- if (measure > threshold)
- {
- textBox1.Text = "不模糊,拉普拉斯算子方差: " + measure.ToString();
- }
- else
- {
- textBox1.Text = "模糊,拉普拉斯算子方差: " + measure.ToString();
- }
-
- }
- }
- }
模糊度检测算法来自 :https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/