Пример #1
0
        private void Test3()
        {
            byte[] imageData = System.IO.File.ReadAllBytes(@"./wwwroot/images/template2.png");
            Mat    img1      = Mat.FromImageData(imageData, ImreadModes.Color);
            Mat    gray1     = Mat.FromImageData(imageData, ImreadModes.Grayscale);

            //gray1 = gray1.GaussianBlur (new OpenCvSharp.Size(3, 3), 0);
            gray1 = gray1.AdaptiveThreshold(255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.BinaryInv, 105, 2); // 11,2 ; 75,10 ; 60,255
            //gray1 = gray1.Threshold(60, 255, ThresholdTypes.BinaryInv);


            //Canny Edge Detector
            //Image<Gray, Byte> cannyGray = gray1.Canny(20, 50);
            //Image<Bgr, Byte> imageResult = img1.Copy();
            //Mat cannyGray = gray1.Canny(20, 35); // 0, 12, blur 9; 2, 17,  blur 7; 0, 25 blur 13; 20 35 blur 0
            //var cannyGray = gray1;


            //Cv2.FindContours(cannyGray, out var contours, out var hierarchy, mode: RetrievalModes.Tree, method: ContourApproximationModes.ApproxSimple);


            Mat copy = gray1.Clone();

            copy.SaveImage("wwwroot/images/output.png");
        }
Пример #2
0
        public static Mat AdaptiveTresholding(Mat greyImage, int c = 2, double perc = 0.001)
        {
            //var spix = (int)Math.Sqrt(greyImage.Width * greyImage.Height * perc);
            //spix = spix % 2 == 1 ? spix : spix + 1;
            //spix = spix < 3 ? 3 : spix;

            var thresh = greyImage.AdaptiveThreshold(255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 11, c);

            return(thresh);
        }
Пример #3
0
        private Rectangle[] DetectMeterNumbers(Mat image)
        {
            var blobDetector = new BlobDetector();

            var threshold = image.AdaptiveThreshold(255,
                                                    AdaptiveThresholdTypes.GaussianC,
                                                    _settings.DarkSectors ? ThresholdTypes.Binary : ThresholdTypes.BinaryInv,
                                                    _settings.NumbersDetectionAdaptiveThresholdBlockSize,
                                                    _settings.NumbersDetectionAdaptiveThresholdC);

            return(blobDetector.DetectMeterNumbers(threshold).ToArray());
        }
Пример #4
0
        private static Mat apply_doc_filters(Mat image)
        {
            //if closed rectangle of the document cant be detected then we will not transform the image but just apply simple filter to make it look like scanned doc

            //apply grayscale
            //Step 6: grayscale it to give it that 'black and white' paper effect
            image = image.CvtColor(ColorConversionCodes.BGR2GRAY);
            //transformImage = transformImage.Threshold(127, 255, ThresholdTypes.Binary);
            //transformImage = transformImage.Dilate(null);
            image = image.AdaptiveThreshold(255, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 17, 11);

            ////add a border to the image to act as border of the doc
            //modifiedImage = modifiedImage.CopyMakeBorder(5, 5, 5, 5, BorderTypes.Constant, value: Scalar.Black);

            return(image);
        }
Пример #5
0
        static void Main(string[] args)
        {
            string filePath = @"SampleImage.jpg";
            var    img      = new Mat(filePath, ImreadModes.Grayscale);

            Mat orginalImg = new Mat();

            img.CopyTo(orginalImg);
            img = ReSizeImage(img);


            Mat edges  = DetectEdges(img);
            var points = GetContours(edges);

            Mat warpedImg = PointTransform(img, points);

            warpedImg = warpedImg.CvtColor(ColorConversionCodes.BayerBG2GRAY);

            var thresh = warpedImg.AdaptiveThreshold(225, AdaptiveThresholdTypes.GaussianC, ThresholdTypes.Binary, 11, 2);

            Cv2.ImShow("results", thresh);
            Cv2.WaitKey();
        }