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

效果

项目

代码

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();
            }

        }
    }
}

Demo下载

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