Example #1
0
 public static Bitmap Mirror(this Bitmap bitmap, bool MirrorX, bool MirrorY)
 {
     // create filter
     AForge.Imaging.Filters.Mirror filter = new AForge.Imaging.Filters.Mirror(MirrorX, MirrorY);
     // apply the filter
     return(filter.Apply(bitmap));
 }
        private Bitmap DisplayAndTrackDrawingTool(Bitmap image)
        {
            //grayscale the image
            AForge.Imaging.Filters.Grayscale grayscaling = new AForge.Imaging.Filters.Grayscale(
                0.2125, 0.7154, 0.0721);
            image = grayscaling.Apply(image);

            //mirror the image
            AForge.Imaging.Filters.Mirror mirrImg = new AForge.Imaging.Filters.Mirror(false, true);
            mirrImg.ApplyInPlace(image);

            //get biggest blob
            AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter(image);
            blobCounter.ProcessImage(image);

            Rectangle[] rectsInImage = blobCounter.GetObjectsRectangles();
            this.txtBoxInfo.Text = rectsInImage.Length + " blobs found.";

            AForge.Imaging.Filters.ExtractBiggestBlob biggestBlob = new AForge.Imaging.Filters.ExtractBiggestBlob();
            Bitmap   bigstBlobImage = biggestBlob.Apply(image);
            IntPoint posBiggBlob    = biggestBlob.BlobPosition;

            this.txtBoxInfo.Text = "Biggest Blob at: " + posBiggBlob.ToString();

            //SetCursor(posBiggBlob);

            GenerateGraphics(posBiggBlob);

            return(image);
        }
Example #3
0
        private void Button4_Click(object sender, EventArgs e)
        {
            Emgu.CV.Util.VectorOfPoint         approx = new Emgu.CV.Util.VectorOfPoint();
            Emgu.CV.Util.VectorOfVectorOfPoint vecOut = new Emgu.CV.Util.VectorOfVectorOfPoint();
            CvInvoke.FindContours(cannyImage, vecOut, null, Emgu.CV.CvEnum.RetrType.External, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple);
            //Point defines the x-y coordinates in 2d-plane

            Dictionary <int, double> dict = new Dictionary <int, double>();

            AForge.IntPoint[] point = new AForge.IntPoint[4];
            if (vecOut.Size > 0)
            {
                for (int i = 0; i < vecOut.Size; i++)
                {
                    //calculating area of contours
                    double area = CvInvoke.ContourArea(vecOut[i]);
                    dict.Add(i, area); //adding areas in dictionary
                }
                var item = dict.OrderByDescending(v => v.Value);
                //Preparing for perspective transformation
                foreach (var it in item)
                {
                    int key = Convert.ToInt32(it.Key.ToString());
                    //generating arc length wrapping the doc
                    double peri = CvInvoke.ArcLength(vecOut[key], true);
                    CvInvoke.ApproxPolyDP(vecOut[key], approx, 0.02 * peri, true);
                    if (approx.Size == 0)
                    {
                    }
                    else if (approx.Size == 4)
                    {
                        try
                        {
                            CvInvoke.DrawContours(copyImage, vecOut, key, new MCvScalar(255, 0, 0), 5);
                            for (int i = 0; i < approx.Size; i++)
                            {
                                point[i].X = approx[i].X;
                                point[i].Y = approx[i].Y;
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        break;
                    }
                }
            }
            sheetDetectImage.Image = copyImage;

            try
            {
                List <AForge.IntPoint> pointList = new List <AForge.IntPoint>();
                for (int i = 0; i < 4; i++)
                {
                    pointList.Add(new AForge.IntPoint(point[i].X, point[i].Y));
                }
                AForge.Imaging.Filters.SimpleQuadrilateralTransformation simple = new AForge.Imaging.Filters.SimpleQuadrilateralTransformation(pointList, 200, 200);
                Bitmap abc1 = simple.Apply(bitmapImage);
                AForge.Imaging.Filters.Mirror m = new AForge.Imaging.Filters.Mirror(false, true);
                m.ApplyInPlace(abc1);
                Image <Bgr, byte> newIm = new Image <Bgr, byte>(abc1);
                CvInvoke.Rotate(newIm, newIm, Emgu.CV.CvEnum.RotateFlags.Rotate90CounterClockwise);
                MessageBox.Show("Scanned Document");
                sheetDetectImage.Image = newIm;
                transformedImage       = newIm.Copy();
            }
            catch (Exception er)
            {
                MessageBox.Show(er.StackTrace);
            }
        }