Пример #1
0
        public List <object> GetListLayerInRect(System.Drawing.Rectangle Rect)
        {
            List <object> listObj = new List <object>();

            if (Rect != Rectangle.Empty)
            {
                //gerber layer
                var a = GetPadsInRect(Rect, true);
                var b = GetPadsInRect(Rect, false);
                if (a.Count + b.Count > 0)
                {
                    listObj.Add(this.Gerber);
                }
                // cad layer
                foreach (var item in this.Cad)
                {
                    for (int i = 0; i < item.CadItems.Count; i++)
                    {
                        Point ct = Point.Round(item.CadItems[i].Center);
                        ct.X += item.X;
                        ct.Y += item.Y;
                        Point     newCtRotate = ImageProcessingUtils.PointRotation(ct, new Point((int)item.CenterRotation.X + item.X, (int)item.CenterRotation.Y + item.Y), item.Angle * Math.PI / 180.0);
                        Rectangle bound       = new Rectangle(newCtRotate.X, newCtRotate.Y, 1, 1);
                        if (Rect.Contains(bound))
                        {
                            listObj.Add(item);
                            break;
                        }
                    }
                }
            }
            return(listObj);
        }
Пример #2
0
        private static int[] GetAdjust(Image <Gray, byte> ImgCap, Image <Gray, byte> ImgGerber, int RangeX, int RangeY)
        {
            int    x_ok = 0;
            int    y_ok = 0;
            double diff = 2448 * 2018;

            for (int x = -RangeX; x < RangeX; x++)
            {
                for (int y = -RangeY; y < RangeY; y++)
                {
                    using (Image <Gray, byte> imgTransform = ImageProcessingUtils.ImageTransformation(ImgCap.Copy(), x, y))
                    {
                        CvInvoke.AbsDiff(imgTransform, ImgGerber, imgTransform);

                        int count = CvInvoke.CountNonZero(imgTransform);
                        if (count < diff)
                        {
                            diff = count;
                            x_ok = x;
                            y_ok = y;
                        }
                    }
                }
            }
            return(new int[] { x_ok, y_ok });
        }
Пример #3
0
        public Point GetCenterSelected()
        {
            List <Point> centerSelected = new List <Point>();

            for (int i = 0; i < CadItems.Count; i++)
            {
                Point ct = Point.Round(CadItems[i].Center);
                ct.X += this.X;
                ct.Y += this.Y;
                Point     newCtRotate = ImageProcessingUtils.PointRotation(ct, new Point((int)this.CenterRotation.X + this.X, (int)this.CenterRotation.Y + this.Y), this.Angle * Math.PI / 180.0);
                Rectangle bound       = new Rectangle(newCtRotate.X, newCtRotate.Y, 1, 1);
                if (this.SelectCenter.Contains(bound))
                {
                    centerSelected.Add(newCtRotate);
                }
            }
            if (centerSelected.Count > 0)
            {
                long x = 0;
                long y = 0;
                for (int i = 0; i < centerSelected.Count; i++)
                {
                    x += centerSelected[i].X;
                    y += centerSelected[i].Y;
                }
                x = x / centerSelected.Count;
                y = y / centerSelected.Count;
                return(new Point(Convert.ToInt32(x), Convert.ToInt32(y)));
            }
            else
            {
                return(new Point());
            }
        }
Пример #4
0
        public static Point GetCenterRotated(Point Center, Point CenterRotate, int X, int Y, double Angle)
        {
            Center.X       += X;
            Center.Y       += Y;
            CenterRotate.X += X;
            CenterRotate.Y += Y;
            Point cadCenterRotated = ImageProcessingUtils.PointRotation(Center, CenterRotate, Angle * Math.PI / 180.0);

            return(cadCenterRotated);
        }
Пример #5
0
        private async void OnPostProcessCompleteAsync(
            byte[] pixelArray, int imageWidth, int imageHeight,
            ObjectDetails fromObjectDetails, ObjectDetails toObjectDetails)
        {
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteableBitmap bitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, imageWidth, imageHeight);

                if (bitmap != null)
                {
                }
            });
        }
Пример #6
0
 public void SetAngle(double Angle, Size FOV, bool Reload = true)
 {
     this.Angle = Angle;
     if (this.ProcessingGerberImage != null)
     {
         this.ProcessingGerberImage.Dispose();
         this.ProcessingGerberImage = null;
     }
     this.ProcessingGerberImage = ImageProcessingUtils.ImageRotation(this.OrgGerberImage.Copy(), new Point(this.OrgGerberImage.Width / 2, this.OrgGerberImage.Height / 2), this.Angle * Math.PI / 180.0);
     if (Reload)
     {
         this.UpdatePadItems();
         this.ResetMark();
     }
 }
Пример #7
0
 public static void PreCapture(Model mModel, string ID, string SavePath, int Index, Image <Bgr, byte> Img, Image <Gray, byte> ImgGerber, Rectangle ROI, Rectangle ROIGerber)
 {
     lock (mModel)
     {
     }
     using (Image <Bgr, byte> imgRotated = ImageProcessingUtils.ImageRotation(Img, new Point(Img.Width / 2, Img.Height / 2), -mModel.AngleAxisCamera * Math.PI / 180.0))
         using (Image <Bgr, byte> imgUndis = new Image <Bgr, byte>(imgRotated.Size))
         {
             CvInvoke.Undistort(imgRotated, imgUndis, mCalibImage.CameraMatrix, mCalibImage.DistCoeffs, mCalibImage.NewCameraMatrix);
             imgUndis.ROI = ROI;
             string fileName = string.Format("{0}//Image_{1}_ROI({2}_{3}_{4}_{5})_ROI_GERBER({6}_{7}_{8},{9}).png",
                                             SavePath, Index + 1, ROI.X, ROI.Y, ROI.Width, ROI.Height,
                                             ROIGerber.X, ROIGerber.Y, ROIGerber.Width, ROIGerber.Height);
             CvInvoke.Imwrite(fileName, imgUndis);
         }
 }
Пример #8
0
        private async void OnFrameCapturedAsync(byte[] pixelArray, int frameWidth, int frameHeight, int frameId)
        {
            _videoEngine.Messenger.FrameCaptured -= OnFrameCapturedAsync;
            System.Diagnostics.Debug.WriteLine("OnFrameCapturedAsync");

            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                WriteableBitmap bitmap = await ImageProcessingUtils.PixelArrayToWriteableBitmapAsync(pixelArray, frameWidth, frameHeight);

                if (bitmap != null)
                {
                    CapturePhoto(bitmap);
                    //capturedPhotoImage.Source = bitmap;
                    //bitmap.Invalidate();
                    //capturedPhotoImage.Visibility = Visibility.Visible;
                }

                _videoEngine.Messenger.FrameCaptured += OnFrameCapturedAsync;
            });
        }
Пример #9
0
        private void cbFOV_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!mLoaded)
            {
                return;
            }
            if (mMark == null)
            {
                CaptureMark();
            }
            int id = -1;

            this.Dispatcher.Invoke(() =>
            {
                id = cbFOV.SelectedIndex;
            });

            if (cbFOV.SelectedIndex > -1)
            {
                System.Drawing.Point fov = mAnchorFOV[id];
                int x = fov.X;
                int y = fov.Y;

                bool lightStrobe = !Convert.ToBoolean(mParam.LIGHT_MODE);
                if (mImage != null)
                {
                    mImage.Dispose();
                    mImage = null;
                }
                mLog.Info(string.Format("{0}, Position Name : {1},  X = {2}, Y = {3}", "Moving TOP Axis", "FOV " + (id + 1).ToString(), x, y));
                using (Image <Bgr, byte> image = VI.CaptureImage.CaptureFOV(mPlcComm, mCamera, fov, 1000))
                {
                    mImage = ImageProcessingUtils.ImageRotation(image, new System.Drawing.Point(image.Width / 2, image.Height / 2), -mModel.AngleAxisCamera * Math.PI / 180.0).Copy();
                    mImage = ImageProcessingUtils.ImageTransformation(mImage, mMarkAdjust.X, mMarkAdjust.Y);
                    ShowDetail();
                }
            }
        }
Пример #10
0
        public void AutoLinkPad(CadFile Cad, int Mode, int Width = 800, int Height = 800)
        {
            List <Tuple <Point, int> > padsList = new List <Tuple <Point, int> >();

            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
            }
            Point cadCenterRotate = Cad.CenterRotation;
            List <Tuple <CadItem, int> > cadItemNotLink = new List <Tuple <CadItem, int> >();
            int    cadX     = Cad.X;
            int    cadY     = Cad.Y;
            double cadAngle = Cad.Angle;

            // filter Resistor and Capacitor component
            for (int t = 0; t < Cad.CadItems.Count; t++)
            {
                var    item             = Cad.CadItems[t];
                string name             = Convert.ToString(item.Name[0]);
                string nextName         = Convert.ToString(item.Name[1]);
                Point  cadCenter        = Point.Round(item.Center);
                Point  cadCenterRotated = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                var    sorted           = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated));
                if ((name.ToUpper() == "R" || name.ToUpper() == "C") && "0123456789".Contains(nextName))
                {
                    item.PadsIndex.Add(sorted.ElementAt(0).Item2);
                    item.PadsIndex.Add(sorted.ElementAt(1).Item2);
                    this.Gerber.PadItems[sorted.ElementAt(0).Item2].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[sorted.ElementAt(0).Item2].CadItemIndex = t;
                    this.Gerber.PadItems[sorted.ElementAt(1).Item2].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[sorted.ElementAt(1).Item2].CadItemIndex = t;
                }
                else
                {
                    if (name.ToUpper() != "S")
                    {
                        cadItemNotLink.Add(new Tuple <CadItem, int>(item, t));
                    }
                }
            }
            // reset pad list
            padsList.Clear();
            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                if (string.IsNullOrEmpty(this.Gerber.PadItems[i].CadFileID))
                {
                    padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
                }
            }
            if (Mode < 1)
            {
                return;
            }
            // filter only has two pad
            foreach (var itemtl in cadItemNotLink)
            {
                var    item                   = itemtl.Item1;
                string name                   = Convert.ToString(item.Name[0]);
                string nextName               = Convert.ToString(item.Name[1]);
                Point  cadCenter              = Point.Round(item.Center);
                Point  cadCenterRotated       = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                var    sorted                 = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated));
                Tuple <Point, int>[] arSorted = sorted.ToArray();
                List <int>           idGot    = new List <int>();
                int    limit                  = arSorted.Length > 10 ? 10 : arSorted.Length;
                double crDist                 = -1;
                for (int i = 0; i < limit - 1; i++)
                {
                    if (idGot.Contains(arSorted[i].Item2))
                    {
                        continue;
                    }
                    if (i == 1 && idGot.Count == 0)
                    {
                        break;
                    }
                    Point  p1 = arSorted[i].Item1;
                    double d1 = ImageProcessingUtils.DistanceTwoPoint(p1, cadCenterRotated);
                    if (idGot.Count > 0 && 2 * crDist < d1)
                    {
                        break;
                    }
                    if (d1 > this.DPI / 2)
                    {
                        break;
                    }
                    for (int j = i + 1; j < limit; j++)
                    {
                        Point  p2            = arSorted[j].Item1;
                        double d2            = ImageProcessingUtils.DistanceTwoPoint(p2, cadCenterRotated);
                        double deviationDist = Math.Max(d1, d2) > 0.05 * this.DPI ? 0.1 * Math.Min(d1, d2) : 0.03 * this.DPI;
                        if (Math.Abs(d1 - d2) > deviationDist || d2 > this.DPI / 2)
                        {
                            break;
                        }
                        Point ctP12 = new Point((p1.X + p2.X) / 2, (p1.Y + p2.Y) / 2);
                        if (Math.Abs(ctP12.X - cadCenterRotated.X) < 0.01 * this.DPI || Math.Abs(ctP12.Y - cadCenterRotated.Y) < 0.01 * this.DPI)
                        {
                            if (crDist != -1 && (Math.Abs(crDist - d1) > 0.5 * crDist || Math.Abs(crDist - d2) > 0.5 * crDist))
                            {
                                break;
                            }
                            crDist = (d1 + d2) / 2;
                            crDist = Math.Min(d1, d2);
                            idGot.Add(arSorted[i].Item2);
                            idGot.Add(arSorted[j].Item2);
                        }
                    }
                }
                if (idGot.Count == 2)
                {
                    for (int i = 0; i < idGot.Count; i++)
                    {
                        this.Gerber.PadItems[idGot[i]].CadFileID    = Cad.CadFileID;
                        this.Gerber.PadItems[idGot[i]].CadItemIndex = itemtl.Item2;
                        item.PadsIndex.Add(idGot[i]);
                    }
                }
            }
            // reset pads and cad list
            padsList.Clear();
            cadItemNotLink.Clear();
            if (Mode < 2)
            {
                return;
            }
            //get all pad
            for (int i = 0; i < this.Gerber.PadItems.Count; i++)
            {
                padsList.Add(new Tuple <Point, int>(this.Gerber.PadItems[i].Center, i));
            }
            for (int i = 0; i < Cad.CadItems.Count; i++)
            {
                if (Cad.CadItems[i].PadsIndex.Count == 0 && Cad.CadItems[i].Name[0].ToString().ToUpper() != "S")
                {
                    cadItemNotLink.Add(new Tuple <CadItem, int>(Cad.CadItems[i], i));
                }
            }
            foreach (var itemtl in cadItemNotLink)
            {
                var    item             = itemtl.Item1;
                string name             = Convert.ToString(item.Name[0]);
                string nextName         = Convert.ToString(item.Name[1]);
                Point  cadCenter        = Point.Round(item.Center);
                Point  cadCenterRotated = CadItem.GetCenterRotated(cadCenter, cadCenterRotate, cadX, cadY, cadAngle);
                double angle            = Math.Abs(item.Angle % 90);
                angle = angle > 45 ? 90 - angle : angle;
                var sorted = padsList.OrderBy(i => ImageProcessingUtils.DistanceTwoPoint(i.Item1, cadCenterRotated) * Math.Cos(angle * Math.PI / 180.0));

                Tuple <Point, int>[] arSorted = sorted.ToArray();
                int        limit = arSorted.Length > 1000 ? 1000 : arSorted.Length;
                List <int> idGot = new List <int>();
                //double deviationDist = Math.Max(d1, d2) > 0.05 * this.DPI ? 0.1 * Math.Min(d1, d2) : 0.03 * this.DPI;
                for (int i = 0; i < limit - 1; i++)
                {
                    if (!string.IsNullOrEmpty(this.Gerber.PadItems[arSorted[i].Item2].CadFileID))
                    {
                        break;
                    }
                    double d1 = ImageProcessingUtils.DistanceTwoPoint(arSorted[i].Item1, cadCenterRotated);
                    if (d1 > this.DPI)
                    {
                        break;
                    }
                    idGot.Add(arSorted[i].Item2);
                }
                for (int i = 0; i < idGot.Count; i++)
                {
                    this.Gerber.PadItems[idGot[i]].CadFileID    = Cad.CadFileID;
                    this.Gerber.PadItems[idGot[i]].CadItemIndex = itemtl.Item2;
                    item.PadsIndex.Add(idGot[i]);
                }
            }
        }
Пример #11
0
        public List <Tuple <CadFile, int> > GetSuggestCadItemName(Rectangle Rect, bool GetAllComponent = false)
        {
            List <Tuple <CadFile, int> > suggest = new List <Tuple <CadFile, int> >();

            if (Rect != Rectangle.Empty)
            {
                Thread t1 = new Thread(() => {
                    // find pads in rect
                    for (int i = 0; i < this.Gerber.PadItems.Count; i++)
                    {
                        var item = this.Gerber.PadItems[i];
                        if (item.CadItemIndex >= 0)
                        {
                            Rectangle bound = new Rectangle(item.Center.X, item.Center.Y, 1, 1);
                            if (Rect.Contains(bound))
                            {
                                for (int j = 0; j < this.Cad.Count; j++)
                                {
                                    if (this.Cad[j].CadFileID == item.CadFileID)
                                    {
                                        Tuple <CadFile, int> tlCad = new Tuple <CadFile, int>(this.Cad[j], item.CadItemIndex);
                                        lock (suggest)
                                        {
                                            if (!suggest.Contains(tlCad))
                                            {
                                                suggest.Add(tlCad);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                });
                // cad layer
                Thread t2 = new Thread(() => {
                    foreach (var item in this.Cad)
                    {
                        for (int i = 0; i < item.CadItems.Count; i++)
                        {
                            Point ct          = Point.Round(item.CadItems[i].Center);
                            ct.X             += item.X;
                            ct.Y             += item.Y;
                            Point newCtRotate = ImageProcessingUtils.PointRotation(ct, new Point((int)item.CenterRotation.X + item.X, (int)item.CenterRotation.Y + item.Y), item.Angle * Math.PI / 180.0);
                            Rectangle bound   = new Rectangle(newCtRotate.X, newCtRotate.Y, 1, 1);
                            if (Rect.Contains(bound))
                            {
                                Tuple <CadFile, int> tlCad = new Tuple <CadFile, int>(item, i);
                                lock (suggest)
                                {
                                    if (!suggest.Contains(tlCad) || i == item.CadItems.Count - 1)
                                    {
                                        suggest.Add(tlCad);
                                    }
                                }
                            }
                        }
                    }
                    foreach (var item in this.Cad)
                    {
                        for (int i = item.CadItems.Count - 1; i >= 0; i--)
                        {
                            Tuple <CadFile, int> tlCad = new Tuple <CadFile, int>(item, i);
                            if (!suggest.Contains(tlCad))
                            {
                                suggest.Add(tlCad);
                                if (!GetAllComponent)
                                {
                                    break;
                                }
                            }
                        }
                    }
                });
                t1.Start();
                t2.Start();
                t1.Join();
                t2.Join();
            }
            return(suggest);
        }
Пример #12
0
 public Image <Gray, byte> RotateImage(Image <Gray, byte> ImgInput, double Angle)
 {
     return(ImageProcessingUtils.ImageRotation(ImgInput, new Point(ImgInput.Width / 2, ImgInput.Height / 2), Angle));
 }
Пример #13
0
        public static Image <Bgr, byte> GetLayoutImage(Model model, ActionMode mode)
        {
            //Stopwatch sw = new Stopwatch();
            //sw.Start();
            Image <Bgr, byte> img = null;

            if (model.Gerber is GerberFile)
            {
                if (model.Gerber.Visible)
                {
                    switch (mode)
                    {
                    case ActionMode.Render:
                        DrawColor(model);
                        break;

                    case ActionMode.Rotation:
                        DrawColor(model);
                        break;

                    case ActionMode.Update_Color_Gerber:
                        DrawColor(model);
                        break;

                    case ActionMode.Draw_Cad:
                        break;

                    case ActionMode.Select_Pad:
                        break;

                    default:
                        break;
                    }
                }
                //Console.WriteLine(sw.ElapsedMilliseconds);
                if (model.Gerber.Visible)
                {
                    img = model.ImgGerberProcessedBgr.Copy();
                    if (model.Gerber.SelectPad != Rectangle.Empty)
                    {
                        HightLightSelectPad(img, model);
                    }
                }

                else
                {
                    img = new Image <Bgr, byte>(model.ImgGerberProcessedBgr.Size);
                }
                foreach (CadFile item in model.Cad)
                {
                    if (item.Visible)
                    {
                        int    x     = item.X;
                        int    y     = item.Y;
                        double angle = item.Angle * Math.PI / 180.0;
                        Color  cl    = item.Color;
                        foreach (CadItem caditem in item.CadItems)
                        {
                            Point  ct  = Point.Round(caditem.Center);
                            string txt = caditem.Name;
                            ct.X += x;
                            ct.Y += y;
                            Point     newCtRotate = ImageProcessingUtils.PointRotation(ct, new Point(item.CenterRotation.X + item.X, item.CenterRotation.Y + item.Y), angle);
                            MCvScalar color       = new MCvScalar(cl.B, cl.G, cl.R);
                            if (item.SelectCenter != Rectangle.Empty)
                            {
                                Rectangle bound = new Rectangle(newCtRotate.X, newCtRotate.Y, 1, 1);
                                if (item.SelectCenter.Contains(bound))
                                {
                                    color = new MCvScalar(255, 255, 255);
                                }
                            }
                            if (model.ShowComponentCenter)
                            {
                                CvInvoke.Circle(img, newCtRotate, 3, color, -1);
                            }
                            if (model.ShowLinkLine)
                            {
                                if (caditem.Name != "UNDEFINE")
                                {
                                    for (int i = 0; i < caditem.PadsIndex.Count; i++)
                                    {
                                        CvInvoke.Line(img, newCtRotate, model.Gerber.PadItems[caditem.PadsIndex[i]].Center, new MCvScalar(0, 255, 0), 1);
                                    }
                                }
                            }
                            if (model.ShowComponentName)
                            {
                                newCtRotate.X += 5;
                                CvInvoke.PutText(img, txt, newCtRotate, Emgu.CV.CvEnum.FontFace.HersheyDuplex, 0.5, color, 1, Emgu.CV.CvEnum.LineType.Filled);
                            }
                        }
                    }
                }
                if (model.Gerber.Visible)
                {
                    HightLightMarkPoint(img, model);
                }
            }

            return(img);
        }