private bool isBabyYiNing = false; //不跑小马的代码
        /// <summary>
        /// 拼图回调函数,用于执行界面更新
        /// </summary>
        /// <param name="end"></param>
        /// <param name="isFront"></param>
        /// <param name="bitmap"></param>
        /// <param name="rect"></param>
        /// <param name="scale"></param>
        /// <returns></returns>
        public int doStitchCallBack(bool isEnd, OneStitchSidePcb oneStitchSidePcb, OneStitchSidePcb.BitmapInfo bitmapInfo, RectangleF scaleRect)
        {
            Console.WriteLine("doStitchCallBack");
            //bool end, bool isFront, Bitmap bitmap, RectangleF rect
            //if (InvokeRequired)
            //{
            //    // If called from a different thread, we must use the Invoke method to marshal the call to the proper GUI thread.
            //    // The grab result will be disposed after the event call. Clone the event arguments for marshaling to the GUI thread.
            //    BeginInvoke(new StitchCallBack(doStitchCallBack), isEnd, oneStitchSidePcb, bitmapInfo, scaleRect);
            //    return 0;
            //}
            Rectangle scaleR = new Rectangle(Convert.ToInt32(scaleRect.X),
                                             Convert.ToInt32(scaleRect.Y),
                                             Convert.ToInt32(scaleRect.Width),
                                             Convert.ToInt32(scaleRect.Height));

            aiDetect(oneStitchSidePcb.zTrajectory,
                     bitmapInfo,
                     scaleR,
                     oneStitchSidePcb.scale,
                     oneStitchSidePcb.confidence,
                     oneStitchSidePcb.savePath);

            return(1);
        }
        /// <summary>
        /// 用于创建初始的Pcb
        /// </summary>
        /// <returns></returns>
        public static Pcb CreatePcb()
        {
            try
            {
                string id    = new Snowflake(1).nextId().ToString();
                string sPath = @"D:\Power-Ftp\" + id;
                if (!Directory.Exists(sPath))
                {
                    Directory.CreateDirectory(sPath);
                }
                Pcb pcb = new Pcb()
                {
                    Id      = id,
                    PcbPath = id,
                    results = new List <Result>(),
                };

                float            confidence = (float)0.01;
                float            scale      = (float)0.25;
                OneStitchSidePcb front      = new OneStitchSidePcb()
                {
                    confidence = confidence,

                    pcbId    = id,
                    savePath = sPath,

                    allRows     = 10,
                    allCols     = 24,
                    allNum      = 240, // 这里多尺度是需要改变总数*2的
                    currentRow  = 0,
                    currentCol  = 0,
                    zTrajectory = true,
                    dst         = null,
                    roi         = new Rectangle(),
                    scale       = scale,
                    stitchEnd   = false,
                    bitmaps     = new Queue <BitmapInfo>(),
                };

                pcb.SurfaceNumber = 1;
                pcb.FrontPcb      = front;
                pcb.AllPhotoNum   = 240;
                return(pcb);
            }
            catch (Exception er) { return(null); }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 拼图主函数
        /// </summary>
        /// <param name="oneSidePcb"></param>
        public static void StitchMain(OneStitchSidePcb oneSidePcb, StitchCallBack stitchCallBack)
        {
            bool       needSave = false;
            double     or_hl    = oneSidePcb.or_hl;
            double     or_hu    = oneSidePcb.or_hu;
            double     or_vl    = oneSidePcb.or_vl;
            double     or_vu    = oneSidePcb.or_vu;
            double     dr_hu    = oneSidePcb.dr_hu;
            double     dr_vu    = oneSidePcb.dr_vu;
            BitmapInfo bitmapInfo;

            lock (oneSidePcb.bitmaps)
            {
                bitmapInfo = oneSidePcb.bitmaps.Dequeue();
                Emgu.CV.Image <Bgr, Byte> currentFrame = new Emgu.CV.Image <Bgr, Byte>(bitmapInfo.bitmap);
                Mat imgOld = new Mat();
                CvInvoke.BitwiseAnd(currentFrame, currentFrame, imgOld);
                int edge = 3;
                Mat img  = new Mat(imgOld, new Rectangle(new Point(edge, edge), new Size(imgOld.Width - edge * 2, imgOld.Height - edge * 2)));

                //Mat smallmat = new Mat();
                //CvInvoke.Resize(img, img, new Size(Convert.ToInt32(img.Cols * 0.5), Convert.ToInt32(img.Rows * 0.5)));
                //AoiAi.hello(smallmat.Ptr);

                //第一行
                if (oneSidePcb.currentRow == 0)
                {
                    if (oneSidePcb.currentCol == 0)
                    {
                        #region 判断s型还是z字形
                        if (oneSidePcb.zTrajectory) //Z形
                        {
                            oneSidePcb.trajectorySide = (int)side.left;
                            int x       = Convert.ToInt32(img.Cols * (oneSidePcb.allCols - 1) * dr_hu);
                            int y       = Convert.ToInt32(img.Rows * (oneSidePcb.allRows - 1) * dr_vu);
                            int dstRows = Convert.ToInt32(img.Rows * (oneSidePcb.allRows + (oneSidePcb.allRows - 1) * (dr_vu * 2 - or_vl)));
                            int dstCols = Convert.ToInt32(img.Cols * (oneSidePcb.allCols + (oneSidePcb.allCols - 1) * (dr_hu * 2 - or_hl)));
                            oneSidePcb.roi = new Rectangle(x, y, img.Cols, img.Rows);
                            oneSidePcb.dst = new Mat(dstRows, dstCols, img.Depth, img.NumberOfChannels); // 第一张图不要0,0 最好留一些像素
                        }
                        else // S型
                        {
                            oneSidePcb.trajectorySide = (int)side.right;

                            int dstRows = Convert.ToInt32(img.Rows * (oneSidePcb.allRows + (oneSidePcb.allRows - 1) * (dr_vu * 2 - or_vl)));
                            int dstCols = Convert.ToInt32(img.Cols * (oneSidePcb.allCols + (oneSidePcb.allCols - 1) * (dr_hu * 2 - or_hl)));
                            int x       = Convert.ToInt32((dstCols - img.Cols) * (1 - dr_hu));
                            int y       = Convert.ToInt32(img.Rows * (oneSidePcb.allRows - 1) * dr_vu);
                            oneSidePcb.roi = new Rectangle(x, y, img.Cols, img.Rows);
                            oneSidePcb.dst = new Mat(dstRows, dstCols, img.Depth, img.NumberOfChannels); // 第一张图不要0,0 最好留一些像素
                        }
                        #endregion
                    }
                    else
                    {
                        stitchv2(oneSidePcb.dst.Ptr, oneSidePcb.roi, img.Ptr, ref oneSidePcb.roi, oneSidePcb.trajectorySide, Convert.ToInt32(img.Cols * or_hl), Convert.ToInt32(img.Cols * or_hu), Convert.ToInt32(img.Rows * dr_vu));
                    }
                    //oneSidePcb.dst.Save(@"C:\Users\Administrator\Desktop\suomi-test-img\" + oneSidePcb.currentRow + "-" + oneSidePcb.currentCol + ".jpg");
                    copy_to(oneSidePcb.dst.Ptr, img.Ptr, oneSidePcb.roi);

                    //oneSidePcb.dst.Save(@"C:\Users\Administrator\Desktop\suomi-test-img\" + oneSidePcb.currentRow + "-" + oneSidePcb.currentCol + ".jpg");

                    oneSidePcb.currentCol++;
                    if (oneSidePcb.currentCol >= oneSidePcb.allCols)
                    {
                        oneSidePcb.currentCol = 0;
                        oneSidePcb.currentRow++;
                        if (oneSidePcb.trajectorySide == (int)side.left)
                        {
                            oneSidePcb.trajectorySide = (int)side.right;
                        }
                        else if (oneSidePcb.trajectorySide == (int)side.right)
                        {
                            oneSidePcb.trajectorySide = (int)side.left;
                        }
                    }
                    //oneSidePcb.dst.Save(@"C:\Users\Administrator\Desktop\suomi-test-img\row1.jpg");
                }
                else // 其他行
                {
                    if (Convert.ToBoolean(oneSidePcb.currentRow % 2)) //偶行
                    {
                        if (oneSidePcb.currentCol == 0)
                        {
                            stitchv2(oneSidePcb.dst.Ptr, oneSidePcb.roi, img.Ptr, ref oneSidePcb.roi, (int)side.up, Convert.ToInt32(img.Cols * or_vl), Convert.ToInt32(img.Cols * or_vu), Convert.ToInt32(img.Rows * dr_hu));
                            //oneSidePcb.roi = oneSidePcb.roi0;
                        }
                        else
                        {
                            stitchv2(oneSidePcb.dst.Ptr, oneSidePcb.roi, img.Ptr, ref oneSidePcb.roi, oneSidePcb.trajectorySide, Convert.ToInt32(img.Cols * or_hl), Convert.ToInt32(img.Cols * or_hu), Convert.ToInt32(img.Rows * dr_vu), (int)side.up, Convert.ToInt32(img.Rows * or_vl), Convert.ToInt32(img.Rows * or_vu), Convert.ToInt32(img.Cols * dr_hu));
                        }
                    }
                    else
                    {
                        if (oneSidePcb.currentCol == 0)
                        {
                            stitchv2(oneSidePcb.dst.Ptr, oneSidePcb.roi, img.Ptr, ref oneSidePcb.roi, (int)side.up, Convert.ToInt32(img.Cols * or_vl), Convert.ToInt32(img.Cols * or_vu), Convert.ToInt32(img.Rows * dr_hu));
                            //oneSidePcb.roi = oneSidePcb.roi0;
                        }
                        else
                        {
                            stitchv2(oneSidePcb.dst.Ptr, oneSidePcb.roi, img.Ptr, ref oneSidePcb.roi, oneSidePcb.trajectorySide, Convert.ToInt32(img.Cols * or_hl), Convert.ToInt32(img.Cols * or_hu), Convert.ToInt32(img.Rows * dr_vu), (int)side.up, Convert.ToInt32(img.Rows * or_vl), Convert.ToInt32(img.Rows * or_vu), Convert.ToInt32(img.Cols * dr_hu));
                        }
                    }
                    //oneSidePcb.dst.Save(@"C:\Users\Administrator\Desktop\suomi-test-img\" + oneSidePcb.currentRow + "-" + oneSidePcb.currentCol + ".jpg");
                    copy_to(oneSidePcb.dst.Ptr, img.Ptr, oneSidePcb.roi);
                    //oneSidePcb.dst.Save(@"C:\Users\Administrator\Desktop\suomi-test-img\" + oneSidePcb.currentRow + "-" + oneSidePcb.currentCol + ".jpg");
                    oneSidePcb.currentCol++;
                    if (oneSidePcb.currentRow >= oneSidePcb.allRows - 1 && oneSidePcb.currentCol >= oneSidePcb.allCols)
                    {
                        needSave = true;
                    }
                    else if (oneSidePcb.currentCol >= oneSidePcb.allCols)
                    {
                        oneSidePcb.currentCol = 0;
                        oneSidePcb.currentRow++;
                        if (oneSidePcb.trajectorySide == (int)side.left)
                        {
                            oneSidePcb.trajectorySide = (int)side.right;
                        }
                        else if (oneSidePcb.trajectorySide == (int)side.right)
                        {
                            oneSidePcb.trajectorySide = (int)side.left;
                        }
                    }
                }
                #region 实时更新采集框

                stitchCallBack(false, oneSidePcb, bitmapInfo, new RectangleF((float)(oneSidePcb.roi.Location.X * 0.25),
                                                                             (float)(oneSidePcb.roi.Location.Y * 0.25),
                                                                             (float)(oneSidePcb.roi.Size.Width * 0.25),
                                                                             (float)(oneSidePcb.roi.Size.Height * 0.25)));
                #endregion

                #region 释放资源
                img.Dispose();
                currentFrame.Dispose();
                //bitmap.Dispose();
                #endregion

                if (needSave) // 加这里主要是为了优化workingForm.imgBoxWorking最后一个框的显示问题
                {
                    stitchCallBack(true, oneSidePcb, bitmapInfo, new RectangleF());
                    Mat smallmat = new Mat();
                    CvInvoke.Resize(oneSidePcb.dst, smallmat, new Size(Convert.ToInt32(oneSidePcb.dst.Cols * oneSidePcb.scale), Convert.ToInt32(oneSidePcb.dst.Rows * oneSidePcb.scale)));
                    if (oneSidePcb.zTrajectory)
                    {
                        string saveFile = Path.Combine(oneSidePcb.savePath, "front.jpg");
                        smallmat.Save(saveFile);
                        saveFile = Path.Combine(oneSidePcb.savePath, "back.jpg");
                        smallmat.Save(saveFile);
                    }
                    else
                    {
                        string saveFile = Path.Combine(oneSidePcb.savePath, "back.jpg");
                        smallmat.Save(saveFile);
                    }
                    smallmat.Dispose();
                    oneSidePcb.dst.Dispose();
                }
            }
        }