99网
您的当前位置:首页[C#]winform 使用opencvsharp实现玉米粒计数

[C#]winform 使用opencvsharp实现玉米粒计数

来源:99网

【算法介绍】

这段代码是使用OpenCvSharp库(OpenCV的C#封装)对图像进行处理,主要流程包括图像的二值化、腐蚀操作、距离变换、轮廓检测,并在原图上标出检测到的轮廓位置及数量。下面是对代码的详细解读:

注意

  • 代码中关于腐蚀的部分实际上执行的是膨胀操作,这可能是一个错误或误解。
  • 距离变换部分的实现似乎有误,特别是关于 dist 变量的使用。
  • 代码中的注释和变量命名(如 morhImage)可能不够准确,可能会引起理解上的困惑。

【效果展示】

【实现部分代码】

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenCvSharp;

namespace FIRC
{
    public partial class Form1 : Form
    {
        Mat src = new Mat();
        CornManager detector = new CornManager();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "图文件(*.*)|*.jpg;*.png;*.jpeg;*.bmp";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Multiselect = false;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
              
                src = Cv2.ImRead(openFileDialog.FileName);
                pictureBox1.Image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(src);


            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(pictureBox1.Image==null)
            {
                return;
            }
            var resultMat = detector.GetCountImage(src);
            pictureBox2.Image= OpenCvSharp.Extensions.BitmapConverter.ToBitmap(resultMat); //Mat转Bitmap
        }

        private void Form1_Load(object sender, EventArgs e)
        {
          
        }

        private void button3_Click(object sender, EventArgs e)
        {
            VideoCapture capture = new VideoCapture("test.mp4");
            if (!capture.IsOpened())
            {
                Console.WriteLine("video not open!");
                return;
            }
            Mat frame = new Mat();
            var sw = new Stopwatch();
            int fps = 0;
            while (true)
            {

                capture.Read(frame);
                if (frame.Empty())
                {
                    Console.WriteLine("data is empty!");
                    break;
                }
                sw.Start();
                var result = detector.GetCountImage(frame);
                sw.Stop();
                fps = Convert.ToInt32(1 / sw.Elapsed.TotalSeconds);
                sw.Reset();
                Cv2.PutText(result, "FPS=" + fps, new OpenCvSharp.Point(30, 30), HersheyFonts.HersheyComplex, 1.0, new Scalar(255, 0, 0), 3);
                //显示结果
                Cv2.ImShow("Result", result);
                int key = Cv2.WaitKey(10);
                if (key == 27)
                    break;
            }

            capture.Release();
        }
    }
}

【测试环境】

vs2019

netframework4.7.2

opencvsharp=4.8.0

因篇幅问题不能全部显示,请点此查看更多更全内容