EmguCv3.4在实际工业应用----2D矩阵条码的位置定位和识别解码

1.定位找到条码图示:


EmguCv3.4在实际工业应用----2D矩阵条码的位置定位和识别解码

EmguCv3.4在实际工业应用----2D矩阵条码的位置定位和识别解码

代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            cam1 = new Capture(0);
            scr_picture = new Mat();
            pai_pic = new Mat();
            try
            {
                cam1.Start();
                cam1.ImageGrabbed += zhuatu;


            }
            catch
            {
                MessageBox.Show("相机错误");
                cam1.Stop();
                cam1 = null;
            }
        }
        /// <summary>
        /// 变量的定义
        /// </summary>
        private  string ocrpath = Application.StartupPath + "\\tessdata";//OCR文档路径
        private const string language = "eng";//识别语言
        private Mat scr_picture=null;//原图像
        private Image<Bgr, byte> tu_2d=null;//2d条码原图
        private Image<Bgr, byte> tuyuan_2d = null;
        private Mat thr_picture = null;//灰图
        private Mat th_pic;//2zhi
        private Mat pai_pic = null;//显示图像
        private Point lab_c_point;//条码中心位置
        private double ThresholdBinary_val;//二值化数值
        private Capture cam1=null;//相机
        private bool b_chufa=false;//触发拍照信号
        private bool b_cpai = false;//完成拍照
        


        private void Form1_Load(object sender, EventArgs e)
        {


        }
        private void zhuatu(object sender, EventArgs e)
        {
                cam1.Retrieve(pai_pic);
                picture_scr.Image = pai_pic.Bitmap;
      
        }


        private void button3_Click(object sender, EventArgs e)
        {
            cam1.Retrieve(scr_picture);


            tu_2d = scr_picture.ToImage<Bgr, byte>().Copy(new Rectangle(new Point(scr_picture.Width / 2 - 200, scr_picture.Height / 2 - 200), new Size(400, 400)));
            tuyuan_2d = tu_2d.Clone();
            tu_2d._EqualizeHist();
            tu_2d._ThresholdBinary(new Bgr(240, 255, 255), new Bgr(255, 255, 255));
            
            //tu_2d._GammaCorrect((double)numericUpDown1.Value);
            picture_result.Image = tu_2d.Bitmap;


        }


        private void 手动查找_Click(object sender, EventArgs e)
        {
            #region
        
                        #endregion
            string a1 = string.Empty;
           


            thr_picture = new Mat();
            th_pic = new Mat();
            CvInvoke.CvtColor(tu_2d, thr_picture, ColorConversion.Bgr2Gray);
            CvInvoke.Threshold(thr_picture, th_pic, (double)numericUpDown1.Value, 255, ThresholdType.Binary);
            CvInvoke.Canny(th_pic, th_pic, 200, 255);
            VectorOfVectorOfPoint cwaikuo = new VectorOfVectorOfPoint();//优化前轮廓
            VectorOfVectorOfPoint cwaikuo_you = new VectorOfVectorOfPoint();//优化后轮廓
            CvInvoke.FindContours(th_pic, cwaikuo, null, Emgu.CV.CvEnum.RetrType.List, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);//提取轮廓
            //  CvInvoke.ApproxPolyDP(cwaikuo, cwaikuo_you, 0.9, false);
            ///通过面积大小进行筛选
            for (int i = 0; i < cwaikuo.Size; i++)
            {
                VectorOfPoint mian_poit = cwaikuo[i];
                
               Double ar1 = CvInvoke.ContourArea(mian_poit);//计算面积
                 Double ar = CvInvoke.ArcLength(mian_poit, true);
                 if (ar > (int)25 && ar1>5)
                {
                    
                 RotatedRect rect=   CvInvoke.MinAreaRect(mian_poit);
                 float w = rect.Size.Width;
                 float h = rect.Size.Height;
                 float rate1 = w / h;
                 float rate2 = h/ w;
                 if ((rate1 > 0.9) && (rate1<1.2))
                    {
                        cwaikuo_you.Push(mian_poit);
                        RotatedRect rect1 = CvInvoke.MinAreaRect(mian_poit);
                        if (rect1.Size.Height * rect1.Size.Width > 1200)
                        {
                            a1 += rect1.Center.ToString() + Environment.NewLine;


                            tuyuan_2d.Draw(rect1, new Bgr(0, 255, 0), 2);
                        }
                    }
                   //放进优化后轮廓
                }


            }
            
          //  CvInvoke.DrawContours(tu_2d, cwaikuo_you, -1, new MCvScalar(0, 0, 255));
            
            pictureBox1.Image = th_pic.Bitmap;
            picture_result.Image = tuyuan_2d.ToBitmap();
            pictureBox1.Image = th_pic.Bitmap;
            richTextBox1.Text = a1;
        }
    }
}