Exemplo n.º 1
0
        public static double templateMatch()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            int resultNum = 1000;

            float[] X1 = new float[1000];
            float[] X2 = new float[1000];
            float[] Y1 = new float[1000];
            float[] Y2 = new float[1000];
            X1[0] = rect.X + rect.Width / 2;
            X2[0] = rect.X + rect.Width;
            Y1[0] = rect.Y + rect.Height / 2;
            Y2[0] = rect.Y + rect.Height;
            float[] Alpha = new float[1000];

            t1 = DateTime.Now;
            templateMatchDLL(tempFilename, objFilename, ref resultNum, X1, Y1, X2, Y2, Alpha);
            t2 = DateTime.Now;

            for (int i = 0; i < resultNum; i++)
            {
                ImageShow.addRect(new PointF(X1[i], Y1[i]), new PointF(X2[i], Y2[i]), Alpha[i], true);
            }

            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 2
0
        public override void mouseDrag(PointF p1, PointF p2, PointF p3)
        {
            float x1 = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y1 = (p1.Y - ImageShow.position.Y) / ImageShow.scale;
            float x2 = (p3.X - ImageShow.position.X) / ImageShow.scale;
            float y2 = (p3.Y - ImageShow.position.Y) / ImageShow.scale;

            ImageShow.editLast(new PointF(x1, y1), new PointF(x2, y2), 0);
        }
Exemplo n.º 3
0
        public override void mouseDrag(PointF p1, PointF p2, PointF p3)
        {
            float x  = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y  = (p1.Y - ImageShow.position.Y) / ImageShow.scale;
            float xr = (p3.X - ImageShow.position.X) / ImageShow.scale;
            float yr = (p3.Y - ImageShow.position.Y) / ImageShow.scale;

            ImageShow.editLast(new PointF(x, y), new PointF(x, y), (float)Math.Sqrt((xr - x) * (xr - x) + (yr - y) * (yr - y)));
        }
Exemplo n.º 4
0
        public override void mouseDown(PointF p1)
        {
            float x = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y = (p1.Y - ImageShow.position.Y) / ImageShow.scale;

            ImageShow.addCircle(new PointF(x, y), 1);
            if (figureDetect.setCircleParaState)
            {
                figureDetect.shapeIndex = ImageShow.shapes.Count - 1;
            }
        }
Exemplo n.º 5
0
        public override void mouseDown(PointF p1)
        {
            float x = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y = (p1.Y - ImageShow.position.Y) / ImageShow.scale;

            ImageShow.addLine(new PointF(x, y), 100);

            if (DefectDetect.selectLineState)
            {
                DefectDetect.selectLineIndex = ImageShow.shapes.Count - 1;
            }
        }
Exemplo n.º 6
0
        public static double detectDefect(ref int isOK)
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            //tempSelectPointIndex.Add(new PointF((float)1152.76, (float)1154.42));
            //tempSelectPointIndex.Add(new PointF((float)1506.53, (float)1118.48));
            //tempSelectPointIndex.Add(new PointF((float)1503.34, (float)1347.21));
            //tempSelectPointIndex.Add(new PointF((float)1166.15, (float)1367.25));

            //selectPointIndex.Add(new PointF((float)1079.04, (float)1087.97));
            //selectPointIndex.Add(new PointF((float)1390.66, (float)1264.99));
            //selectPointIndex.Add(new PointF((float)1254.86, (float)1449.18));
            //selectPointIndex.Add(new PointF((float)966.40, (float)1268.93));

            if (templateFile != null && objectFile != null && tempSelectPointIndex.Count() == 4 && selectPointIndex.Count() == 4)
            {
                int   resultNum = 1000;
                int[] X         = new int[resultNum];
                int[] Y         = new int[resultNum];
                int[] W         = new int[resultNum];
                int[] H         = new int[resultNum];

                t1 = DateTime.Now;
                defectDetect(templateFile, objectFile,
                             tempSelectPointIndex[0].X, tempSelectPointIndex[0].Y,
                             tempSelectPointIndex[1].X, tempSelectPointIndex[1].Y,
                             tempSelectPointIndex[2].X, tempSelectPointIndex[2].Y,
                             tempSelectPointIndex[3].X, tempSelectPointIndex[3].Y,
                             selectPointIndex[0].X, selectPointIndex[0].Y,
                             selectPointIndex[1].X, selectPointIndex[1].Y,
                             selectPointIndex[2].X, selectPointIndex[2].Y,
                             selectPointIndex[3].X, selectPointIndex[3].Y,
                             colorThr, areaThr, ref resultNum, X, Y, W, H);
                t2 = DateTime.Now;

                for (int i = 0; i < resultNum; i++)
                {
                    ImageShow.addRect(new PointF((float)(X[i] + (double)W[i] / 2.0), (float)(Y[i] + (double)H[i] / 2.0)), new PointF((float)X[i], (float)Y[i]), 0, false);
                }

                if (resultNum > 0)
                {
                    isOK = 2;
                }
                else
                {
                    isOK = 1;
                }
            }
            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 7
0
        public static double undistort()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            ImageShow.cls();

            if (ImageShow.nowFile != null)
            {
                byte[] data    = new byte[ImageShow.source.Width * ImageShow.source.Height * 3];
                int    channel = 3;

                t1 = DateTime.Now;

                runUndistort(ImageShow.nowFile, ImageShow.source.Width * ImageShow.source.Height * 3, data, paraNum, para, ref channel);

                t2 = DateTime.Now;

                Rectangle rect = new Rectangle(0, 0, ImageShow.source.Width, ImageShow.source.Height);
                System.Drawing.Imaging.BitmapData bmpData = ImageShow.source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, ImageShow.source.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * ImageShow.source.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                channel = Bitmap.GetPixelFormatSize(ImageShow.source.PixelFormat);
                if (channel == 8)
                {
                    for (int counter = 0; counter < ImageShow.source.Width * ImageShow.source.Height; counter++)
                    {
                        rgbValues[counter] = data[counter * 3];
                    }
                }
                else
                {
                    for (int counter = 0; counter < ImageShow.source.Width * ImageShow.source.Height * 3; counter++)
                    {
                        rgbValues[counter] = data[counter];
                    }
                }

                System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes);

                ImageShow.source.UnlockBits(bmpData);
            }
            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 8
0
        public override void mouseDown(PointF p1)
        {
            float x1 = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y1 = (p1.Y - ImageShow.position.Y) / ImageShow.scale;

            ImageShow.addRect(new PointF(x1, y1), new PointF(x1, y1), 0, true);
            if (figureDetect.setRectParaState)
            {
                figureDetect.shapeIndex = ImageShow.shapes.Count - 1;
            }
            if (CodeRecog.setRectParaState > 0)
            {
                CodeRecog.shapeIndex = ImageShow.shapes.Count - 1;
            }
        }
Exemplo n.º 9
0
        public static double callTemplateDetect()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            if (ImageShow.nowFile != null && filename != null && detectROI != new Rectangle(0, 0, 0, 0))
            {
                ImageShow.source = new Bitmap(ImageShow.nowFile);
                Rectangle rect = new Rectangle(0, 0, ImageShow.source.Width, ImageShow.source.Height);
                System.Drawing.Imaging.BitmapData bmpData = ImageShow.source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, ImageShow.source.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * ImageShow.source.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                byte[] data = new byte[detectROI.Width * (bmpData.Stride / bmpData.Width) * detectROI.Height];
                for (int i = 0; i < detectROI.Height; i++)
                {
                    for (int j = 0; j < detectROI.Width * (bmpData.Stride / bmpData.Width); j++)
                    {
                        data[i * detectROI.Width * (bmpData.Stride / bmpData.Width) + j] = rgbValues[(i + detectROI.Y) * bmpData.Stride + j + detectROI.X * (bmpData.Stride / bmpData.Width)];
                    }
                }


                int     resultNum = 1000;
                int[]   resultX   = new int[resultNum];
                int[]   resultY   = new int[resultNum];
                int[]   dir       = new int[resultNum];
                float[] rate      = new float[resultNum];

                t1 = DateTime.Now;
                templateDetect(detectROI.Width, detectROI.Height, detectROI.Height * detectROI.Width * (bmpData.Stride / bmpData.Width), data, filename, ref resultNum, resultX, resultY, dir, rate);
                t2 = DateTime.Now;

                for (int i = 0; i < resultNum; i++)
                {
                    ImageShow.addRect(new PointF((float)(resultX[i] + detectROI.X), (float)(resultY[i] + detectROI.Y)),
                                      new PointF((float)(resultX[i] + detectROI.X + Math.Sqrt(w * w / 4 + h * h / 4) * Math.Cos((double)dir[i] * Math.PI / 180.0 + Math.Atan((double)h / (double)w))),
                                                 (float)(resultY[i] + detectROI.Y + Math.Sqrt(w * w / 4 + h * h / 4) * Math.Sin((double)dir[i] * Math.PI / 180.0 + Math.Atan((double)h / (double)w)))), (float)(dir[i] * Math.PI / 180.0), false);
                }

                ImageShow.source.UnlockBits(bmpData);
            }
            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 10
0
        public static double runManualExtract()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            ImageShow.cls();

            if (ImageShow.nowFile != null)
            {
                int     w         = boardWidth;
                int     h         = boardHeight;
                int     cornerNum = w * h;
                float[] cornerX   = new float[cornerNum], cornerY = new float[cornerNum];
                for (int i = 0; i < 4; i++)
                {
                    cornerX[i] = manualCorner[i].X;
                    cornerY[i] = manualCorner[i].Y;
                }

                t1 = DateTime.Now;

                CameraCalibrate.manualExtract(ImageShow.nowFile, ref cornerNum, cornerX, cornerY, w, h, manualFixSize, fixSize);

                t2 = DateTime.Now;

                CameraCalibrate.nowCornerNum = cornerNum;
                if (CameraCalibrate.nowCornerX == null)
                {
                    CameraCalibrate.nowCornerX = new float[cornerNum];
                    CameraCalibrate.nowCornerY = new float[cornerNum];
                }
                for (int i = 0; i < cornerNum; i++)
                {
                    //float x = cornerX[i] * ImageShow.position.Width / ImageShow.initPosition.Width + ImageShow.position.X;
                    //float y = cornerY[i] * ImageShow.position.Height / ImageShow.initPosition.Height + ImageShow.position.Y;
                    //float r = 10 * ImageShow.scale;
                    ImageShow.addPoint(new PointF(cornerX[i], cornerY[i]));
                    CameraCalibrate.nowCornerX[i] = cornerX[i];
                    CameraCalibrate.nowCornerY[i] = cornerY[i];
                }
                CameraCalibrate.isUpdated = true;
            }

            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 11
0
        public static double callSobel()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            if (ImageShow.nowFile != null)
            {
                ImageShow.source = new Bitmap(ImageShow.nowFile);
                Rectangle rect = new Rectangle(0, 0, ImageShow.source.Width, ImageShow.source.Height);
                System.Drawing.Imaging.BitmapData bmpData = ImageShow.source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, ImageShow.source.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * ImageShow.source.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                if (sobelOTSU)
                {
                    sobelThr = -1;
                }

                int[] edgeX   = new int[bytes];
                int[] edgeY   = new int[bytes];
                int   edgeNum = 0;

                t1 = DateTime.Now;
                sobel(ImageShow.source.Width, ImageShow.source.Height, bytes, rgbValues, sobelKsize, ref sobelThr, ref edgeNum, edgeX, edgeY);
                t2 = DateTime.Now;

                while (ImageShow.edgeDots != null && ImageShow.edgeDots.Count > 0)
                {
                    ImageShow.edgeDots.RemoveAt(ImageShow.edgeDots.Count - 1);
                }
                for (int i = 0; i < edgeNum; i++)
                {
                    ImageShow.addDot(new Point(edgeX[i], edgeY[i]));
                }
                ImageShow.source.UnlockBits(bmpData);
            }

            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 12
0
        public override void mouseDrag(PointF p1, PointF p2, PointF p3)
        {
            float x1 = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y1 = (p1.Y - ImageShow.position.Y) / ImageShow.scale;
            float x2 = (p3.X - ImageShow.position.X) / ImageShow.scale;
            float y2 = (p3.Y - ImageShow.position.Y) / ImageShow.scale;
            float xr = x2 - x1;
            float yr = y2 - y1;

            float r = 0;

            if (xr == 0)
            {
                if (yr >= 0)
                {
                    r = (float)Math.PI / 2;
                }
                else
                {
                    r = (float)Math.PI / 2 * 3;
                }
            }
            else
            {
                r = (float)Math.Atan(yr / xr);
                if (xr < 0)
                {
                    r += (float)Math.PI;
                }
                else
                {
                    if (yr < 0)
                    {
                        r += (float)Math.PI * 2;
                    }
                }
            }
            ImageShow.editLast(new PointF(x1, y1), new PointF(x2, y2), r);
        }
Exemplo n.º 13
0
        public static void callTemplateFinetune()
        {
            if (ImageShow.nowFile != null)
            {
                ImageShow.source = new Bitmap(ImageShow.nowFile);
                Rectangle rect = new Rectangle(0, 0, ImageShow.source.Width, ImageShow.source.Height);
                System.Drawing.Imaging.BitmapData bmpData = ImageShow.source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, ImageShow.source.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * ImageShow.source.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                byte[] data = new byte[templateROI.Width * (bmpData.Stride / bmpData.Width) * templateROI.Height];
                for (int i = 0; i < templateROI.Height; i++)
                {
                    for (int j = 0; j < templateROI.Width * (bmpData.Stride / bmpData.Width); j++)
                    {
                        data[i * templateROI.Width * (bmpData.Stride / bmpData.Width) + j] = rgbValues[(i + templateROI.Y) * bmpData.Stride + j + templateROI.X * (bmpData.Stride / bmpData.Width)];
                    }
                }

                int   edgeNum = templateROI.Width * templateROI.Height;
                int[] edgeX   = new int[edgeNum];
                int[] edgeY   = new int[edgeNum];
                templateFinetune(templateROI.Width, templateROI.Height, templateROI.Height * templateROI.Width * (bmpData.Stride / bmpData.Width), data, thr, ref edgeNum, edgeX, edgeY);

                ImageShow.edgeDots.Clear();
                for (int i = 0; i < edgeNum; i++)
                {
                    ImageShow.addDot(new Point(edgeX[i] + templateROI.X, edgeY[i] + templateROI.Y));
                }

                ImageShow.source.UnlockBits(bmpData);
            }
        }
Exemplo n.º 14
0
        public static double newEdgeDetectFun()
        {
            DateTime t1 = DateTime.Now, t2 = t1;
            int      imgSize = ImageShow.source.Width * ImageShow.source.Height;

            byte[] data = new byte[imgSize];

            t1 = DateTime.Now;

            newEdgeDetect(ImageShow.nowFile, edgeDetectStep, edgeDetectThr, imgSize, data);

            t2 = DateTime.Now;

            for (int i = 0; i < imgSize; i++)
            {
                if (data[i] == 254)
                {
                    ImageShow.addDot(new Point(i % ImageShow.source.Width, i / ImageShow.source.Width));
                }
            }

            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 15
0
        public static double callTriangleDetect()
        {
            DateTime t1 = DateTime.Now, t2 = t1;

            if (ImageShow.nowFile != null)
            {
                ImageShow.source = new Bitmap(ImageShow.nowFile);
                Rectangle rect = new Rectangle(0, 0, ImageShow.source.Width, ImageShow.source.Height);
                System.Drawing.Imaging.BitmapData bmpData = ImageShow.source.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, ImageShow.source.PixelFormat);

                IntPtr ptr = bmpData.Scan0;

                int    bytes     = Math.Abs(bmpData.Stride) * ImageShow.source.Height;
                byte[] rgbValues = new byte[bytes];

                System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes);

                int     resultNum = 1000;
                float[] resultX   = new float[resultNum * 3];
                float[] resultY   = new float[resultNum * 3];

                t1 = DateTime.Now;
                triangleDetect(ImageShow.source.Width, ImageShow.source.Height, bytes, rgbValues, angle1, angle2, angle3, range, triangleLineThr, minLength, maxGap, ref resultNum, resultX, resultY);
                t2 = DateTime.Now;

                for (int i = 0; i < resultNum; i++)
                {
                    //Console.WriteLine(resultX[i * 3].ToString()+","+resultY[i * 3].ToString()+","+resultX[i * 3 + 1].ToString()+","+ resultY[i * 3 + 1].ToString()+","+resultX[i * 3 + 2].ToString()+","+ resultY[i * 3 + 2].ToString());
                    ImageShow.addTriangle(new PointF(resultX[i * 3], resultY[i * 3]), new PointF(resultX[i * 3 + 1], resultY[i * 3 + 1]), new PointF(resultX[i * 3 + 2], resultY[i * 3 + 2]));
                }

                ImageShow.source.UnlockBits(bmpData);
            }

            return((t2 - t1).TotalMilliseconds);
        }
Exemplo n.º 16
0
        public override double mouseClick(PointF p1)
        {
            float x = (p1.X - ImageShow.position.X) / ImageShow.scale;
            float y = (p1.Y - ImageShow.position.Y) / ImageShow.scale;

            List <int> lines = new List <int>();

            foreach (var shape in ImageShow.shapes)
            {
                if (shape.onShape(p1) > 10)
                {
                    lines.Add(ImageShow.shapes.IndexOf(shape));
                }
            }

            if (lines.Count > 1)
            {
                float x0     = ImageShow.shapes[lines[0]].P1.X;
                float y0     = ImageShow.shapes[lines[0]].P1.Y;
                float x1     = ImageShow.shapes[lines[1]].P1.X;
                float y1     = ImageShow.shapes[lines[1]].P1.Y;
                float angle0 = ImageShow.shapes[lines[0]].R;
                float angle1 = ImageShow.shapes[lines[1]].R;

                float c0 = (float)Math.Cos(angle0), s0 = (float)Math.Sin(angle0);
                float c1 = (float)Math.Cos(angle1), s1 = (float)Math.Sin(angle1);

                if (c0 == 0)
                {
                    if (c1 != 0)
                    {
                        x = x0;
                        y = (x - x1) * s1 / c1 + y1;
                    }
                }
                else if (c1 == 0)
                {
                    x = x1;
                    y = (x - x0) * s0 / c0 + y0;
                }
                else
                {
                    float k0 = s0 / c0;
                    float k1 = s1 / c1;

                    if (k0 != k1)
                    {
                        float b0 = y0 - x0 * k0;
                        float b1 = y1 - x1 * k1;
                        x = (b0 - b1) / (k1 - k0);
                        y = x * k0 + b0;
                    }
                }
            }

            ImageShow.addPoint(new PointF(x, y));

            if (DistanceMeasure.pointSelectState > 0)
            {
                DistanceMeasure.shapeIndex = ImageShow.shapes.Count - 1;
            }


            if (CameraCalibrate.manualExtractState > 0)
            {
                CameraCalibrate.manualCorner[4 - CameraCalibrate.manualExtractState] = new PointF(x, y);
                CameraCalibrate.manualExtractState--;
                if (CameraCalibrate.manualExtractState == 0)
                {
                    return(CameraCalibrate.runManualExtract());
                }
            }

            if (DefectDetect.selectPointState > 0)
            {
                DefectDetect.selectPointIndex.Add(new PointF(x, y));
                //DefectDetect.selectPointState--;
            }
            if (DefectDetect.tempSelectPointState > 0)
            {
                DefectDetect.tempSelectPointIndex.Add(new PointF(x, y));
                //DefectDetect.tempSelectPointState--;
            }
            return(0);
        }