EmguCv3.4在实际工业应用----2D矩阵条码的位置定位和识别解码(二)---检测条码断码和打印偏移

实际生产中需要,需用简单便宜WEBCAM来检测条码断码和打印偏移,如下简单介绍

原理:定位2D条码,解码2D条码,通过检测条码纸中心和条码图像码中心进行比对,来确认打印是否偏移!

希望大家一起学习!其中有一个问题,方向性还存在问题。




下面列出主要功能代码:

 #region //自动化代码
        private void test_lable()
        {
            //线程
            while (b_auoto)//启动线程
            {
                if (b_chufa && b_cpai == false)
                {
                    bool B_jiema = false;
                    bool B_pianyi = false;
                    #region ///拍图进行滤波
                    scr_picture = new Mat();
                    cam1.Retrieve(scr_picture);
                    b_cpai = true;//拍照中,避免反复进行拍照
                    tuyuan_2d = scr_picture.ToImage<Bgr, byte>();
                    tuyuan_2d.ROI = lablejian_rectangle;
                    tuyuan_2d._GammaCorrect(gamma);
                    tu_2d = tuyuan_2d.Copy(tuyuan_2d.ROI);
                    tu_2d._SmoothGaussian(3);
                    scr_picture.Dispose();//清理拍照图片,会不会有问题,要测试才知道。
                    tiao_num = jiema2d(tu_2d.Mat);
                    if (tiao_num.Length > 10)
                    {
                        b_jiemaok = true;
                        B_jiema = true;
                    }
                    else
                    {
                        b_jiemaok = false;
                        B_jiema = false;
                    }
                    BeginInvoke(new Action(() =>
                    {
                        //show_status(b_jiemaok, b_wenzok);
                        //show_num_status();
                       richTextBox1.Text = tiao_num;
                        picture_result.Image = tuyuan_2d.Bitmap;
                        pictureBox1.Image = tu_2d.Bitmap;
                    }));
                    #endregion
                    if (B_jiema)
                    {
                        #region  ///进行二值化
                        Image<Bgr, byte> tu_2d_new = tu_2d.Clone();
                        tu_2d_new._Erode(3);
                        tu_2d_new._Dilate(3);
                        thresh_scr = new Image<Gray, byte>(tu_2d.Size);
                        thresh_scr = tu_2d_new.Convert<Gray, byte>();
                        thresh_scr._ThresholdBinary(new Gray(ThresholdBinary_val), new Gray(255));
                        thresh_scr._Erode(3);
                        thresh_scr._Dilate(3);
                        thresh_scr._Dilate(3);
                        thresh_scr._Dilate(3);
                        thresh_scr._Dilate(3);
                        BeginInvoke(new Action(() =>
                        {
                            pictureBox1.Image = thresh_scr.ToBitmap();
                        }));
                        //  
                        #endregion


                        #region  //定位检测显示


                        optimaztion_outline(thresh_scr);


                        Image<Bgr, byte> tu_2d_newL = tu_2d.Clone();


                        CvInvoke.DrawContours(tu_2d_newL, outlines_optimization, -1, new MCvScalar(255, 0, 0), 3);
                        string a = outlines_optimization.Size.ToString();
                        int b = outlines_optimization.Size;
                        Point[] cent_poit = getcent(outlines_optimization);
                        RotatedRect codeRoi = new RotatedRect();
                        BarCode.Reader red1 = new Reader();
                        string lab_string = string.Empty;
                        try
                        {
                            lab_string = red1.ReadECC200(tu_2d_newL.Mat, out codeRoi);
                            lab_c_2dtu_point = new Point((int)codeRoi.Center.X, (int)codeRoi.Center.Y);
                        }
                        catch
                        {
                            lab_string = "解码错误!,请确认条码";
                        }
                        Image<Bgr, byte> tu_2d_ye = null;


                        BeginInvoke(new Action(() =>
                        {
                            foreach (var pointa in cent_poit)
                            {
                                CvInvoke.Circle(tu_2d_newL, pointa, 5, new MCvScalar(0, 0, 255), 3);
                            }
                            CvInvoke.Circle(tu_2d_newL, lab_c_2dtu_point, 5, new MCvScalar(0, 255, 0), 3);
                            if (b == 1)
                            {
                                RotatedRect rec1 = CvInvoke.FitEllipse(outlines_optimization[0]);
                                tu_2d_ye = tu_2d_newL.Copy(rec1).Clone();
                                labl_2d_corn = tu_2d_ye.Clone();
                                pictureBox1.Image = tu_2d_ye.Bitmap;
                            }
                            picture_result.Image = tu_2d_newL.Bitmap;
                            if (cent_poit.Length > 0)
                            {
                                lab_c_point = cent_poit[0]; //条码的中心点
                                lab_centrol = new LineSegment2D(lab_c_point, lab_c_2dtu_point);
                            }
                            // if(lab_centrol.Length>
                            richTextBox1.Text = lab_centrol.Length.ToString();
                        }));






                        #endregion


                       
                    }
                    
                    BeginInvoke(new Action(() =>
                    {
                        if (lab_centrol.Length > 16)
                        {
                            b_wenzok = false;
                            B_pianyi = false;
                        }
                        else
                        {
                            b_wenzok = true;
                            B_pianyi = true;
                        }
                        show_status(B_jiema, B_pianyi);
                        show_num_status();
                        richTextBox1.Text = tiao_num +"\n"+ lab_centrol.Length;
                    }));
                    b_chufa = false;
                    b_cpai = false;
                }
                else
                    Thread.Sleep(100);


            }


        }
        #endregion


猜你喜欢

转载自blog.csdn.net/tuto7534/article/details/80943021