예제 #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 /// <remarks></remarks>
 public BoardDetector()
 {
     this.median         = new Median();
     this.quadFinder     = new QuadrilateralFinder();
     this.resize         = new ResizeBilinear(640, 480);
     this.boardCorners   = new List <IntPoint>();
     this.oldCorners     = new List <List <IntPoint> >();
     this.filteredBoard  = new Bitmap(640, 480);
     this.rectifiedBoard = new Bitmap(640, 480);
 }
        public static Bitmap TranformImage(Bitmap image)
        {
            List <IntPoint> corners;

            //using (Bitmap workingBitmap = (Bitmap)image.Clone())
            using (Bitmap grayscaleBitmap = ConvertToGreyscale(image))
            {
                DifferenceEdgeDetector detector = new DifferenceEdgeDetector();
                detector.ApplyInPlace(grayscaleBitmap);

                //grayscaleBitmap.Save(@"D:\k\trash\workingBitmap.jpg");

                //todo: threshold value
                new Threshold(20).ApplyInPlace(grayscaleBitmap);

                //grayscaleBitmap.Save(@"D:\k\trash\workingBitmapAfterThreshold.jpg");

                QuadrilateralFinder quadrilateralFinder = new QuadrilateralFinder();
                corners = quadrilateralFinder.ProcessImage(grayscaleBitmap);

                //todo del
                //using (Bitmap clone = workingBitmap.Clone(new Rectangle(0, 0, image.Width, image.Height), PixelFormat.Format32bppArgb))
                //{
                //    foreach (IntPoint corner in corners)
                //    {

                //        clone.SetPixel(corner.X, corner.Y, Color.Red);
                //        //g.
                //        //g.FillEllipse(Brushes.Red, corner.X -len, corner.Y + len, corner.X + len, corner.Y - len);
                //    }
                //    clone.Save(@"D:\k\trash\edgeDifference.jpg");
                //}
            }

            int boundingSquareSideLength = GetBoundingSquareSideLength(corners);
            QuadrilateralTransformation quadrilateralTransformation
                = new QuadrilateralTransformation(corners, boundingSquareSideLength, boundingSquareSideLength);

            Corners = corners;
            return(quadrilateralTransformation.Apply(image));
        }
예제 #3
0
        public mAnalyzeQuads(Bitmap InitialBitmap)
        {
            BitmapType = 0;

            QuadrilateralFinder cMethod = new QuadrilateralFinder();

            //Effect = new CornersMarker(cMethod, CornerColor);

            //Effect.Detector = cMethod;


            List <IntPoint> corners = cMethod.ProcessImage(InitialBitmap);

            foreach (IntPoint corner in corners)
            {
                Points.Add(new wPoint(corner.X, corner.Y));
            }

            Sequence.Clear();
            Sequence.Add(Effect);
        }
예제 #4
0
        public void picback()
        {
            Bitmap temp1;
            Bitmap temp2;
            Bitmap temp3;
            Bitmap temp4;
            Bitmap temp5;
            Bitmap temp6;
            Bitmap temp7;
            Bitmap temp8;
            Bitmap temp9;
            Bitmap temp10;

            Bitmap sourceImage;


            //新建轮廓过滤器
            CannyEdgeDetector filter = new CannyEdgeDetector();

            //生成颜色过滤器
            ColorFiltering colorFilter = new ColorFiltering();

            //将颜色过滤器设置为白色
            colorFilter.Red   = new IntRange(50, 255);
            colorFilter.Green = new IntRange(50, 255);
            colorFilter.Blue  = new IntRange(50, 255);

            //从摄像头中截取图像
            sourceImage = videoSourcePlayer1.GetCurrentVideoFrame();

            //将原图格式化复制
            temp1 = AForge.Imaging.Image.Clone(sourceImage, sourceImage.PixelFormat);
            //清除sourceImage占用
            sourceImage.Dispose();
            //sourceImage = temp1;

            int Height = temp1.Size.Height;
            int Width  = temp1.Size.Width;

            //pictureBox1是原图
            //pictureBox1.Image = temp1;

            //从temp1提取颜色
            temp2 = filter.Apply(temp1.PixelFormat != PixelFormat.Format8bppIndexed ?
                                 Grayscale.CommonAlgorithms.BT709.Apply(temp1) : temp1);
            //pictureBox2原图轮廓
            //pictureBox2.Image = temp2;


            //从temp1进行颜色过滤
            temp5 = colorFilter.Apply(temp1);
            //pictureBox5原图轮廓
            //pictureBox5.Image = temp5;

            //从temp5进行灰度转化
            temp3 = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp5);

            //pictureBox3灰度转化
            //pictureBox3.Image = temp3;

            //从temp3进行二值化
            temp4 = new Threshold(10).Apply(temp3);
            //pictureBox4是二值化后的图
            //pictureBox4.Image = temp4;

            //temp7去噪点后的图
            temp7 = new BlobsFiltering(40, 40, temp4.Width, temp4.Height).Apply(temp4);

            //pictureBox7.Image = temp7;

            //temp6先原图格式化复制
            temp6 = AForge.Imaging.Image.Clone(temp7, temp1.PixelFormat);
            temp8 = temp6;

            try
            {
                QuadrilateralFinder qf      = new QuadrilateralFinder();//获取三角形、四边形角点
                List <IntPoint>     corners = qf.ProcessImage(temp6);
                //进行角点转换
                corners = CornersChange(corners, temp6.Size.Width, temp6.Size.Height);
                //生成四角变换过滤器
                QuadrilateralTransformation filter2 = new QuadrilateralTransformation(corners, 1920, 1040);
                //对原图temp1进行四角型变换
                temp8 = filter2.Apply(temp1);
            }
            catch
            {
            }
            //temp9为temp8的复制
            temp9 = AForge.Imaging.Image.Clone(temp8, temp1.PixelFormat);
            //pictureBox8.Image = temp8;

            //生成一个新的过滤器
            ColorFiltering colorFilter2 = new ColorFiltering();

            colorFilter2.Red   = new IntRange(100, 255);
            colorFilter2.Green = new IntRange(100, 255);
            colorFilter2.Blue  = new IntRange(0, 90);

            //提取颜色
            temp5 = colorFilter2.Apply(temp9);

            //灰度转化
            temp3 = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp5);

            //二值化
            temp4 = new Threshold(10).Apply(temp3);

            //去噪点
            temp7 = new BlobsFiltering(40, 40, temp4.Width, temp4.Height).Apply(temp4);

            temp6             = AForge.Imaging.Image.Clone(temp7, temp9.PixelFormat);
            temp10            = AForge.Imaging.Image.Clone(temp6, temp6.PixelFormat);
            pictureBox8.Image = temp10;
            try
            {
                QuadrilateralFinder qf      = new QuadrilateralFinder();//获取三角形、四边形角点
                List <IntPoint>     corners = qf.ProcessImage(temp6);

                corners = CornersChange(corners, temp6.Size.Width, temp6.Size.Height);



                Rectangle rect = new Rectangle();
                rect = Screen.GetWorkingArea(this);



                string path = OriPath + "\\SourceInputImage.jpg";

                Bitmap bt = new Bitmap(path);
                //初始化一个和屏幕面积一样大小的bitmap且格式和bt一样
                DisplayBitmap = new Bitmap(rect.Width, rect.Height, bt.PixelFormat);

                Graphics g = Graphics.FromImage(DisplayBitmap);

                g.FillRectangle(Brushes.White, new Rectangle(0, 0, rect.Width, rect.Height));//这句实现填充矩形的功能

                AForge.Imaging.Filters.BackwardQuadrilateralTransformation Bfilter = new AForge.Imaging.Filters.BackwardQuadrilateralTransformation(bt, corners);

                temp10 = Bfilter.Apply(DisplayBitmap);


                //string testsavepath = OriPath + "\\SourcePic.bmp";
                //DisplayBitmap.Save(testsavepath);

                /*
                 * BitmapData data = temp6.LockBits(new Rectangle(0, 0, temp6.Width, temp6.Height),
                 *  ImageLockMode.ReadWrite, temp6.PixelFormat);
                 * Drawing.Polygon(data, corners, Color.Red);
                 * for (int i = 0; i < corners.Count; i++)
                 * {
                 *  Drawing.FillRectangle(data,
                 *      new Rectangle(corners[i].X - 2, corners[i].Y - 2, 10, 10),
                 *      Color.Red);
                 * }
                 *
                 * temp6.UnlockBits(data);
                 */
            }
            catch
            {
            }



            pictureBox9.Image = temp10;
        }
예제 #5
0
        public void picshow()
        {
            Bitmap temp1;

            Bitmap temp2;
            Bitmap temp3;
            Bitmap temp4;
            Bitmap temp5;

            Bitmap temp7;
            Bitmap temp8;
            Bitmap temp9;

            Bitmap sourceImage;


            //新建轮廓过滤器
            CannyEdgeDetector filter = new CannyEdgeDetector();

            //生成颜色过滤器
            ColorFiltering colorFilter = new ColorFiltering();

            //白色

            colorFilter.Red   = new IntRange(50, 255);
            colorFilter.Green = new IntRange(50, 255);
            colorFilter.Blue  = new IntRange(50, 255);


            //从摄像头中截取图像
            sourceImage = videoSourcePlayer1.GetCurrentVideoFrame();

            //将原图格式化复制
            temp1 = AForge.Imaging.Image.Clone(sourceImage, sourceImage.PixelFormat);
            sourceImage.Dispose();
            sourceImage = temp1;

            int Height = sourceImage.Size.Height;
            int Width  = sourceImage.Size.Width;

            //pictureBox1是原图
            pictureBox1.Image = temp1;


            //pictureBox2原图轮廓
            temp2 = filter.Apply(sourceImage.PixelFormat != PixelFormat.Format8bppIndexed ?
                                 Grayscale.CommonAlgorithms.BT709.Apply(sourceImage) : sourceImage);

            pictureBox2.Image = temp2;


            //pictureBox5提取颜色后的图

            temp5 = colorFilter.Apply(temp1);

            pictureBox5.Image = temp5;

            //pictureBox3灰度转化后的图
            temp3 = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp5);


            pictureBox3.Image = temp3;

            //pictureBox4二值化后的图

            temp4 = new Threshold(10).Apply(temp3);

            pictureBox4.Image = temp4;
            //pictureBox7去噪点后的图
            temp7 = new BlobsFiltering(40, 40, temp4.Width, temp4.Height).Apply(temp4);

            pictureBox7.Image = temp7;
            Bitmap temp6 = AForge.Imaging.Image.Clone(temp7, temp1.PixelFormat);

            temp8 = temp6;

            try
            {
                QuadrilateralFinder qf      = new QuadrilateralFinder();//获取三角形、四边形角点
                List <IntPoint>     corners = qf.ProcessImage(temp6);

                /*
                 * BlobCounter extractor = new BlobCounter();
                 * extractor.FilterBlobs = true;
                 * extractor.MinWidth = extractor.MinHeight = 150;
                 * extractor.MaxWidth = extractor.MaxHeight = 350;
                 * extractor.ProcessImage(temp6);
                 *
                 * foreach (Blob blob in extractor.GetObjectsInformation())
                 * {
                 *  // 获取边缘点
                 *  List<IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob);
                 *  // 利用边缘点,在原始图像上找到四角
                 *  corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                 * }
                 */
                corners = CornersChange(corners, temp6.Size.Width, temp6.Size.Height);

                QuadrilateralTransformation filter2 = new QuadrilateralTransformation(corners, 384, 216);

                temp8 = filter2.Apply(temp1);
            }
            catch
            {
            }
            //pictureBox8原图中的投影经过四边形转换后的图
            temp9             = AForge.Imaging.Image.Clone(temp8, temp1.PixelFormat);
            pictureBox8.Image = temp8;

            //亮黄
            ColorFiltering colorFilter2 = new ColorFiltering();

            colorFilter2.Red   = new IntRange(100, 255);
            colorFilter2.Green = new IntRange(100, 255);
            colorFilter2.Blue  = new IntRange(0, 90);

            //提取颜色

            temp5 = colorFilter2.Apply(temp9);

            pictureBox5.Image = temp5;

            //灰度转化
            temp3 = new Grayscale(0.2125, 0.7154, 0.0721).Apply(temp5);


            pictureBox3.Image = temp3;

            //二值化

            temp4 = new Threshold(10).Apply(temp3);

            //去噪点
            temp7 = new BlobsFiltering(40, 40, temp4.Width, temp4.Height).Apply(temp4);

            temp6 = AForge.Imaging.Image.Clone(temp7, temp9.PixelFormat);
            temp9 = temp6;

            try
            {
                QuadrilateralFinder qf      = new QuadrilateralFinder();//获取三角形、四边形角点
                List <IntPoint>     corners = qf.ProcessImage(temp6);

                corners = CornersChange(corners, temp6.Size.Width, temp6.Size.Height);

                QuadrilateralTransformation filter2 = new QuadrilateralTransformation(corners, 384, 216);


                BitmapData data = temp6.LockBits(new Rectangle(0, 0, temp6.Width, temp6.Height),
                                                 ImageLockMode.ReadWrite, temp6.PixelFormat);
                Drawing.Polygon(data, corners, Color.Red);
                for (int i = 0; i < corners.Count; i++)
                {
                    Drawing.FillRectangle(data,
                                          new Rectangle(corners[i].X - 2, corners[i].Y - 2, 10, 10),
                                          Color.Red);
                }
                float juli = (corners[0].Y + corners[3].Y - corners[1].Y - corners[2].Y) / 2;

                label1.Text = ((int)((400 - juli) / 7.5)).ToString();
                temp6.UnlockBits(data);
            }
            catch
            {
            }

            pictureBox9.Image = temp9;
        }