Beispiel #1
2
        private Bitmap Flatten(Bitmap bmp, int fillint, int contint)
        {
            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red = new IntRange(0, fillint);
            colorFilter.Green = new IntRange(0, fillint);
            colorFilter.Blue = new IntRange(0, fillint);
            colorFilter.FillOutsideRange = false;
            using (Bitmap filteredBmp = colorFilter.Apply(bmp))
            {
                AForge.Imaging.Filters.ContrastCorrection Contrast = new ContrastCorrection(contint);
                AForge.Imaging.Filters.Invert invert = new Invert();
                AForge.Imaging.Filters.ExtractChannel extract_channel = new ExtractChannel(RGB.B);
                AForge.Imaging.Filters.Threshold thresh_hold = new Threshold(44);

                Contrast.ApplyInPlace(filteredBmp);

                Bitmap extractedBmp = extract_channel.Apply(filteredBmp);
                thresh_hold.ApplyInPlace(extractedBmp);
                invert.ApplyInPlace(extractedBmp);

                return extractedBmp;
            }
        }
Beispiel #2
1
        private void cBFilters_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bitmap pictureTransform = (Bitmap)pbChoose.Image;
                if ((string)cBFilters.SelectedItem == "HSL_Filter")
                {
                    AForge.Imaging.Filters.SaturationCorrection filter = new SaturationCorrection(0.1);
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Mediana")
                {
                    AForge.Imaging.Filters.Median filter = new Median();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                /*if ((string)cBFilters.SelectedItem == "Fourier_Transformation")
                {
                 Complex[] dst = new Complex[n];

                    AForge.Math.FourierTransform filter = AForge.Math.FourierTransform.DFT();// .FromBitmap(pictureTransform);
                    Bitmap newImage = filter.Apply();
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }*/

                if ((string)cBFilters.SelectedItem == "Binarization")
                {
                    pictureTransform = Grayscale.CommonAlgorithms.BT709.Apply(pictureTransform);
                    Threshold threshold = new Threshold(127);
                    threshold.ApplyInPlace(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = pictureTransform;
                }

                if ((string)cBFilters.SelectedItem == "Grayscale")
                {
                    AForge.Imaging.Filters.Grayscale filter = new AForge.Imaging.Filters.Grayscale(0.2125, 0.7154, 0.0721);
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "FillHoles")
                {

                    pictureTransform = Grayscale.CommonAlgorithms.BT709.Apply(pictureTransform);
                    Threshold threshold = new Threshold(127);
                    threshold.ApplyInPlace(pictureTransform);
                    AForge.Imaging.Filters.FillHoles filter = new AForge.Imaging.Filters.FillHoles();
                    filter.MaxHoleHeight = 5;
                    filter.MaxHoleWidth = 15;
                    filter.CoupledSizeFiltering = false;
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Opening")
                {
                    AForge.Imaging.Filters.Opening filter = new AForge.Imaging.Filters.Opening();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Closing")
                {
                    AForge.Imaging.Filters.Closing filter = new AForge.Imaging.Filters.Closing();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Erosion")
                {
                    AForge.Imaging.Filters.Erosion filter = new AForge.Imaging.Filters.Erosion();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Dilatation")
                {
                    AForge.Imaging.Filters.Dilatation filter = new AForge.Imaging.Filters.Dilatation();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }

                if ((string)cBFilters.SelectedItem == "Edges")
                {
                    AForge.Imaging.Filters.Edges filter = new AForge.Imaging.Filters.Edges();
                    Bitmap newImage = filter.Apply(pictureTransform);
                    pbChoose.SizeMode = PictureBoxSizeMode.CenterImage;
                    pBNew.Image = newImage;
                }
        }
Beispiel #3
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        /// <param name="rect">Image rectangle for processing by the filter.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            // calculate threshold
            thresholdFilter.ThresholdValue = CalculateThreshold(image, rect);

            // thresholding
            thresholdFilter.ApplyInPlace(image, rect);
        }
Beispiel #4
0
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     pictureBox1.Image = new Bitmap(Bitmap.FromFile("C:\\testimg\\" +comboBox1.SelectedIndex + ".png"), pictureBox1.Size);
     Bitmap channel = selected.Apply((Bitmap)pictureBox1.Image);
     thresholdFilter = new Threshold(Int32.Parse(textBox1.Text));
     thresholdFilter.ApplyInPlace(channel);
     pictureBox2.Image = channel;
     pictureBox3.Image = brownFilter.Apply((Bitmap)pictureBox1.Image);
 }
Beispiel #5
0
        public static FoundBlobs FindAny(FoundColorSpaces colorSpaces)
        {
            FoundBlobs foundBlobs = new FoundBlobs();

            SobelEdgeDetector edge = new SobelEdgeDetector();
            Bitmap edges = edge.Apply(colorSpaces.GrayColorSpace);

            Threshold threshold = new Threshold(50);
            threshold.ApplyInPlace(edges);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.ProcessImage(edges);
            foundBlobs.Blobs = blobCounter.GetObjects(colorSpaces.GrayColorSpace, false).ToArray();

            foundBlobs.BlobCounter = blobCounter;

            return foundBlobs;
        }
Beispiel #6
0
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    selected = redFilter;
                    break;
                case 1:
                    selected = greenFilter;
                    break;
                case 2:
                    selected = blueFilter;
                    break;
                default:
                    break;

            }
            Bitmap channel = selected.Apply((Bitmap)pictureBox1.Image);
            thresholdFilter = new Threshold(Int32.Parse(textBox1.Text));
            thresholdFilter.ApplyInPlace(channel);
            pictureBox2.Image = channel;
        }
 private Bitmap binarizationImage(Bitmap image)
 {
     Grayscale filterGrayscale = Grayscale.CommonAlgorithms.BT709;
     //Grayscale filterGrayscale = new Grayscale(0.5, 0.419, 0.081); // R-Y
     Bitmap grayImage = filterGrayscale.Apply(image);
     Threshold filter = new Threshold(200);
     filter.ApplyInPlace(grayImage);
     return doBlackAndWhiteImage(grayImage);
 }
        /// <summary>
        /// Get rectangle contain glyphs in current frame
        /// </summary>
        /// <param name="image">Frame source</param>
        /// <param name="lists">Rectangles contain glyphs</param>
        public static void GlyphsPotentialsTracking(ref UnmanagedImage image, ref List<Rectangle> lists)
        {
            // 1 - grayscaling
            UnmanagedImage grayImage = null;

            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                grayImage = image;
            }
            else
            {
                grayImage = UnmanagedImage.Create(image.Width, image.Height,
                    PixelFormat.Format8bppIndexed);
                Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage);
            }

            // 2 - Edge detection
            DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
            UnmanagedImage edgesImage = edgeDetector.Apply(grayImage);

            // 3 - Threshold edges
            Threshold thresholdFilter = new Threshold(80);
            thresholdFilter.ApplyInPlace(edgesImage);

            //// ---------------- MILESTONE
            //image = edgesImage;
            //return;

            // create and configure blob counter
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.MinHeight = 32;
            blobCounter.MinWidth = 32;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

            // 4 - find all stand alone blobs
            blobCounter.ProcessImage(edgesImage);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            List<IntPoint> edgePoints = null;
            List<IntPoint> corners = null;
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            // 5 - check each blob
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                corners = null;

                // does it look like a quadrilateral ?
                if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    // get edge points on the left and on the right side
                    List<IntPoint> leftEdgePoints, rightEdgePoints;

                    blobCounter.GetBlobsLeftAndRightEdges(blobs[i],
                        out leftEdgePoints, out rightEdgePoints);

                    // calculate average difference between pixel values from outside of the
                    // shape and from inside
                    float diff = CalculateAverageEdgesBrightnessDifference(
                        leftEdgePoints, rightEdgePoints, grayImage);

                    // check average difference, which tells how much outside is lighter than
                    // inside on the average
                    if (diff > 20)
                    {
                        lists.Add(blobs[i].Rectangle);
                    }
                }
            }
        }
        public void DetectQuads()
        {
            // Greyscale
            FilteredBitmap = Grayscale.CommonAlgorithms.BT709.Apply(CameraBitmap);

            // edge filter
            var edgeFilter = new SobelEdgeDetector();
            edgeFilter.ApplyInPlace(FilteredBitmap);

            // Threshhold filter
            var threshholdFilter = new Threshold(190);
            threshholdFilter.ApplyInPlace(FilteredBitmap);

            var bitmapData = FilteredBitmap.LockBits(
                new Rectangle(0, 0, FilteredBitmap.Width, FilteredBitmap.Height),
                ImageLockMode.ReadWrite, FilteredBitmap.PixelFormat);

            var blobCounter = new BlobCounter
            {
                FilterBlobs = true,
                MinHeight = 125,
                MinWidth = 125
            };

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();
            FilteredBitmap.UnlockBits(bitmapData);

            var shapeChecker = new SimpleShapeChecker();

            var bm = new Bitmap(FilteredBitmap.Width, FilteredBitmap.Height, PixelFormat.Format24bppRgb);

            var g = Graphics.FromImage(bm);
            g.DrawImage(FilteredBitmap, 0, 0);

            var pen = new Pen((Color)new ColorConverter().ConvertFromString(Application.Current.Resources["AccentColor"].ToString()), 5);
            var cardPositions = new List<IntPoint>();

            // Loop through detected shapes
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                var edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List<IntPoint> corners;
                var sameCard = false;

                // is triangle or quadrilateral
                if (shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    if (!corners.Any())
                        return;
                    // get sub-type
                    var subType = shapeChecker.CheckPolygonSubType(corners);

                    // Only return 4 corner rectanges
                    if ((subType != PolygonSubType.Parallelogram && subType != PolygonSubType.Rectangle) || corners.Count != 4)
                        continue;

                    // Check if its sideways, if so rearrange the corners so it's veritcal
                    RearrangeCorners(corners);

                    // Prevent it from detecting the same card twice
                    foreach (var point in cardPositions)
                    {
                        var distance = corners[0].DistanceTo(point);
                        if (corners[0].DistanceTo(point) < 40)
                            sameCard = true;
                    }

                    if (sameCard)
                        continue;

                    // Hack to prevent it from detecting smaller sections of the card instead of the whole card
                    var area = GetArea(corners);

                    if (area < 20000)// || area > 35000)
                        continue;

                    cardPositions.Add(corners[0]);

                    g.DrawPolygon(pen, ToPointsArray(corners));

                    // Extract the card bitmap
                    var transformFilter = new QuadrilateralTransformation(corners, 225, 325);
                    CardBitmap = transformFilter.Apply(CameraBitmap);
                    TmpCard = new MagicCard
                    {
                        Corners = corners,
                        CardBitmap = CardBitmap
                    };
                }
            }

            pen.Dispose();
            g.Dispose();

            FilteredBitmap = bm;
        }
Beispiel #10
0
        public void TestThresholdedBitmapToDoubleArray3()
        {
            //Test multi-dimensional bitmap
            Bitmap b = new Bitmap(2, 2);
            b.SetPixel(0, 0, Color.Black);
            b.SetPixel(1, 0, Color.White);
            b.SetPixel(0, 1, Color.Black);
            b.SetPixel(1, 1, Color.White);

            Bitmap greyImg = Grayscale.CommonAlgorithms.BT709.Apply(b);
            Threshold threshold = new Threshold(128);
            threshold.ApplyInPlace(greyImg);
            b.Dispose();

            double[] expected = new double[] { 0.5, -0.5, 0.5, -0.5 };

            CollectionAssert.AreEqual(expected, Converters.ThresholdedBitmapToDoubleArray(greyImg));

            greyImg.Dispose();
        }
Beispiel #11
0
        /// <summary>
        /// Filter: Threshold (Binary)
        /// Binarize the image (threshold)
        /// </summary>
        public void ToBinary()
        {
            var filter = new Threshold(230); // create filter
            filter.ApplyInPlace(Image); // apply the filter
            Save("binarized");

            /*
             * Note: Since the filter can be applied as to 8 bpp and to 16 bpp images, the Threshold Value value
             * should be set appropriately to the pixel format. In the case of 8 bpp images the threshold value
             * is in the [0, 255] range, but in the case of 16 bpp images the threshold value is in the [0, 65535] range.
             */
        }
        public void Video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            UnmanagedImage image = UnmanagedImage.FromManagedImage((Bitmap) eventArgs.Frame.Clone());

            var extractChannel = new ExtractChannel(RGB.R);
            UnmanagedImage channel = extractChannel.Apply(image);
            //		UnmanagedImage originalRed = channel.Clone();
            if (true) {
                var threshold = new Threshold(200);
                threshold.ApplyInPlace(channel);

                ////filter to convert RGB image to 8bpp gray scale for image processing
                //IFilter gray_filter = new GrayscaleBT709();
                //gray_image = gray_filter.Apply(gray_image);

                ////thresholding a image
                //Threshold th_filter = new Threshold(color_data.threshold);
                //th_filter.ApplyInPlace(gray_image);

                //erosion filter to filter out small unwanted pixels
                Erosion3x3 erosion = new Erosion3x3();
                erosion.ApplyInPlace(channel);

                //dilation filter
                //Dilatation3x3 dilatation = new Dilatation3x3();
                //dilatation.ApplyInPlace(channel);

                //GrayscaleToRGB filter = new GrayscaleToRGB();
                //image = filter.Apply(channel);

                //ReplaceChannel replaceFilter = new ReplaceChannel(RGB.B, channel);
                //replaceFilter.ApplyInPlace(image);
            }

            BlobCounter bc = new BlobCounter();
            //arrange blobs by area
            bc.ObjectsOrder = ObjectsOrder.Area;
            bc.FilterBlobs = true;
            bc.MinHeight = minObjectSize;
            bc.MinWidth = minObjectSize;
            bc.MaxHeight = maxObjectSize;
            bc.MaxWidth = maxObjectSize;

            //process image for blobs
            bc.ProcessImage(channel);
            channel.Dispose();

            //	if (motionDetector.ProcessFrame(image) > 0.02) {
            //	for (int i = 0; i < blobCountingObjectsProcessing.ObjectRectangles.Length; i++) {
            Rectangle[] rectangles = bc.GetObjectsRectangles();
            Blob[] blobs = bc.GetObjectsInformation();
            for (int i = 0; i < bc.ObjectsCount; i++) {
                Rectangle rectangle = rectangles[i];
                int width = rectangle.Width;
                int height = rectangle.Height;

            //		if (width < maxObjectSize && height < maxObjectSize && width > minObjectSize && height > minObjectSize) {
                    Drawing.Rectangle(image, rectangle, colorList[i % colorList.Length]);

                    if (i == 0) {
                        Position = GetCenterOfMass(image, rectangle);
                        Drawing.FillRectangle(image, rectangle, Color.BlanchedAlmond);
                        Drawing.FillRectangle(image, new Rectangle((int) Position.U - dotSize, (int) Position.V - dotSize, dotSize * 3, dotSize * 3), Color.Indigo);
                    }
            //		}
            }
            //	}

            Image = image.ToManagedImage();
            //	videoForm.ImageDestination.Image = image.ToManagedImage();
        }
Beispiel #13
0
        /*
         * Private helpers
         */
        private Bitmap get8bppConvertedSinglePixelBitmap(Color colour)
        {
            Bitmap b = new Bitmap(1, 1);
            b.SetPixel(0, 0, colour);

            Bitmap greyImg = Grayscale.CommonAlgorithms.BT709.Apply(b);
            Threshold threshold = new Threshold(128);
            threshold.ApplyInPlace(greyImg);

            b.Dispose();

            return greyImg;
        }
Beispiel #14
0
        /// <summary>
        /// [Português]
        /// Método que realiza a subtração entre duas imagens.
        /// <para></para>
        /// [English]
        /// Method that performs the subtraction between two images.
        /// </summary>
        /// <param name="patientImg"></param>
        /// <param name="backgroundImg"></param>
        /// <returns></returns>
        public Bitmap ProcessImage(Bitmap patientImg, Bitmap backgroundImg)
        {
            //#if RELEASE
            backgroundImg.SetResolution(96, 96);
            patientImg.SetResolution(96, 96);

            var subtract = new AForge.Imaging.Filters.Subtract(backgroundImg);
            subtract.ApplyInPlace(patientImg);

            var contrast = new AForge.Imaging.Filters.ContrastCorrection(60);
            contrast.ApplyInPlace(patientImg);

            Grayscale escalaCinza = Grayscale.CommonAlgorithms.BT709;
            patientImg = escalaCinza.Apply(patientImg);

            Threshold threshold = new Threshold(10);
            threshold.ApplyInPlace(patientImg);
            return patientImg;
            //#endif
        }
        static void Main(string[] args)
        {
            Threshold thresh = new Threshold(10);
            Median median = new Median(9);
            Erosion3x3 erode = new Erosion3x3();
            Dilatation3x3 dilate = new Dilatation3x3();
            GrahamConvexHull hullFinder = new GrahamConvexHull();
            ConnectedComponentsLabeling ccLabeler = new ConnectedComponentsLabeling();
            BorderFollowing contourFinder = new BorderFollowing();
            GrayscaleToRGB rgb = new GrayscaleToRGB();
            ConvexHullDefects defectFinder = new ConvexHullDefects(10);

            Bitmap img = (Bitmap)Bitmap.FromFile("hand3.jpg");

            Bitmap image = Grayscale.CommonAlgorithms.BT709.Apply(img);
            thresh.ApplyInPlace(image);
            //median.ApplyInPlace(image);
            erode.ApplyInPlace(image);
            dilate.ApplyInPlace(image);

            BlobCounter counter = new BlobCounter(image);
            counter.ObjectsOrder = ObjectsOrder.Area;

            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0)
            {
                counter.ExtractBlobsImage(image, blobs[0], true);

                UnmanagedImage hand = blobs[0].Image;

                var contour = contourFinder.FindContour(hand);

                if (contour.Count() > 0)
                {
                    var initialHull = hullFinder.FindHull(contour);

                    var defects = defectFinder.FindDefects(contour, initialHull);

                    var filteredHull = initialHull.ClusterHullPoints().FilterLinearHullPoints();

                    var palmCenter = defects.Centroid(contour);

                    var wristPoints = filteredHull.SelectWristPoints(defects, contour);

                    Bitmap color = rgb.Apply(hand).ToManagedImage();

                    //BitmapData data = color.LockBits(new Rectangle(0, 0, color.Width, color.Height), ImageLockMode.ReadWrite, color.PixelFormat);
                    //Drawing.Polyline(data, contour, Color.Blue);
                    //Drawing.Polygon(data, filteredHull, Color.Red);
                    //color.UnlockBits(data);

                    Graphics gr = Graphics.FromImage(color);

                    gr.DrawPolygon(new Pen(Brushes.Red, 3), filteredHull.ToPtArray());
                    gr.DrawLines(new Pen(Brushes.Blue, 3), contour.ToPtArray());
                    gr.DrawEllipse(new Pen(Brushes.Red, 3), palmCenter.X - 10, palmCenter.Y - 10, 20, 20);

                    foreach (ConvexityDefect defect in defects)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Green, 6), contour[defect.Point].X - 10, contour[defect.Point].Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in filteredHull)
                    {
                        gr.DrawEllipse(new Pen(Brushes.Yellow, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    foreach (AForge.IntPoint pt in wristPoints)
                    {
                        gr.DrawEllipse(new Pen(Brushes.PowderBlue, 6), pt.X - 10, pt.Y - 10, 20, 20);
                    }

                    ImageBox.Show(color);
                }
            }
        }
        private Bitmap thresholdImage(Bitmap image, int thresholdVal)
        {
            Threshold thrshold = new Threshold();
            thrshold.ThresholdValue = thresholdVal;
            thrshold.ApplyInPlace(image);

            return image;
        }
Beispiel #17
0
        public void RunWorker()
        {
            while (true)
            {
                bool ajSuccess = false;
                resetEvent.WaitOne();

                frameMutex.WaitOne(); //RNM

                // Process image
                this.workerStatus = WorkerStatus.Busy;

                if (processingImage.Width != grayBuffer.Width || processingImage.Height != grayBuffer.Height)
                    grayBuffer = new Bitmap(processingImage.Width, processingImage.Height, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);

                System.Drawing.Imaging.BitmapData imageBD = LockTotalBitmap(processingImage, System.Drawing.Imaging.ImageLockMode.ReadWrite);
                System.Drawing.Imaging.BitmapData grayBufferBD = LockTotalBitmap(grayBuffer, System.Drawing.Imaging.ImageLockMode.ReadWrite);

                try
                {
                    // create unmanaged images
                    UnmanagedImage imageUMI = new UnmanagedImage(imageBD);
                    UnmanagedImage grayBufferUMI = new UnmanagedImage(grayBufferBD);

                    // Apply grayscale and otsu filters
                    if (this.probabilityMap == null)
                    {
                        // Grayscale Otsu Thresholding
                        grayFilter.Apply(imageUMI, grayBufferUMI);

                        otsuFilter.ApplyInPlace(grayBufferUMI);
                        //Median mf = new Median(5);
                        //mf.ApplyInPlace(grayBufferUMI);

                    }
                    else
                    {
                        // BPM Colour Thresholding
                        probabilityMap.TresholdImage(imageUMI, grayBufferUMI);

                        //Median mf = new Median(5);
                        //mf.ApplyInPlace(grayBufferUMI);

                        GaussianBlur gb = new GaussianBlur(3.0);
                        Threshold t = new Threshold(127);
                        gb.ApplyInPlace(grayBufferUMI);
                        t.ApplyInPlace(grayBufferUMI);

                        //AForge.Imaging.Filters.Erosion er = new AForge.Imaging.Filters.Erosion();
                        //er.ApplyInPlace(grayBufferUMI);
                    }

                    bool ccSuccess = componentFinder.FindBlobs(grayBufferUMI);

                    // Create adjacency matrix
                    if (ccSuccess)
                    {
                        ajSuccess = regionAdjacencyGraph.ConstructGraph(componentFinder.ObjectLabels, componentFinder.ObjectCount, processingImage.Width, processingImage.Height);
                    }

                    // If producing the graph has failed, for example due to too many regions, stop processing the current frame.
                    if (!ajSuccess)
                    {
                        if (detectedMarkerList != null)
                            detectedMarkerList.Clear();
                        if (permittedMarkerList != null)
                            permittedMarkerList.Clear();
                        this.workerStatus = WorkerStatus.Finished;
                        continue;
                    }

                    detectedMarkerList = markerDetector.FindMarkers(componentFinder, regionAdjacencyGraph);
                    permittedMarkerList = markerDetector.PermitMarkers(detectedMarkerList, permittedCodes);
                }
                catch (Exception e)
                {
                    //RNM
                    resetEvent.Reset();
                    frameMutex.ReleaseMutex();
                    MessageBox.Show("An exception occurred during Marker Detection: " + e.Message);

                    throw;
                }
                finally
                {
                    processingImage.UnlockBits(imageBD);
                    grayBuffer.UnlockBits(grayBufferBD);

                    if (!ajSuccess)
                    {
                        resetEvent.Reset();
                        frameMutex.ReleaseMutex();
                    }

                }

                if (detectedMarkerList != null && permittedMarkerList.Count > 0)
                {

                    //send marker xml.
                    sendMarkerXml(permittedMarkerList);

                    // Draw other UI components
                    using (Graphics g = Graphics.FromImage(processingImage))
                    {
                        Pen boundingBoxPen = new Pen(Color.Blue, 4.0f);
                        boundingBoxPen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
                        boundingBoxPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
                        Font markerFont = new Font("Arial", 16.0f);

                        for (int marker = 0; marker < permittedMarkerList.Count; marker++)
                        {
                            int markerID = permittedMarkerList[marker].ID;
                            string markerCode = permittedMarkerList[marker].Code;
                            BoundingBox bb = componentFinder.ObjectBoundingBoxes[markerID];

                            g.DrawLine(boundingBoxPen, new System.Drawing.Point(bb.x1, bb.y1), new System.Drawing.Point(bb.x1, bb.y2));
                            g.DrawLine(boundingBoxPen, new System.Drawing.Point(bb.x1, bb.y2), new System.Drawing.Point(bb.x2, bb.y2));
                            g.DrawLine(boundingBoxPen, new System.Drawing.Point(bb.x2, bb.y2), new System.Drawing.Point(bb.x2, bb.y1));
                            g.DrawLine(boundingBoxPen, new System.Drawing.Point(bb.x2, bb.y1), new System.Drawing.Point(bb.x1, bb.y1));
                        }

                        for (int marker = 0; marker < permittedMarkerList.Count; marker++)
                        {
                            int markerID = permittedMarkerList[marker].ID;
                            string markerCode = permittedMarkerList[marker].Code;
                            BoundingBox bb = componentFinder.ObjectBoundingBoxes[markerID];
                            g.DrawString(markerCode, markerFont, Brushes.Red, new PointF(bb.x1, bb.y2));
                        }
                    }
                }

                this.workerStatus = WorkerStatus.Finished;
                workerTicks++;
                resetEvent.Reset();
                frameMutex.ReleaseMutex();
            }
        }
        public void Detect(ref Bitmap image)
        {
            List<List<IntPoint>> markers = new List<List<IntPoint>>();
            Bitmap tmp = image;

            BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
            UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

            UnmanagedImage grayImage = UnmanagedImage.Create(unmanagedImage.Width, unmanagedImage.Height, PixelFormat.Format8bppIndexed);
            Grayscale.CommonAlgorithms.BT709.Apply(unmanagedImage, grayImage);

            DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
            UnmanagedImage edgesImage = edgeDetector.Apply(grayImage);

            image.UnlockBits(bitmapData);

            if (this.edgeImage.Checked)
                tmp = edgesImage.ToManagedImage().Clone(new Rectangle(0,0,edgesImage.Width, edgesImage.Height), PixelFormat.Format24bppRgb);

            Threshold thresholdFilter = new Threshold(this.binThreshold);
            thresholdFilter.ApplyInPlace(edgesImage);

            if (this.thresholdEdgeImage.Checked)
                tmp = edgesImage.ToManagedImage().Clone(new Rectangle(0, 0, edgesImage.Width, edgesImage.Height), PixelFormat.Format24bppRgb);

            this.blobCounter.ProcessImage(edgesImage);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List<IntPoint> corners = null;

                if (this.isSquare(edgePoints, out corners))
                {
                    List<IntPoint> leftEdgePoints, rightEdgePoints;
                    blobCounter.GetBlobsLeftAndRightEdges(blobs[i],
                        out leftEdgePoints, out rightEdgePoints);

                    float diff = calculateAverageEdgesBrightnessDifference(
                        leftEdgePoints, rightEdgePoints, grayImage);

                    if (diff > 50)
                    {
                        markers.Add(corners);
                    }
                }
            }

            foreach (List<IntPoint> marker in markers)
            {
                Color markerColor;
                IntPoint markerOrientation = this.markerOrientation(image, marker, out markerColor);
                IntPoint center = marker[2] - marker[0];
                center.X = marker[0].X + Convert.ToInt32(center.X * 0.5);
                center.Y = marker[0].Y + Convert.ToInt32(center.Y * 0.5);

                if (this.drawMarkersOnVideo.Checked)
                {
                    if ((this.edgeImage.Checked) || (this.thresholdEdgeImage.Checked))
                        this.drawMarker(tmp, marker, markerOrientation, markerColor);
                    else
                        this.drawMarker(image, marker, markerOrientation, markerColor);
                }
                ColorDiscriminator discriminator = new ColorDiscriminator();
                discriminator.Color = markerColor;

                LocationSourceManager.Instance.updateLocationSource(discriminator, center);
            }
            image = tmp;
        }
Beispiel #19
0
        /// <summary>
        /// Returns an image that is thresholded and converted to 1 bit per pixel format.
        /// </summary>
        /// <param name="image">The original image. Not necessarily thresholded but needs to be resized beforehand.</param>
        /// <returns></returns>
        public static Bitmap ThresholdAndConvertTo1bpp(Image image)
        {
            Bitmap oriBitmap = new Bitmap(image);

            //Copies the original bitmap into a grey scale bitmap
            Grayscale grayScaleFilter = new Grayscale(0.2125, 0.7154, 0.0721);
            Bitmap grayScaleBitmap = grayScaleFilter.Apply(oriBitmap);
            oriBitmap.Dispose();

            //This part does thresholding, with Aforge.NET library.
            Threshold filter = new Threshold(ThresholdValue);
            filter.ApplyInPlace(grayScaleBitmap);

            //Copies the original bitmap to a 1bpp bitmap.
            Bitmap resultBitmap = CopyTo1Bpp(grayScaleBitmap);
            grayScaleBitmap.Dispose();

            return resultBitmap;
        }
Beispiel #20
0
        // Process specified image trying to recognize counter's image
        public void Process( Bitmap image, IImageProcessingLog log )
        {
            log.AddMessage( "Image size: " + image.Width + " x " + image.Height );

            // 1 - Grayscale
            Bitmap grayImage = Grayscale.CommonAlgorithms.BT709.Apply( image );
            log.AddImage( "Grayscale", grayImage );

            // 2 - Edge detection
            DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector( ); 
            Bitmap edges = edgeDetector.Apply( grayImage );
            log.AddImage( "Edges", edges );

            // 3 - Threshold edges
            Threshold thresholdFilter = new Threshold( 40 ); 
            thresholdFilter.ApplyInPlace( edges );
            log.AddImage( "Thresholded Edges", edges );

            // 4 - Blob Counter
            BlobCounter blobCounter = new BlobCounter( );
            blobCounter.MinHeight = 32;
            blobCounter.MinWidth  = 32;
            blobCounter.FilterBlobs  = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

            blobCounter.ProcessImage( edges );
            Blob[] blobs = blobCounter.GetObjectsInformation( );

            // create copy of source image, so we could draw on it
            Bitmap imageCopy = AForge.Imaging.Image.Clone( image );

            BitmapData imageData = imageCopy.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                ImageLockMode.ReadWrite, imageCopy.PixelFormat );

            // lock grayscale image, so we could access it's pixel values
            BitmapData grayData = grayImage.LockBits( new Rectangle( 0, 0, image.Width, image.Height ),
                ImageLockMode.ReadOnly, grayImage.PixelFormat );
            UnmanagedImage grayUI = new UnmanagedImage( grayData );

            // list of found dark/black quadrilaterals surrounded by white area
            List<List<IntPoint>> foundObjects = new List<List<IntPoint>>( );
            // shape checker for checking quadrilaterals
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker( );

            // 5 - check each blob
            for ( int i = 0, n = blobs.Length; i < n; i++ )
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints( blobs[i] );
                List<IntPoint> corners = null;

                // does it look like a quadrilateral ?
                if ( shapeChecker.IsQuadrilateral( edgePoints, out corners ) )
                {
                    // do some more checks to filter so unacceptable shapes
                    // if ( CheckIfShapeIsAcceptable( corners ) )
                    {
                        log.AddMessage( "Blob size: " + blobs[i].Rectangle.Width + " x " + blobs[i].Rectangle.Height );

                        // get edge points on the left and on the right side
                        List<IntPoint> leftEdgePoints, rightEdgePoints;
                        blobCounter.GetBlobsLeftAndRightEdges( blobs[i], out leftEdgePoints, out rightEdgePoints );

                        // calculate average difference between pixel values from outside of the shape and from inside
                        float diff = CalculateAverageEdgesBrightnessDifference(
                            leftEdgePoints, rightEdgePoints, grayUI );

                        log.AddMessage( "Avg Diff: " + diff );

                        // check average difference, which tells how much outside is lighter than inside on the average
                        if ( diff > 20 )
                        {
                            Drawing.Polygon( imageData, corners, Color.Red );
                            // add the object to the list of interesting objects for further processing
                            foundObjects.Add( corners );
                        }
                    }
                }
            }

            imageCopy.UnlockBits( imageData );
            grayImage.UnlockBits( grayData );

            log.AddImage( "Potential glyps", imageCopy );

            int counter = 1;

            // further processing of each potential glyph
            foreach ( List<IntPoint> corners in foundObjects )
            {
                log.AddMessage( "Glyph #" + counter );
                
                log.AddMessage( string.Format( "Corners: ({0}), ({1}), ({2}), ({3})",
                    corners[0], corners[1], corners[2], corners[3] ) );

                // 6 - do quadrilateral transformation
                QuadrilateralTransformation quadrilateralTransformation =
                    new QuadrilateralTransformation( corners, 250, 250 );

                Bitmap transformed = quadrilateralTransformation.Apply( grayImage );

                log.AddImage( "Transformed #" + counter, transformed );

                // 7 - otsu thresholding
                OtsuThreshold otsuThresholdFilter = new OtsuThreshold( ); 
                Bitmap transformedOtsu = otsuThresholdFilter.Apply( transformed );
                log.AddImage( "Transformed Otsu #" + counter, transformedOtsu );

                int glyphSize = 5;
                SquareBinaryGlyphRecognizer gr = new SquareBinaryGlyphRecognizer( glyphSize );

                bool[,] glyphValues = gr.Recognize( transformedOtsu,
                    new Rectangle( 0, 0, 250, 250 ) );

                log.AddImage( "Glyph lines #" + counter, transformedOtsu );

                // output recognize glyph to log
                log.AddMessage( string.Format( "glyph: {0:F2}%", gr.confidence * 100 ) );
                for ( int i = 0; i < glyphSize; i++ )
                {
                    StringBuilder sb = new StringBuilder( "   " );

                    for ( int j = 0; j < glyphSize; j++ )
                    {
                        sb.Append( ( glyphValues[i, j] ) ? "1 " : "0 " );
                    }

                    log.AddMessage( sb.ToString( ) );
                }

                counter++;
            }
        }
Beispiel #21
0
        private void detectQuads(Bitmap bitmap)
        {
            // Greyscale
            filteredBitmap = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);

            // edge filter
            SobelEdgeDetector edgeFilter = new SobelEdgeDetector();
            edgeFilter.ApplyInPlace(filteredBitmap);

            // Threshhold filter
            Threshold threshholdFilter = new Threshold(190);
            threshholdFilter.ApplyInPlace(filteredBitmap);

            BitmapData bitmapData = filteredBitmap.LockBits(
                new Rectangle(0, 0, filteredBitmap.Width, filteredBitmap.Height),
                ImageLockMode.ReadWrite, filteredBitmap.PixelFormat);

            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 125;
            blobCounter.MinWidth = 125;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            filteredBitmap.UnlockBits(bitmapData);

            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();

            Bitmap bm = new Bitmap(filteredBitmap.Width, filteredBitmap.Height, PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(bm);
            g.DrawImage(filteredBitmap, 0, 0);

            Pen pen = new Pen(Color.Red, 5);
            List<IntPoint> cardPositions = new List<IntPoint>();

            // Loop through detected shapes
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List<IntPoint> corners;
                bool sameCard = false;

                // is triangle or quadrilateral
                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    // get sub-type
                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);

                    // Only return 4 corner rectanges
                    if ((subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rectangle) &&  corners.Count == 4)
                    {
                        // Check if its sideways, if so rearrange the corners so it's veritcal
                        rearrangeCorners(corners);

                        // Prevent it from detecting the same card twice
                        foreach (IntPoint point in cardPositions)
                        {
                            if (corners[0].DistanceTo(point) < 40)
                                sameCard = true;
                        }

                        if (sameCard)
                            continue;

                        // Hack to prevent it from detecting smaller sections of the card instead of the whole card
                        if (GetArea(corners) < 20000)
                            continue;

                        cardPositions.Add(corners[0]);

                        g.DrawPolygon(pen, ToPointsArray(corners));

                        // Extract the card bitmap
                        QuadrilateralTransformation transformFilter = new QuadrilateralTransformation(corners, 211, 298);
                        cardBitmap = transformFilter.Apply(cameraBitmap);

                        List<IntPoint> artCorners = new List<IntPoint>();
                        artCorners.Add(new IntPoint(14, 35));
                        artCorners.Add(new IntPoint(193, 35));
                        artCorners.Add(new IntPoint(193, 168));
                        artCorners.Add(new IntPoint(14, 168));

                        // Extract the art bitmap
                        QuadrilateralTransformation cartArtFilter = new QuadrilateralTransformation(artCorners, 183, 133);
                        cardArtBitmap = cartArtFilter.Apply(cardBitmap);

                        MagicCard card = new MagicCard();
                        card.corners = corners;
                        card.cardBitmap = cardBitmap;
                        card.cardArtBitmap = cardArtBitmap;

                        magicCards.Add(card);
                    }
                }
            }

            pen.Dispose();
            g.Dispose();

            filteredBitmap = bm;
        }
        private void cameraTask_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                
                BitmapImage bmp = new BitmapImage();
                bmp.SetSource(e.ChosenPhoto);
                bmp.ImageOpened += ImageOpenedTrue;
                //imgPreview.Source = bmp;
                //bmp.DecodePixelHeight = 460;
                //Height="460" Margin="10,10,0,0" VerticalAlignment="Top" Width="684">
                WriteableBitmap wbmp = new WriteableBitmap(bmp);
                //txtImage.Text = "Korkeus: " + wbmp.PixelHeight + " Leveys: " + wbmp.PixelWidth;
                Bitmap image = (Bitmap)wbmp;
                //AForge.Imaging.Filters.Grayscale
                //Grayscale gs = new Grayscale(

                // create filter
                ResizeNearestNeighbor filter = new ResizeNearestNeighbor(700, 480);
                // apply the filter
                Bitmap resizeImage = filter.Apply(image);
                txtImage.Text = "Korkeus: " + resizeImage.Height + " Leveys: " + resizeImage.Width;
                // create grayscale filter (BT709)
                Grayscale GSfilter = new Grayscale(0.2125, 0.7154, 0.0721);
                // apply the filter
                Bitmap grayImage = GSfilter.Apply(resizeImage);

                // create filter
                Threshold BIfilter = new Threshold(70);
                // apply the filter
                BIfilter.ApplyInPlace(grayImage);

                // create filter
                Invert Invertfilter = new Invert();
                // apply the filter
                Invertfilter.ApplyInPlace(grayImage);

                AForge.Imaging.Filters.ConnectedComponentsLabeling Blobfilter = new AForge.Imaging.Filters.ConnectedComponentsLabeling();
                Bitmap newImage = Blobfilter.Apply(grayImage);
                BlobCounter blobCounter = new BlobCounter(newImage);
                blobCounter.FilterBlobs = true;
                blobCounter.MinWidth = 5;
                blobCounter.MinHeight = 5;
                Blob[] blobs = blobCounter.GetObjectsInformation();
                gos = new List<GameObject>();
                List<Blob> blobList = blobs.ToList();
                PointCollection extremePoints = new PointCollection();
                System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle();
                PointCollection edges;
                List<AForge.IntPoint> lst;
                List<int[]> intlist = new List<int[]>();
                GameObject go = new GameObject();
                foreach (Blob b in blobs)
                {
                    lst = blobCounter.GetBlobsEdgePoints(b);
                    edges = new PointCollection();
                    foreach (AForge.IntPoint p in lst)
                    {
                        edges.Add(new System.Windows.Point(p.X, p.Y));
                    }
                    /*
                    extremePoints.Add(new System.Windows.Point(b.Rectangle.X, b.Rectangle.Y));
                    extremePoints.Add(new System.Windows.Point(b.Rectangle.X+b.Rectangle.Width, b.Rectangle.Y));
                    extremePoints.Add(new System.Windows.Point(b.Rectangle.X, b.Rectangle.Y+b.Rectangle.Height));
                    extremePoints.Add(new System.Windows.Point(b.Rectangle.X + b.Rectangle.Width, b.Rectangle.Y + b.Rectangle.Height));
                     * */
                    gos.Add(new GameObject(b.Rectangle.Y,b.Rectangle.X,b.Rectangle.Width,b.Rectangle.Height,edges)); // x ja y käännettynä, jotta lopullisessa mallissa suunnat oikein.
                    //GameObject go = new GameObject(
                    //extremePoints.Clear();
                    //edges.Clear();
                }
                //Serialize("LevelName.bin", gos);
                //SaveToLocalFolderAsync(e.ChosenPhoto, "LevelNameBG.jpg");
                //writeJsonAsync();
                fileName = txtBoxLevelName.Text;
                jsonWrite(fileName);
                WriteableBitmap wb;
                wb = (WriteableBitmap)resizeImage;

                using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    if (iso.FileExists(fileName+"_bg.jpg"))
                    {
                        iso.DeleteFile(fileName+"_bg.jpg");
                    }
                    using (IsolatedStorageFileStream isostream = iso.CreateFile(fileName+"_bg.jpg"))
                    {
                        Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 85);
                        isostream.Close(); 
                    }
                }

                //Rectangle[] rects = blobCounter.GetObjectsRectangles();
                //txtImage.Text = txtImage.Text + "Number of rectangles:+ " + rects.Length;

                wbmp = (WriteableBitmap)newImage;

                imgImage.Source = wbmp;
                // BY PIXEL
                /*
                foreach (int pix in wbmp.Pixels)
                {
                    int a = pix >> 24;
                    int r = (pix & 0x00ff0000) >> 16;
                    int g = (pix & 0x0000ff00) >> 8;
                    int b = (pix & 0x000000ff);
                    Color c = new Color(r, g, b, a);
                    int intensity = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11);
                    if (intensity > 127) ; // BLACK / SOLID
                    else ; // WHITE / NON-SOLID
                }*/
                //byte[] baLevel = CustomConvertToBytes(bmp);
                /*
                WriteableBitmap wbmp = new WriteableBitmap(bmp as BitmapSource);
                imgPreview.Source = wbmp;*/
            }
        }
Beispiel #23
0
 protected override void ProcessFilter(UnmanagedImage image, Rectangle rect)
 {
     thresholdFilter.ThresholdValue = CalculateThreshold(image, rect);
     thresholdFilter.ApplyInPlace(image, rect);
 }
 // =========================================================
 private void ThresholdFunct(ref Bitmap frame,  double par_d)
 {
     try {
         frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);
         Threshold filter = new Threshold((int)par_d);
         filter.ApplyInPlace(frame);
         GrayscaleToRGB toColFilter = new GrayscaleToRGB();
         frame = toColFilter.Apply(frame);
     } catch {
     }
 }
Beispiel #25
0
        // Process image
        private void ProcessImage(Bitmap bitmap)
        {
            // create filter
            HistogramEqualization hisFilter = new HistogramEqualization();
            // process image
            //hisFilter.ApplyInPlace(Image);

            // create filter
            ContrastCorrection contrastFilter = new ContrastCorrection(4);
            // apply the filter
            contrastFilter.ApplyInPlace(bitmap);

            //QuantizeImage(bitmap);

            Grayscale grayscaleFilter = new Grayscale(0.33, 0.33, 0.34);
            // apply the filter
            Bitmap grayImage = grayscaleFilter.Apply(bitmap);

            // create filter
            SobelEdgeDetector sobelFilter = new SobelEdgeDetector();
            // apply the filter
            sobelFilter.ApplyInPlace(grayImage);
            Threshold filter = new Threshold(110);
            // apply the filter
            filter.ApplyInPlace(grayImage);

            if (GetIntersect(grayImage, bitmap, intersectPoint)) {
                //MessageBox.Show("draw");
                Graphics g = Graphics.FromImage(bitmap);
                //Sorting

                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        for (int k = 0; k < 19 - 1; k++)
                        {
                            if (Math.Abs(intersectPoint[1, i, k]) > Math.Abs(intersectPoint[1, i, k + 1]))
                            {
                                int tempX = Math.Abs(intersectPoint[0, i, k]);
                                int tempY = Math.Abs(intersectPoint[1, i, k]);
                                intersectPoint[0, i, k] = Math.Abs(intersectPoint[0, i, k + 1]);
                                intersectPoint[1, i, k] = Math.Abs(intersectPoint[1, i, k + 1]);
                                intersectPoint[0, i, k + 1] = tempX;
                                intersectPoint[1, i, k + 1] = tempY;
                                //MessageBox.Show(i + " " + k +" " +tempX + " " + tempY + " " + intersectPoint[0, i, k] + " " + intersectPoint[1, i, k] ,"ok", MessageBoxButtons.OK);
                            }
                        }
                    }
                }

                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        for (int k = 0; k < 19 - 1; k++)
                        {
                            if (Math.Abs(intersectPoint[0, k, i]) > Math.Abs(intersectPoint[0, k + 1, i]))
                            {
                                int tempX = Math.Abs(intersectPoint[0, k, i]);
                                int tempY = Math.Abs(intersectPoint[1, k, i]);
                                intersectPoint[0, k, i] = intersectPoint[0, k + 1, i];
                                intersectPoint[1, k, i] = intersectPoint[1, k + 1, i];
                                intersectPoint[0, k + 1, i] = tempX;
                                intersectPoint[1, k + 1, i] = tempY;
                            }
                        }
                    }
                }

                Pen redPen = new Pen(Color.Red, 4);
                for (int i = 0; i < 19; i++)
                {
                    for (int j = 0; j < 19; j++)
                    {
                        g.DrawEllipse(redPen, Math.Abs(intersectPoint[0, i, j]) , Math.Abs(intersectPoint[1, i, j]) , (int)5, (int)5);
                        //g.DrawEllipse(redPen, 0, 0, (int)5, (int)5);

                        //Debug.WriteLine((Math.Abs(intersectPoint[0, i, j]) - 2) + " " + (Math.Abs(intersectPoint[1, i, j]) - 2));
                    }
                }

                Pen greenPen = new Pen(Color.Green, 4);

                greenPen.Dispose();
                redPen.Dispose();
                g.Dispose();

                // Initializes the variables to pass to the MessageBox.Show method.
                EspacioCamera.Image = bitmap;
                string message = "Do you accecpt this detection?";
                string caption = "The system found totally 38 lines.";
                MessageBoxButtons buttons = MessageBoxButtons.YesNo;
                DialogResult result;

                // Displays the MessageBox.

                result = MessageBox.Show(message, caption, buttons);

                if (result == System.Windows.Forms.DialogResult.Yes)
                {
                    // Closes the parent form.
                    locked = true;
                }
            }

            EspacioCamera.Image = bitmap;
        }
Beispiel #26
0
        // Process image
        private void ProcessImage( Bitmap bitmap )
        {
            Grayscale grayscaleFilter = new Grayscale(0.33, 0.33, 0.34);
            // apply the filter
            Bitmap grayImage = grayscaleFilter.Apply(bitmap);

            //Get Histogram
            /*int[] histogram = new int[256];
            for (int i = 0; i < 256; i++) {
                histogram[i] = 0;
            }
            for (int i = 0; i < grayImage.Width; i++ )
            {
                for (int j = 0; j < grayImage.Height; j++) {
                    histogram[(Convert.ToInt32(grayImage.GetPixel(i, j).R.ToString()) + Convert.ToInt32(grayImage.GetPixel(i, j).G.ToString()) +
                        Convert.ToInt32(grayImage.GetPixel(i, j).B.ToString()))/3] +=1;
                }
            }*/

            //create filter
            SobelEdgeDetector sobelFilter = new SobelEdgeDetector();
            // apply the filter
            sobelFilter.ApplyInPlace(grayImage);
            Threshold filter = new Threshold(243);
            // apply the filter
            filter.ApplyInPlace(grayImage);

            HoughLineTransformation lineTransform = new HoughLineTransformation();

            lineTransform.ProcessImage(grayImage);
            Bitmap houghLineImage = lineTransform.ToBitmap();
            // get lines using relative intensity
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.5);

            BitmapData bitmapData = bitmap.LockBits(
               new Rectangle(0, 0, bitmap.Width, bitmap.Height),
               ImageLockMode.ReadWrite, bitmap.PixelFormat);
            BitmapData grayImageData = grayImage.LockBits(
               new Rectangle(0, 0, grayImage.Width, grayImage.Height),
               ImageLockMode.ReadWrite, grayImage.PixelFormat);
            UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

            int w2 = 0, h2 = 0;
            int vertical = 0;
            int horizons = 0;

            int testColor = 0;
            Color color = Color.Black;

            foreach (HoughLine line in lines)
            {

                String temp = line.Theta.ToString();
                //MessageBox.Show(temp, "ok", MessageBoxButtons.OK);
                // get line's radius and theta values
                int r = line.Radius;
                double t = line.Theta;

                // check if line is in lower part of the image
                if (r < 0)
                {
                    t += 180;
                    r = -r;
                }

                // convert degrees to radians
                t = (t / 180) * Math.PI;

                // get image centers (all coordinate are measured relative
                // to center)
                w2 = grayImage.Width / 2;
                h2 = grayImage.Height / 2;

                double x0 = 0, x1 = 0, y0 = 0, y1 = 0;

                if (line.Theta != 0)
                {
                    // horizontal
                    // none-vertical line

                    if (line.Theta > 0 && line.Theta < 10 || line.Theta > 170)
                    {
                        x0 = -w2; // most left point
                        x1 = w2;  // most right point

                        y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                        y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);

                        color = Color.Red;

                        Drawing.Line(bitmapData,
                        new IntPoint((int)x0 + w2, h2 - (int)y0),
                        new IntPoint((int)x1 + w2, h2 - (int)y1),
                        color);
                        Drawing.Line(grayImageData,
                         new IntPoint((int)x0 + w2, h2 - (int)y0),
                         new IntPoint((int)x1 + w2, h2 - (int)y1),
                         color);
                    }

                    if (line.Theta > 80 && line.Theta < 100)
                    {
                        x0 = -w2; // most left point
                        x1 = w2;  // most right point

                        y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                        y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);

                        Drawing.Line(bitmapData,
                        new IntPoint((int)x0 + w2, h2 - (int)y0),
                        new IntPoint((int)x1 + w2, h2 - (int)y1),
                        Color.Blue);
                        Drawing.Line(grayImageData,
                         new IntPoint((int)x0 + w2, h2 - (int)y0),
                         new IntPoint((int)x1 + w2, h2 - (int)y1),
                         Color.Blue);
                    }

                }
                else
                {
                    // vertical line
                    x0 = line.Radius;
                    x1 = line.Radius;
                    y0 = h2;
                    y1 = -h2;

                    color = Color.Red;

                    Drawing.Line(bitmapData,
                    new IntPoint((int)x0 + w2, h2 - (int)y0),
                    new IntPoint((int)x1 + w2, h2 - (int)y1),
                    color);
                    Drawing.Line(grayImageData,
                     new IntPoint((int)x0 + w2, h2 - (int)y0),
                     new IntPoint((int)x1 + w2, h2 - (int)y1),
                     color);
                }
            }

            bitmap.UnlockBits(bitmapData);
            grayImage.UnlockBits(grayImageData);
            MessageBox.Show(lines.Length+"", "ok", MessageBoxButtons.OK);
            // put new image to clipboard

            Clipboard.SetDataObject(bitmap);
            // and to picture box
            pictureBox.Image = bitmap;

            UpdatePictureBoxPosition();
        }
        public void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            // get new frame
            CheckForIllegalCrossThreadCalls = false;
            Bitmap bitmap = (Bitmap)eventArgs.Frame.Clone();
            if (pictureBox1.Image == null)
            {
                bitmap.Save(@"modelo0.jpg");
                bgFrame = bitmap;
            }
            else
            {
                try
                {
                    bgFrame = (Bitmap)pictureBox1.Image.Clone();
                    //bitmap.Save(@"currImage.jpg");
                    currentFrame = (Bitmap)bitmap.Clone();

                    Bitmap gcurrent = Grayscale.CommonAlgorithms.BT709.Apply(bitmap);
                    Bitmap gbground = Grayscale.CommonAlgorithms.BT709.Apply(bgFrame);

                    Difference differenceFilter = new Difference();
                    differenceFilter.OverlayImage = gbground;
                    Bitmap currentImg = differenceFilter.Apply(gcurrent);

                    //bloqueo temporal de current image
                    BitmapData currentData = currentImg.LockBits(new Rectangle(0, 0, framesProcessing.fWidth,
                                        framesProcessing.fHeight), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                    //**Thresholdfilter
                    Threshold thresholdFilter = new Threshold(15);
                    thresholdFilter.ApplyInPlace(currentData);
                    IFilter erosionFilter = new Erosion();
                    Bitmap tmp2 = erosionFilter.Apply(currentData);

                    currentImg.UnlockBits(currentData);
                    i = framesProcessing.CalculateWhitePixels(tmp2);

                    if (i > 300)
                    {
                        mov = mov2 = true;
                    }
                    else
                        mov = false;

                    pictureBox2.Image = currentImg;
                }
                catch{//MessageBox.Show(ex.Message.ToString());
                }

            }
            pictureBox1.Image = bitmap;
        }
Beispiel #28
0
        private void ProcessImage()
        {
            m_selectedBlobs.Clear();
            pictureBox1.Controls.Clear();
            pictureBox1.Image = null;

            if (m_original != null)
                m_original.Dispose();
            if (m_binarized != null)
                m_binarized.Dispose();

            m_original = new Bitmap(txtFile.Text);

            // create grayscale filter (BT709)
            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            m_binarized = filter.Apply(m_original);

            // Binarize Picture.
            Threshold bin = new Threshold((int)txtBinThershold.Value);
            bin.ApplyInPlace(m_binarized);

            // create filter
            Invert inv = new Invert();
            inv.ApplyInPlace(m_binarized);

            // create an instance of blob counter algorithm
            BlobCounter bc = new BlobCounter();
            bc.ObjectsOrder = ObjectsOrder.XY;
            bc.ProcessImage(m_binarized);
            Rectangle[] blobsRect = bc.GetObjectsRectangles();
            Dictionary<int, List<Rectangle>> orderedBlobs = ReorderBlobs(blobsRect);

            foreach (KeyValuePair<int, List<Rectangle>> orderedBlob in orderedBlobs)
            {
                orderedBlob.Value.ForEach(r => AddBlobPanel(orderedBlob.Key, r));
            }

            pictureBox1.Image = chkShowBinarize.Checked ? m_binarized : m_original;

            pictureBox1.Invalidate();
        }
Beispiel #29
0
 // =========================================================
 private void ThresholdFunct(ref Bitmap frame, int par_int, double par_d, int par_R, int par_G, int par_B)
 {
     frame = Grayscale.CommonAlgorithms.RMY.Apply(frame);
     Threshold filter = new Threshold(par_int);
     filter.ApplyInPlace(frame);
     GrayscaleToRGB toColFilter = new GrayscaleToRGB();
     frame = toColFilter.Apply(frame);
 }
Beispiel #30
0
        //eventhandler if new frame is ready
        private void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
        {
            Bitmap img = (Bitmap)eventArgs.Frame.Clone();
            if (counterImg == 10)
            {

                double delaisImage = DateTime.Now.TimeOfDay.TotalMilliseconds - _mill_last_pic;
                _mill_last_pic = DateTime.Now.TimeOfDay.TotalMilliseconds;

                double FPS = 1 / delaisImage * 1000 * counterImg + 1;
                // txt_nb_fps.Text = FPS.ToString() ;

                //txt_resolution.Text = "" + videoSource.DesiredFrameSize.Height + " * " + videoSource.DesiredFrameSize.Width;
                string resolutionTxt = "" + img.Width + " * " + img.Height;
                if (this != null && (!this.IsDisposed))
                {
                    try
                    {
                        this.Invoke((ProcessNewFPS)UpdateNewFPS, FPS);
                        this.Invoke((ProcessNewResolution)UpdateNewResolution, resolutionTxt);
                    }
                    catch (ObjectDisposedException) // La fenetre était en train de se fermée
                    {
                    }
                }
                counterImg = 0;
            }
            counterImg++;

            //Rectangle rect = new Rectangle(0,0,eventArgs.Frame.Width,eventArgs.Frame.Height);

            // 1 - grayscaling
            UnmanagedImage image = UnmanagedImage.FromManagedImage(img);
            UnmanagedImage imageRouge = image.Clone();
            UnmanagedImage imageBleu = image.Clone();
            UnmanagedImage imageVert = image.Clone();
            UnmanagedImage grayImage = null;

            Color colorPoint = image.GetPixel(posX, posY);

            this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetHue().ToString(), lbl_hue });
            this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetBrightness().ToString(), lbl_lum });
            this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetSaturation().ToString(), lbl_sat });

            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                grayImage = image;
            }
            else
            {
                grayImage = UnmanagedImage.Create(image.Width, image.Height,
                    PixelFormat.Format8bppIndexed);
                Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage);
            }

            // 2 - Edge detection
            DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector();
            UnmanagedImage edgesImage = edgeDetector.Apply(grayImage);

            // 3 - Threshold edges
            Threshold thresholdFilterGlyph = new Threshold((short)numericUpDown3.Value);
            Threshold thresholdFilterCouleur = new Threshold((short)numericUpDown2.Value);

            thresholdFilterGlyph.ApplyInPlace(edgesImage);

            /*
             *
             * Bitmap image = (Bitmap)eventArgs.Frame.Clone();

            //Reference : http://www.aforgenet.com/framework/docs/html/743311a9-6c27-972d-39d2-ddc383dd1dd4.htm

             *  private HSLFiltering filter = new HSLFiltering();
            // set color ranges to keep red-orange
            filter.Hue = new IntRange(0, 20);
            filter.Saturation = new DoubleRange(0.5, 1);

            // apply the filter
            filter.ApplyInPlace(image);
             * */
            /*RGB colorRed = new RGB(215, 30, 30);
            RGB colorBlue = new RGB(10, 10, 215);
            RGB colorVert = new RGB(30, 215, 30);
            RGB colorBlanc = new RGB(225, 219, 160);*/

            HSLFiltering filter = new HSLFiltering();
            // create filter
               // EuclideanColorFiltering filter = new EuclideanColorFiltering();
            //filter.Radius = (short)numericUpDown1.Value;
            filter.Hue = new IntRange(40, 140);
            filter.Saturation = new Range(0.5f, 1.0f);
            filter.Luminance = new Range(0.2f, 1.0f);

            //filter.CenterColor = colorRed;
            filter.ApplyInPlace(imageRouge);

            filter.Hue = new IntRange(100, 180);
            //filter.CenterColor = colorBlanc;
            filter.ApplyInPlace(imageVert);

            filter.Hue = new IntRange(0, 40);
             //filter.CenterColor = colorBlue;
            filter.ApplyInPlace(imageBleu);

            Grayscale filterRouge = new Grayscale(0.800, 0.200, 0.200);
            Grayscale filterVert = new Grayscale(0.200, 0.800, 0.200);
            Grayscale filterBleu = new Grayscale(0.200, 0.200, 0.800);

            UnmanagedImage grayRougeImage = filterRouge.Apply(imageRouge);
            UnmanagedImage grayBleuImage = filterBleu.Apply(imageBleu);

            UnmanagedImage edgesRougeImage = edgeDetector.Apply(grayRougeImage);
            UnmanagedImage edgesBleuImage = edgeDetector.Apply(grayBleuImage);

            thresholdFilterCouleur.ApplyInPlace(edgesRougeImage);
            thresholdFilterCouleur.ApplyInPlace(edgesBleuImage);
            // All the image processing is done here...

            // pictureBox1.Image = image.ToManagedImage();
            if (this != null && (!this.IsDisposed)) // Si on est pas en train de suppirmer la fenetre
            {
                try
                {
                    this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { image,         pic_ImageNormal });
                    this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { edgesImage,    pic_ImageEdge });

                    this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageRouge, pic_ImageRouge });

                    this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageBleu, pic_ImageBleu });
                    this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageVert, pic_ImageVert });
                }
                catch (ObjectDisposedException) // La fenetre était en train de se fermée
                {
                }
            }
            /*pictureBox2.Image = grayImage.ToManagedImage();
            pictureBox3.Image = edgesImage.ToManagedImage();
            pictureBox4.Image = imageRouge.ToManagedImage();*/
        }
Beispiel #31
0
        public static IEnumerable<MagicCard> DetectCardArt(Bitmap cameraBitmap)
        {
            var ret = new List<MagicCard>();

            var filteredBitmap = Grayscale.CommonAlgorithms.BT709.Apply(cameraBitmap);

            // edge filter
            var edgeFilter = new SobelEdgeDetector();
            edgeFilter.ApplyInPlace(filteredBitmap);

            // Threshhold filter
            var threshholdFilter = new Threshold(190);
            threshholdFilter.ApplyInPlace(filteredBitmap);

            var bitmapData = filteredBitmap.LockBits(
                new Rectangle(0, 0, filteredBitmap.Width, filteredBitmap.Height),
                ImageLockMode.ReadWrite, filteredBitmap.PixelFormat);

            var blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 125;
            blobCounter.MinWidth = 125;

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();
            filteredBitmap.UnlockBits(bitmapData);

            var shapeChecker = new SimpleShapeChecker();
            var bm = new Bitmap(filteredBitmap.Width, filteredBitmap.Height, PixelFormat.Format24bppRgb);

            var cardPositions = new List<IntPoint>();
            foreach (var blob in blobs)
            {
                var edgePoints = blobCounter.GetBlobsEdgePoints(blob);

                List<IntPoint> corners;

                // only operate on 4 sided polygons
                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    var subtype = shapeChecker.CheckPolygonSubType(corners);

                    if (corners.Count() != 4)
                        continue;

                    if (subtype != PolygonSubType.Parallelogram && subtype != PolygonSubType.Rectangle)
                        continue;

                    // if the image is sideways, rotate it so it'll match the DB card art
                    corners = Utilities.RotateCorners(corners).ToList();

                    if (Utilities.GetArea(corners) < 20000)
                        continue;

                    ret.Add( new MagicCard(cameraBitmap, corners));
                }
            }

            return ret;
        }
Beispiel #32
0
        // Process image
        private void ProcessImage( Bitmap bitmap )
        {
            //QuantizeImage(bitmap);

            Grayscale grayscaleFilter = new Grayscale(0.33, 0.33, 0.34);
            // apply the filter
            Bitmap grayImage = grayscaleFilter.Apply(bitmap);

            // create filter
            SobelEdgeDetector sobelFilter = new SobelEdgeDetector();
            // apply the filter
            sobelFilter.ApplyInPlace(grayImage);
            Threshold filter = new Threshold(243);
            // apply the filter
            filter.ApplyInPlace(grayImage);

            int[,,] intersectPoint = new int[2,19,19];
            GetIntersect(grayImage,bitmap,intersectPoint);

            Graphics g = Graphics.FromImage(bitmap);
            //Sorting
            /*int[,] top19 = new int[19, 2];
            int[,] left19 = new int[19, 2];
            int[,] bot19 = new int[19, 2];
            int[,] right19 = new int[19, 2];
            int lowest = 9999999;
            int lowX = 0;
            int lowY = 0;
            for (int i = 0; i < 19; i++) {
                for (int j = 0; j < 19; j++) {
                    if (intersectPoint[0, i, j] + intersectPoint[1, i, j] < lowest) {
                        lowest = intersectPoint[0, i, j] + intersectPoint[1, i, j];
                        lowX = Math.Abs(intersectPoint[0, i, j]);
                        lowY = Math.Abs(intersectPoint[1, i, j]);
                    }
                }
            }
            */

            for (int i = 0; i < 19; i++) {
                for (int j = 0; j < 19; j++) {
                    for (int k = 0; k < 19 - 1; k++) {
                        if(Math.Abs(intersectPoint[1,i,k]) > Math.Abs(intersectPoint[1,i,k+1])){
                            int tempX = Math.Abs(intersectPoint[0,i,k]);
                            int tempY = Math.Abs(intersectPoint[1,i,k]);
                            intersectPoint[0,i,k] = Math.Abs(intersectPoint[0,i,k+1]);
                            intersectPoint[1,i,k] = Math.Abs(intersectPoint[1,i,k+1]);
                            intersectPoint[0,i,k+1] = tempX;
                            intersectPoint[1,i,k+1] = tempY;
                            //MessageBox.Show(i + " " + k +" " +tempX + " " + tempY + " " + intersectPoint[0, i, k] + " " + intersectPoint[1, i, k] ,"ok", MessageBoxButtons.OK);
                        }
                    }
                }
            }

            for (int i = 0; i < 19; i++) {
                for (int j = 0; j < 19; j++) {
                    for (int k = 0; k < 19 - 1; k++) {
                        if(Math.Abs(intersectPoint[0,k,i]) > Math.Abs(intersectPoint[0,k+1,i])){
                            int tempX = Math.Abs(intersectPoint[0,k,i]);
                            int tempY = Math.Abs(intersectPoint[1,k,i]);
                            intersectPoint[0,k,i] = intersectPoint[0,k+1,i];
                            intersectPoint[1,k,i] = intersectPoint[1,k+1,i];
                            intersectPoint[0,k+1,i] = tempX;
                            intersectPoint[1,k+1,i] = tempY;
                        }
                    }
                }
            }

            Pen redPen = new Pen(Color.Red, 4);
            for (int i = 0; i < 19; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    g.DrawEllipse(redPen, Math.Abs(intersectPoint[0, i, j]) - 2, Math.Abs(intersectPoint[1, i, j]) - 2, (int)5, (int)5);
                    //g.DrawEllipse(redPen, 0, 0, (int)5, (int)5);
                }
            }
            Pen greenPen = new Pen(Color.Green, 4);

            greenPen.Dispose();
            redPen.Dispose();
            g.Dispose();

            /*HoughLineTransformation lineTransform = new HoughLineTransformation();
            lineTransform.ProcessImage(grayImage);
            Bitmap houghLineImage = lineTransform.ToBitmap();
            // get lines using relative intensity
            HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(0.5);

            BitmapData bitmapData = bitmap.LockBits(
               new Rectangle(0, 0, bitmap.Width, bitmap.Height),
               ImageLockMode.ReadWrite, bitmap.PixelFormat);
            BitmapData grayImageData = grayImage.LockBits(
               new Rectangle(0, 0, grayImage.Width, grayImage.Height),
               ImageLockMode.ReadWrite, grayImage.PixelFormat);
            UnmanagedImage unmanagedImage = new UnmanagedImage(bitmapData);

            int w2 = 0, h2 = 0;
            Color color = Color.Black;

            foreach (HoughLine line in lines)
            {

                String temp = line.Theta.ToString();
                //MessageBox.Show(temp, "ok", MessageBoxButtons.OK);
                // get line's radius and theta values
                int r = line.Radius;
                double t = line.Theta;

                // check if line is in lower part of the image
                if (r < 0)
                {
                    t += 180;
                    r = -r;
                }

                // convert degrees to radians
                t = (t / 180) * Math.PI;

                // get image centers (all coordinate are measured relative
                // to center)
                w2 = grayImage.Width / 2;
                h2 = grayImage.Height / 2;

                double x0 = 0, x1 = 0, y0 = 0, y1 = 0;

                if (line.Theta != 0)
                {
                    // horizontal
                    // none-vertical line

                    if (line.Theta > 0 && line.Theta < 10 || line.Theta > 170)
                    {
                        x0 = -w2; // most left point
                        x1 = w2;  // most right point

                        y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                        y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);

                        color = Color.Red;

                        Drawing.Line(bitmapData,
                        new IntPoint((int)x0 + w2, h2 - (int)y0),
                        new IntPoint((int)x1 + w2, h2 - (int)y1),
                        color);
                        Drawing.Line(grayImageData,
                        new IntPoint((int)x0 + w2, h2 - (int)y0),
                        new IntPoint((int)x1 + w2, h2 - (int)y1),
                        color);
                    }

                    if (line.Theta > 80 && line.Theta < 100)
                    {
                        x0 = -w2; // most left point
                        x1 = w2;  // most right point

                        y0 = (-Math.Cos(t) * x0 + r) / Math.Sin(t);
                        y1 = (-Math.Cos(t) * x1 + r) / Math.Sin(t);

                        Drawing.Line(bitmapData,
                        new IntPoint((int)x0 + w2, h2 - (int)y0),
                        new IntPoint((int)x1 + w2, h2 - (int)y1),
                        Color.Blue);
                        Drawing.Line(grayImageData,
                         new IntPoint((int)x0 + w2, h2 - (int)y0),
                         new IntPoint((int)x1 + w2, h2 - (int)y1),
                         Color.Blue);
                    }
                }
                else
                {
                    // vertical line
                    x0 = line.Radius;
                    x1 = line.Radius;
                    y0 = h2;
                    y1 = -h2;

                    color = Color.Red;

                    Drawing.Line(bitmapData,
                    new IntPoint((int)x0 + w2, h2 - (int)y0),
                    new IntPoint((int)x1 + w2, h2 - (int)y1),
                    color);
                    Drawing.Line(grayImageData,
                     new IntPoint((int)x0 + w2, h2 - (int)y0),
                     new IntPoint((int)x1 + w2, h2 - (int)y1),
                     color);
                }
            }
            bitmap.UnlockBits(bitmapData);
            grayImage.UnlockBits(grayImageData);
            MessageBox.Show(lines.Length+"", "ok", MessageBoxButtons.OK);
            */
            Clipboard.SetDataObject(bitmap);
            // and to picture box
            pictureBox.Image = bitmap;
            UpdatePictureBoxPosition();
        }