public static Mat PreprocessImageForTesseract(Mat img)
        {
            int scalePercent = 18;
            int newWidth     = (int)img.Width * scalePercent / 100;
            int newHeight    = (int)img.Height * scalePercent / 100;

            CvInvoke.Resize(img, img, new System.Drawing.Size(newWidth, newHeight), interpolation: Inter.Area);
            img = ImageProcessor.ApplyBlur(img, 0, 3);
            Mat output = new Mat(img.Size, DepthType.Cv8U, 3);

            CvInvoke.CvtColor(img, img, ColorConversion.Bgr2Gray);
            //CvInvoke.EqualizeHist(img, img);
            CvInvoke.BitwiseNot(img, img);
            //img = ImageProcessor.CannyEdgeDetection(img, 20, 20);
            //img = ImageProcessor.ApplyErosion(img, 3);
            //CvInvoke.GaussianBlur(img, img, new System.Drawing.Size(3, 3), 0);
            CvInvoke.AdaptiveThreshold(img, img, 255, AdaptiveThresholdType.GaussianC, ThresholdType.Binary, 11, 2);
            CvInvoke.Threshold(img, output, 0, 255, ThresholdType.Otsu);//double ret =
            //output = ImageProcessor.ApplyErosion(output, 3);
            //CvInvoke.Threshold(output, output, ret, 255, ThresholdType.Binary);
            var kernel = CvInvoke.GetStructuringElement(Emgu.CV.CvEnum.ElementShape.Rectangle, new System.Drawing.Size(2, 2), new System.Drawing.Point(-1, -1));

            CvInvoke.Dilate(output, output, kernel, new System.Drawing.Point(-1, -1), 2, Emgu.CV.CvEnum.BorderType.Constant, default(MCvScalar));
            //output = ImageProcessor.ApplyDilation(output, 7);
            //CvInvoke.Invert()

            return(output);
        }
Exemplo n.º 2
0
        private void BtnPreviewTemplate_Click(object sender, RoutedEventArgs e)
        {
            if (parent.currentScreen != null)
            {
                Mat img  = parent.selectedScreen;//CvInvoke.Imread("test_save.jpg", Emgu.CV.CvEnum.LoadImageType.Grayscale);
                Mat warp = new Mat(img.Size, Emgu.CV.CvEnum.DepthType.Cv8U, 1);
                img.CopyTo(warp);
                img = ImageProcessor.ApplyBlur(img, 0, 3);

                img = ImageProcessor.CannyEdgeDetection(img, lowThreshold, highThreshold);
                img = ImageProcessor.ApplyDilation(img, 3);
                img = ImageProcessor.ApplyErosion(img, 3);

                LineSegment2D[] lines       = ImageProcessor.HoughLines(img, rho, houghThreshold, minLineLength, maxLineGap);
                LineSegment2D[] singleLines = Utils.GetSingleLinesFromHoughLines(lines, 20);
                if (singleLines != null)
                {
                    Mat mask          = ImageProcessor.AddLines(singleLines, Utils.GetRatioOfSelectedArea(Utils.GetPoints(parent.currentScreen.coordinates)));
                    Mat templateImage = ImageProcessor.ApplyMask(warp, mask);
                    parent.template.SetTemplateImageAndMask(templateImage.ToImage <Bgr, byte>(), mask.ToImage <Bgr, byte>());
                    tempImage.Source    = Utils.ToBitmapSource(templateImage);
                    templateMask.Source = Utils.ToBitmapSource(mask);
                    //lblTemplateName.Visibility = Visibility.Visible;
                    //txtTemplateName.Visibility = Visibility.Visible;
                    btnSaveTemplate.Visibility = Visibility.Visible;
                }
                else
                {
                    MessageBox.Show("Couldn't find any lines with the specified parameters");
                    InitImages();
                }
            }
            else
            {
                MessageBox.Show("You didn't select any screen! ", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }