示例#1
0
        /// <summary>
        /// 重置图片的指定大小并且居中
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List <Bitmap> ToResizeAndCenterIt(List <Bitmap> list, int w = 20, int h = 20)
        {
            List <Bitmap> resizeList = new List <Bitmap>();


            for (int i = 0; i < list.Count; i++)
            {
                //反转一下图片
                list[i] = new Invert().Apply(list[i]);

                int sw = list[i].Width;
                int sh = list[i].Height;

                Crop corpFilter = new Crop(new Rectangle(0, 0, w, h));

                list[i] = corpFilter.Apply(list[i]);

                //再反转回去
                list[i] = new Invert().Apply(list[i]);

                //计算中心位置
                int centerX = (w - sw) / 2;
                int centerY = (h - sh) / 2;

                list[i] = new CanvasMove(new IntPoint(centerX, centerY), Color.White).Apply(list[i]);

                resizeList.Add(list[i]);
            }

            return(resizeList);
        }
示例#2
0
        //Finds the offset of the center of the part and the center of the image
        public Bitmap centerPart(Bitmap inImage)
        {
            //Updates the center of the part
            findCenter(inImage);
            //Center of the frame
            int imageCenterX = inImage.Width / 2;
            int imageCenterY = inImage.Height / 2;
            //initialization of the X offset and Y offset for the center of the image and the part
            int xOffset = 0;
            int yOffset = 0;

            //Creating a temporary bitmap to apply graphics to(Grpahics doesn't accept canny images)
            Bitmap   modifiedBitmap = new Bitmap(inImage.Width, inImage.Height);
            Graphics g = Graphics.FromImage(modifiedBitmap);

            //Finds the distance from part's center and the center of the frame
            if (centerPoint.X < imageCenterX)
            {
                xOffset = (imageCenterX - (int)centerPoint.X);
            }
            else if (centerPoint.X > imageCenterX)
            {
                xOffset = -((int)centerPoint.X - imageCenterX);
            }

            if (centerPoint.Y < imageCenterY)
            {
                yOffset = (imageCenterY - (int)centerPoint.Y);
            }
            else if (centerPoint.Y > imageCenterY)
            {
                yOffset = -((int)centerPoint.Y - imageCenterY);
            }
            //Redraws the orignal image
            g.DrawImage(inImage, 0, 0);
            //Shifts the image according to the X and Y offsets
            CanvasMove shiftImage = new CanvasMove(new AForge.IntPoint(xOffset, yOffset), Color.White);

            modifiedBitmap = shiftImage.Apply(modifiedBitmap);

            return(modifiedBitmap);
        }
示例#3
0
        public static Bitmap ToResizeAndCenterIt(Bitmap img, int w = 40, int h = 50)
        {
            //反转一下图片
            img = new Invert().Apply(img);

            int sw = img.Width;
            int sh = img.Height;

            Crop corpFilter = new Crop(new Rectangle(0, 0, w, h));

            img = corpFilter.Apply(img);

            //再反转回去
            img = new Invert().Apply(img);

            //计算中心位置
            int centerX = (w - sw) / 2;
            int centerY = (h - sh) / 2;

            img = new CanvasMove(new IntPoint(centerX, centerY), Color.White).Apply(img);


            return(img);
        }
示例#4
0
        public void DeSkew()
        {
            Rectangle vBoundary = new Rectangle(new Point(0, 0), new Size(140, originalImage.Height));

            Emgu.CV.Cvb.CvBlobDetector bDetect    = new Emgu.CV.Cvb.CvBlobDetector();
            Emgu.CV.Cvb.CvBlobs        markerBlob = new Emgu.CV.Cvb.CvBlobs();

            List <Rectangle> blobs = new List <Rectangle>();

            Image <Gray, Byte> preprocessImage = originalImage.Convert <Gray, Byte>();

            preprocessImage = preprocessImage.ThresholdBinary(new Gray(200), new Gray(255));
            preprocessImage = preprocessImage.Not();

            markerBlob.Clear();

            bDetect.Detect(preprocessImage, markerBlob);
            preprocessImage.Dispose();
            preprocessImage = null;

            markerBlob.FilterByArea(250, 1800);

            foreach (Emgu.CV.Cvb.CvBlob targetBlob in markerBlob.Values)
            {
                if (vBoundary.Contains(targetBlob.BoundingBox))
                {
                    if (targetBlob.BoundingBox.Width >= targetBlob.BoundingBox.Height - 5)
                    {
                        Rectangle r = new Rectangle(targetBlob.BoundingBox.X, targetBlob.BoundingBox.Y, targetBlob.BoundingBox.Width, targetBlob.BoundingBox.Height);
                        blobs.Add(r);
                    }
                }
            }

            RectangleF temp  = blobs.First();
            RectangleF temp2 = blobs.Last();

            double dY = Math.Abs(temp.Y - temp2.Y);
            double dX = Math.Abs(temp.X - temp2.X);

            double angle = Math.Atan2(dX, dY);

            angle = angle * (180 / Math.PI);

            if (temp2.X > temp.X)
            {
                angle = angle * -1;
            }

            RotatedRect rot_rec = new RotatedRect();

            rot_rec.Center = new PointF(temp.X, temp.Y);
            RotationMatrix2D  rot_mat  = new RotationMatrix2D(rot_rec.Center, angle, 1);
            Image <Bgr, Byte> outimage = originalImage.CopyBlank();

            CvInvoke.WarpAffine(originalImage, outimage, rot_mat, originalImage.Size, Inter.Cubic, Warp.Default, BorderType.Constant, new Bgr(Color.White).MCvScalar);

            int xOffset = 80 - (int)temp.X;
            int yOffset = 45 - (int)temp.Y;

            originalImage = outimage.Copy();

            Bitmap     a      = originalImage.ToBitmap();
            CanvasMove filter = new CanvasMove(new AForge.IntPoint(xOffset, yOffset), Color.White);

            a             = filter.Apply(a);
            originalImage = new Image <Bgr, Byte>(a);

            a.Dispose();
            a = null;
            outimage.Dispose();
            outimage = null;
            blobs    = null;
        }