Exemplo n.º 1
0
        public static Option <Tuple <uint, DataMatrixExtraction> > ExtractCodeExt(Bitmap sourceImage, int minPatternRadius, int maxPatternRadius)
        {
            List <Point3> finderCircles = FinderCircleHoughTransform.LocateFinderCircles(sourceImage, minPatternRadius, maxPatternRadius, 2);

            var fpp = new FinderPatternPair();

            fpp.p1    = new Point(finderCircles[0].X, finderCircles[0].Y);
            fpp.size1 = finderCircles[0].Z;
            fpp.p2    = new Point(finderCircles[1].X, finderCircles[1].Y);
            fpp.size2 = finderCircles[1].Z;

            var dme = new DataMatrixExtraction(sourceImage, fpp);

            Option <uint> extractedCode = DataMarshaller.UnMarshallInt(dme.extractedData);

            if (extractedCode.NonEmpty())
            {
                return(new Some <Tuple <uint, DataMatrixExtraction> >(new Tuple <uint, DataMatrixExtraction>(extractedCode.Get(), dme)));
            }
            else
            {
                return(new None <Tuple <uint, DataMatrixExtraction> >());
            }
        }
Exemplo n.º 2
0
        private void RunOCR(Bitmap sourceImage, int minPatternRadius, int maxPatternRadius, uint inputValue, NoiseFilter noiseFilter)
        {
            this.inputDataLabel.Text = inputValue.ToString();

            Bitmap grayImage = ImageUtil.ToGrayscale(sourceImage);

            this.inputImagePV.Image = grayImage;

            Bitmap noiseImage = noiseFilter.Apply(sourceImage);

            if (noiseFilter.GetType() != typeof(EmptyFilter))
            {
                this.noiseImagePV.Image = noiseImage;
            }

            int scaleFactor = FinderCircleHoughTransform.GetScaleFactor(minPatternRadius);

            Console.WriteLine("scaleFactor = " + scaleFactor);
            Bitmap downscaledImage = ImageScaling.ScaleDown(noiseImage, scaleFactor);

            int[,,] hough = Util.Timed("hough transform", () =>
                                       FinderCircleHoughTransform.HoughTransform(downscaledImage, minPatternRadius / scaleFactor, maxPatternRadius / scaleFactor));
            Bitmap houghTransformImage = FinderCircleHoughTransform.HoughTransformImage(hough);

            this.houghImagePV.Image = houghTransformImage;
            List <Point3> peaks         = FinderCircleHoughTransform.LocatePeaks(hough, 2, minPatternRadius / scaleFactor);
            List <Point3> descaledPeaks = peaks.ConvertAll(p => new Point3(p.X * scaleFactor, p.Y * scaleFactor, p.Z * scaleFactor + minPatternRadius));

            foreach (var p in descaledPeaks)
            {
                Console.WriteLine("Raw peak at {0}x{1}x{2}", p.X, p.Y, p.Z);
            }
            List <Point3> tunedPeaks = Util.Timed("tune peaks", () =>
                                                  descaledPeaks.ConvertAll(peak => FinderCircleHoughTransform.TunePeak(noiseImage, minPatternRadius, maxPatternRadius, peak)));

            foreach (var p in tunedPeaks)
            {
                Console.WriteLine("Tuned peak at {0}x{1}x{2}", p.X, p.Y, p.Z);
            }
            Bitmap houghPeaksImage = new Bitmap(houghTransformImage);

            DrawPeaks(houghPeaksImage, peaks, Color.Red);
            this.houghPeakImagePV.Image = houghPeaksImage;

            Bitmap resultPeaksImage = new Bitmap(noiseImage);

            DrawPeaks(resultPeaksImage, descaledPeaks, Color.Red);
            DrawPeaks(resultPeaksImage, tunedPeaks, Color.Green);
            this.peakResultImagePV.Image = resultPeaksImage;

            FinderPatternPair fpp = new FinderPatternPair();

            fpp.p1    = new Point(tunedPeaks[0].X, tunedPeaks[0].Y);
            fpp.size1 = tunedPeaks[0].Z;
            fpp.p2    = new Point(tunedPeaks[1].X, tunedPeaks[1].Y);
            fpp.size2 = tunedPeaks[1].Z;

            DataMatrixExtraction dme     = new DataMatrixExtraction(noiseImage, fpp);
            Bitmap positioningDebugImage = new Bitmap(noiseImage);

            dme.DrawPositioningDebug(positioningDebugImage);
            dataMatrixLocationPV.Image   = positioningDebugImage;
            rotatedDataMatrixPV.Image    = dme.rotatedMatrix;
            recognizedDataMatrixPV.Image = dme.RecognitionDebugImage();
            Option <uint> extractedCode = DataMarshaller.UnMarshallInt(dme.extractedData);

            outputDataLabel.Text = extractedCode.Map(c => c.ToString()).GetOrElse("none");
        }