使用Emgu.CV实现高效条码检测!

目录

效果

项目

代码


效果

图片

项目

图片

代码

using Emgu.CV;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Emgu.CV_条码检测
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        Bitmap bmp;
        String imgPath = "";
        Graphics g = null;
        Pen p = new Pen(Brushes.Red);
        SolidBrush drawBush = new SolidBrush(Color.Red);
        Brush bush = new SolidBrush(Color.Green);
        Font drawFont = new Font("Arial", 8, FontStyle.Bold, GraphicsUnit.Millimeter);

        string prototxt_path = "sr.prototxt";
        string caffe_model_path = "sr.caffemodel";

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            imgPath = ofd.FileName;
            bmp = new Bitmap(imgPath);
            pictureBox1.Image = bmp;
            g = Graphics.FromImage(bmp);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            BarcodeDetector barcodeDetector = new BarcodeDetector(prototxt_path, caffe_model_path);

            Mat src = new Mat(imgPath, CV.CvEnum.ImreadModes.Grayscale);

            Mat rects = new Mat();
            bool b = barcodeDetector.Detect(src, rects);

            Array data = rects.GetData();
            if (data == null) { return; }
            int num = data.GetLength(0);
            for (int i = 0; i < num; i++)
            {
                float x1 = (float)data.GetValue(i, 0, 0);
                float y1 = (float)data.GetValue(i, 0, 1);

                float x2 = (float)data.GetValue(i, 1, 0);
                float y2 = (float)data.GetValue(i, 1, 1);

                float x3 = (float)data.GetValue(i, 2, 0);
                float y3 = (float)data.GetValue(i, 2, 1);

                float x4 = (float)data.GetValue(i, 3, 0);
                float y4 = (float)data.GetValue(i, 3, 1);

                g.FillEllipse(bush, x1, y1, 10, 10);
                g.DrawString("1", drawFont, drawBush, x1, y1);

                g.FillEllipse(bush, x2, y2, 10, 10);
                g.DrawString("2", drawFont, drawBush, x2, y2);

                g.FillEllipse(bush, x3, y3, 10, 10);
                g.DrawString("3", drawFont, drawBush, x3, y3);

                g.FillEllipse(bush, x4, y4, 10, 10);
                g.DrawString("4", drawFont, drawBush, x4, y4);

                g.DrawRectangle(p, x2, y2, x4 - x2, y4 - y2);

            }

            pictureBox2.Image = bmp;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            imgPath = "1.jpg";
            bmp = new Bitmap(imgPath);
            pictureBox1.Image = bmp;
            g = Graphics.FromImage(bmp);
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        }
    }
}

引入地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值