private Bitmap cutLot(Bitmap blackCanvas, System.Drawing.Point[] ParkingPoint, int i) { Bitmap compoundImage = null; System.Drawing.Point[] parkingLot = { ParkingPoint[0 + (4 * i)], ParkingPoint[1 + (4 * i)], ParkingPoint[2 + (4 * i)], ParkingPoint[3 + (4 * i)] }; List<AForge.IntPoint> corners = new List<AForge.IntPoint>(); corners.Add(new AForge.IntPoint(ParkingPoint[3 + (4 * i)].X, ParkingPoint[3 + (4 * i)].Y)); corners.Add(new AForge.IntPoint(ParkingPoint[0 + (4 * i)].X, ParkingPoint[0 + (4 * i)].Y)); corners.Add(new AForge.IntPoint(ParkingPoint[1 + (4 * i)].X, ParkingPoint[1 + (4 * i)].Y)); corners.Add(new AForge.IntPoint(ParkingPoint[2 + (4 * i)].X, ParkingPoint[2 + (4 * i)].Y)); QuadrilateralTransformation Qfilter = new QuadrilateralTransformation(corners, 200, 200); //Bitmap frame = new Bitmap(@"currImage.jpg"); Bitmap frame = (Bitmap)currentFrame.Clone(); Bitmap nframe = Qfilter.Apply(frame); nframe.RotateFlip(RotateFlipType.Rotate180FlipY); BackwardQuadrilateralTransformation BQfilter = new BackwardQuadrilateralTransformation(nframe, corners); compoundImage = BQfilter.Apply(blackCanvas); return nframe; }
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; }