Beispiel #1
0
        private void EdgeDetectImg(ref Bitmap frame)
        {
            frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);    // Make gray
            switch (EdgeDetectValue)
            {
            case 1:
                SobelEdgeDetector SobelFilter = new SobelEdgeDetector();
                SobelFilter.ApplyInPlace(frame);
                break;

            case 2:
                DifferenceEdgeDetector DifferenceFilter = new DifferenceEdgeDetector();
                DifferenceFilter.ApplyInPlace(frame);
                break;

            case 3:
                HomogenityEdgeDetector HomogenityFilter = new HomogenityEdgeDetector();
                HomogenityFilter.ApplyInPlace(frame);
                break;

            case 4:
                CannyEdgeDetector Cannyfilter = new CannyEdgeDetector();
                // apply the MirrFilter
                Cannyfilter.ApplyInPlace(frame);
                break;

            default:
                HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
                filter.ApplyInPlace(frame);
                break;
            }
            GrayscaleToRGB RGBfilter = new GrayscaleToRGB();    // back to color format

            frame = RGBfilter.Apply(frame);
        }
Beispiel #2
0
        void chosendevice_newframe(object sender, NewFrameEventArgs eventargs)
        {
            // throw new NotImplementedException();
            image = (Bitmap)eventargs.Frame.Clone();

            // create grayscale filter (BT709)
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            // apply the filter
            Bitmap grayImage = filter.Apply(image);

            //edge
            // create filter
            DifferenceEdgeDetector filters = new DifferenceEdgeDetector();

            // apply the filter
            filters.ApplyInPlace(grayImage);

            pictureBox1.Image = grayImage;
        }
        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));
        }
Beispiel #4
0
        // =========================================================
        private void Edge_detectFunc(ref Bitmap frame, int par_int)
        {
            frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);        // Make gray
            switch (par_int)
            {
            case 1:
                SobelEdgeDetector SobelFilter = new SobelEdgeDetector();
                SobelFilter.ApplyInPlace(frame);
                break;

            case 2:
                DifferenceEdgeDetector DifferenceFilter = new DifferenceEdgeDetector();
                DifferenceFilter.ApplyInPlace(frame);
                break;

            case 3:
                HomogenityEdgeDetector HomogenityFilter = new HomogenityEdgeDetector();
                HomogenityFilter.ApplyInPlace(frame);
                break;

            case 4:
                // can we not have references to canny in the code. gives me ptsd flashbacks
                CannyEdgeDetector Nightmare = new CannyEdgeDetector();
                // apply the filter
                Nightmare.ApplyInPlace(frame);
                break;

            default:
                HomogenityEdgeDetector filter = new HomogenityEdgeDetector();
                filter.ApplyInPlace(frame);
                break;
            }
            GrayscaleToRGB RGBfilter = new GrayscaleToRGB();    // back to color format

            frame = RGBfilter.Apply(frame);
        }
Beispiel #5
0
        public static Tuple <Bitmap, double, Rectangle> smate_rotate(Bitmap src)
        {
            Bitmap        retB     = null;
            Rectangle     retR     = Rectangle.Empty;
            double        retAngle = 90.0;
            double        angle    = 0.0;
            RotateBicubic filter   = new RotateBicubic(retAngle);
            Bitmap        src1     = filter.Apply(src);
            //Bitmap src1 = (Bitmap)src.Clone();
            Bitmap g  = Grayscale.CommonAlgorithms.BT709.Apply(src1);
            Invert it = new Invert();

            it.ApplyInPlace(g);
            //g.Save("temp_1.jpg");
            ImageStatistics stat = new ImageStatistics(g);
            Threshold       t    = new Threshold((int)(stat.Gray.Mean - stat.Gray.StdDev));

            t.ApplyInPlace(g);
            //g.Save("temp_2.jpg");
            stat = new ImageStatistics(g);
            DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();

            edgeDetector.ApplyInPlace(g);
            //g.Save("temp_3.jpg");
            HoughLineTransformation lineTransform = new HoughLineTransformation();

            lineTransform.ProcessImage(g);
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.8);
            foreach (HoughLine l in lines)
            {
                Program.logIt(string.Format("Intensity={0}, Radius={1}, Theta={2}", l.Intensity, l.Radius, l.Theta));
                if (l.Radius < 0)
                {
                    if (l.Theta < 90)
                    {
                        angle = -l.Theta;
                    }
                    else
                    {
                        angle = 180.0 - l.Theta;
                    }
                }
                else
                {
                    if (l.Theta < 90)
                    {
                        angle = -l.Theta;
                    }
                    else
                    {
                        angle = 180.0 - l.Theta;
                    }
                }
                if (Math.Abs(angle) < 45.0)
                {
                    break;
                }
            }
            Program.logIt(string.Format("angle={0}", angle));
            retAngle += angle;
            RotateBicubic r_filter = new RotateBicubic(angle);
            Bitmap        rotated  = r_filter.Apply(src1);

            // crop
            if (rotated != null)
            {
                Grayscale g_filter  = new Grayscale(0.2125, 0.7154, 0.0721);
                Bitmap    grayImage = g_filter.Apply(rotated);
                Blur      bf        = new Blur();
                bf.ApplyInPlace(grayImage);
                OtsuThreshold o_filter = new OtsuThreshold();
                o_filter.ApplyInPlace(grayImage);
                BlobCounter blobCounter = new BlobCounter();
                blobCounter.MinHeight    = 20;
                blobCounter.MinWidth     = 20;
                blobCounter.FilterBlobs  = false;
                blobCounter.BlobsFilter  = null;
                blobCounter.ObjectsOrder = ObjectsOrder.YX;
                blobCounter.ProcessImage(grayImage);
                Blob[] blobs = blobCounter.GetObjectsInformation();
                Program.logIt(string.Format("blobs={0}", blobCounter.ObjectsCount));
                Rectangle r = Rectangle.Empty;
                for (int i = 1; i < blobs.Length; i++)
                {
                    Blob b = blobs[i];
                    Program.logIt(string.Format("{0}: {1}", b.ID, b.Rectangle));
                    if (r == Rectangle.Empty)
                    {
                        r = b.Rectangle;
                    }
                    else
                    {
                        r = Rectangle.Union(r, b.Rectangle);
                    }
                }
                Program.logIt(string.Format("rect: {0}", r));
                retR = r;
                Crop c_filter = new Crop(r);
                retB = c_filter.Apply(rotated);
            }
            return(new Tuple <Bitmap, double, Rectangle>(retB, retAngle, retR));
        }
Beispiel #6
0
        private void FillPictureBoxes(ref Bitmap image)
        {
            Bitmap tmpImg  = image;
            Bitmap tmpImg2 = image;


            try
            {
                bool hasFilter = false;
                //setup resize and filtersequesce


                //resize img to fit picturebox
                ResizeBicubic resizeFilter = new ResizeBicubic(0, 0);

                resizeFilter = new ResizeBicubic(pbCapture.Width, pbCapture.Height);
                tmpImg       = resizeFilter.Apply(tmpImg);

                resizeFilter = new ResizeBicubic(pbShapes.Width, pbShapes.Height);
                tmpImg2      = resizeFilter.Apply(tmpImg2);



                FiltersSequence processingFilter = new FiltersSequence();


                //List all filters
                IFilter ConservativeSmoothingFilter = new AForge.Imaging.Filters.ConservativeSmoothing();
                IFilter InvertFilter          = new AForge.Imaging.Filters.Invert();
                IFilter HSLFilteringFilter    = new AForge.Imaging.Filters.HSLFiltering();
                IFilter SepiaFilter           = new AForge.Imaging.Filters.Sepia();
                IFilter grayscaleFilter       = new AForge.Imaging.Filters.GrayscaleBT709();
                IFilter SkeletonizationFilter = new AForge.Imaging.Filters.SimpleSkeletonization();
                IFilter pixFilter             = new AForge.Imaging.Filters.Pixellate();


                ////apply filter and process img---------------------------------------------



                if (ConservativeSmoothing)
                {
                    processingFilter.Add(ConservativeSmoothingFilter);
                    hasFilter = true;
                }

                if (Invert)
                {
                    processingFilter.Add(InvertFilter);
                    hasFilter = true;
                }

                if (HSLswitch)
                {
                    processingFilter.Add(HSLFilteringFilter);
                    hasFilter = true;
                }

                if (sepiaSwitch)
                {
                    processingFilter.Add(SepiaFilter);
                    hasFilter = true;
                }


                if (Skeletonization)
                {
                    processingFilter.Add(grayscaleFilter);
                    processingFilter.Add(SkeletonizationFilter);
                    hasFilter = true;
                }

                //apply the filter(s) to image
                if (hasFilter)
                {
                    //tmpImg = processingFilter.Apply(tmpImg);
                    tmpImg2 = processingFilter.Apply(tmpImg2);
                }

                processingFilter.Clear();


                if (bwSwitch)
                {
                    switchBandW(ref tmpImg);
                }



                if (CannyEdgeDetector)
                {
                    // create filter
                    CannyEdgeDetector filter = new CannyEdgeDetector();
                    // apply the filter
                    tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg);
                    filter.ApplyInPlace(tmpImg);


                    // image = DrawFocusArea(gsImage);
                }
                else
                {
                    // image = DrawFocusArea(image);
                }


                if (DifferenceEdgeDetector)
                {
                    DifferenceEdgeDetector dFilter = new DifferenceEdgeDetector();
                    // apply the filter
                    tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg);
                    dFilter.ApplyInPlace(tmpImg);
                }


                if (HomogenityEdgeDetector)
                {
                    HomogenityEdgeDetector hFilter = new HomogenityEdgeDetector();
                    // apply the filter
                    tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg);
                    hFilter.ApplyInPlace(tmpImg);
                }


                if (SobelEdgeDetector)
                {
                    SobelEdgeDetector hFilter = new SobelEdgeDetector();
                    // apply the filter
                    tmpImg = Grayscale.CommonAlgorithms.BT709.Apply(tmpImg);
                    hFilter.ApplyInPlace(tmpImg);

                    BlobCounter bc    = new BlobCounter(tmpImg);
                    Rectangle[] brecs = bc.GetObjectsRectangles();


                    //Graphics pg = Graphics.FromImage(tmpImg);
                    //Pen p = new Pen(Color.White, 2);

                    //foreach (Rectangle r in brecs)
                    //{
                    //    pg.DrawRectangle(p, r);
                    //}
                }



                if (findShapes)
                {
                    tmpImg = FindShapes(tmpImg, ref tmpImg2);
                    //ProcessImage(image);
                }
                else
                {
                    pbCapture.Image = tmpImg;  //set picturebox image----------------
                    pbShapes.Image  = tmpImg2; //set picturebox image----------------
                }



                // Graphics g = Graphics.FromImage(tmpImg);
                // Pen p = new Pen(Color.Red, 2);

                // Rectangle lr = new Rectangle(100, 120, 80, 40);
                //// Rectangle rr = new Rectangle(360, 220, 80, 40);

                // g.DrawRectangle(p, lr);
                // //g.DrawRectangle(p, rr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


            //  pbCapture.Image = tmpImg;//set picturebox image----------------
            //   pbShapes.Image = tmpImg2;//set picturebox image----------------
        }
Beispiel #7
0
        public static void DifferenceEdgeFilter(UnmanagedImage img)
        {
            DifferenceEdgeDetector filter2 = new DifferenceEdgeDetector();

            filter2.ApplyInPlace(img);
        }