Dilatation operator from Mathematical Morphology
Inheritance: IFilter
Beispiel #1
1
 private string reconhecerCaptcha(Image img)
 {
     Bitmap imagem = new Bitmap(img);
     imagem = imagem.Clone(new Rectangle(0, 0, img.Width, img.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
     Erosion erosion = new Erosion();
     Dilatation dilatation = new Dilatation();
     Invert inverter = new Invert();
     ColorFiltering cor = new ColorFiltering();
     cor.Blue = new AForge.IntRange(200, 255);
     cor.Red = new AForge.IntRange(200, 255);
     cor.Green = new AForge.IntRange(200, 255);
     Opening open = new Opening();
     BlobsFiltering bc = new BlobsFiltering();
     Closing close = new Closing();
     GaussianSharpen gs = new GaussianSharpen();
     ContrastCorrection cc = new ContrastCorrection();
     bc.MinHeight = 10;
     FiltersSequence seq = new FiltersSequence(gs, inverter, open, inverter, bc, inverter, open, cc, cor, bc, inverter);
     pictureBox.Image = seq.Apply(imagem);
     string reconhecido = OCR((Bitmap)pictureBox.Image);
     return reconhecido;
 }
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;
                }
        }
 public Closing(short[,] se)
 {
     this.errosion   = new Erosion();
     this.dilatation = new Dilatation();
     this.errosion   = new Erosion(se);
     this.dilatation = new Dilatation(se);
 }
        /// <summary>
        /// Aforge median dilatation morfology with default kernel
        /// </summary>
        public static Image Dilatation(this Image image)
        {
            // default kernel
            var se = new short[,] {  {1, 1, 1},
                                     {1, 1, 1},
                                     {1, 1, 1}  };

            Dilatation dilatationFilter = new Dilatation(se);
            return dilatationFilter.Apply(BitmapGrayscale(image));
        }
Beispiel #5
0
        /// <summary>
        /// Celem metody jest przykadrowanie otrzymanego obrazka, tak aby widok zawierał jedynie kartkę papieru, oraz zwrócenie go do metody UploadFile
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public System.Drawing.Image CropImage(System.Drawing.Image img)
        {
            //DocumentSkewChecker skewChecker = new DocumentSkewChecker();
            //double angle = skewChecker.GetSkewAngle(image);
            //RotateBilinear rotationFilter = new RotateBilinear(-angle);
            //rotationFilter.FillColor = Color.White;
            //Bitmap rotatedImage = rotationFilter.Apply(image);

            Bitmap image = new Bitmap(img);

            UnmanagedImage grayImage = null;

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

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

            OtsuThreshold thresholdFilter = new OtsuThreshold();
            thresholdFilter.ApplyInPlace(edgesImage);

            Dilatation DilatationFilter = new Dilatation();
            DilatationFilter.Apply(edgesImage);

            Opening OpeningFilter = new Opening();
            OpeningFilter.Apply(edgesImage);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinHeight = 32;
            blobCounter.MinWidth = 32;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

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

            ExtractBiggestBlob BiggestBlob = new ExtractBiggestBlob();
            Bitmap biggestBlobsImage = BiggestBlob.Apply(edgesImage.ToManagedImage());
            AForge.IntPoint BiggestBlogCorners = BiggestBlob.BlobPosition;

            Crop cropFilter = new Crop(new Rectangle(BiggestBlogCorners.X, BiggestBlogCorners.Y, biggestBlobsImage.Width, biggestBlobsImage.Height));
            Bitmap croppedImage = cropFilter.Apply(image);
            return croppedImage;
        }
Beispiel #6
0
        public Bitmap process(Bitmap bmp)
        {
            Bitmap newImage = (Bitmap)bmp.Clone();

            newImage = new AForge.Imaging.Filters.GrayscaleY().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Threshold(50).Apply(newImage);
            newImage = new AForge.Imaging.Filters.Median().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Median().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Dilatation().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Median().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Erosion().Apply(newImage);
            newImage = new AForge.Imaging.Filters.Erosion().Apply(newImage);

            return(newImage);
        }
Beispiel #7
0
        /// <summary>
        /// Filter the image froma HSV colour range
        /// </summary>
        /// <param name="image">Bitmap Image, any suitable image</param>
        /// <returns>Bitmap Image</returns>
        public Bitmap addFilters(Bitmap image)
        {
			//Instantiate new HSL filter 
            AForge.Imaging.Filters.HSLFiltering hslFilter = new AForge.Imaging.Filters.HSLFiltering();
			
			//Define HSL colour range (default colour is orange)
            hslFilter.Hue = new AForge.IntRange(0, 50);
            hslFilter.Saturation = new AForge.DoubleRange(0.7, 1);
            hslFilter.Luminance = new AForge.DoubleRange(0.2, 0.7);

	    //Instantiate new RGB filter 
            /*AForge.Imaging.Filters.ColorFiltering rgbFilter = new AForge.Imaging.Filters.ColorFiltering();
			
	    //Define RGB colour range (default colour is orange)
            rgbFilter.Red = new AForge.IntRange(60, 180);
            rgbFilter.Green = new AForge.IntRange(10, 80);
            rgbFilter.Blue = new AForge.IntRange(5, 25);*/

	    //Apply the HSL filter to the input image
            hslFilter.ApplyInPlace(image);
			
	    //Create a 5 by 5 matrix using a short array
            int rows = 5;
            int cols = 5;
            short[,] Matrix = new short[rows, cols];

            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    Matrix[i, j] = 1;
			
			//Instantiate new dilate filter with the 5 by 5 matrix
			AForge.Imaging.Filters.Dilatation dilate = new AForge.Imaging.Filters.Dilatation(Matrix);
			//Apply the filter
			dilate.ApplyInPlace(image);

            return image;
        }
        public void Dilatate()
        {
            var se = new short[,] {  {1, 1, 1},
                                     {1, 1, 1},
                                     {1, 1, 1}  };

            Grayscale grayScaler = new Grayscale(0.2125, 0.7154, 0.0721);
            Dilatation dilatationFilter = new Dilatation(se);
            _digit = dilatationFilter.Apply(grayScaler.Apply(new Bitmap(_digit)));
        }
Beispiel #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Opening"/> class.
 /// </summary>
 /// 
 /// <param name="se">Structuring element.</param>
 /// 
 /// <remarks><para>See documentation to <see cref="Erosion"/> and <see cref="Dilatation"/>
 /// classes for information about structuring element constraints.</para></remarks>
 /// 
 public Opening( short[,] se )
 {
     errosion   = new Erosion( se );
     dilatation = new Dilatation( se );
 }
Beispiel #10
0
        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 #11
0
        // 선 가늘게
        public static Bitmap thin(Bitmap source)
        {
            ///////////// ini 객체 생성 시작 /////////////////////////////////////////////////////
            //현재 프로그램이 실행되고 있는정보 가져오기: 디버깅 모드라면 bin/debug/프로그램명.exe
            FileInfo exefileinfo = new FileInfo(@"C:\Program Files\PLOCR\PLOCR.exe");
            string pathini = exefileinfo.Directory.FullName.ToString();  //프로그램 실행되고 있는데 path 가져오기
            string fileName = @"\PLOCRconfig.ini";  // 환경설정 파일명
            string filePath = pathini + fileName;   //ini 파일 경로
            PLOCR.IniUtil ini = new PLOCR.IniUtil(filePath);   // 만들어 놓았던 iniUtil 객체 생성(생성자 인자로 파일경로 정보 넘겨줌)
            //////////// ini 객체 생성 끝 /////////////////////////////////////////////////////////

            int order = int.Parse(ini.GetIniValue("선굵기값", "가늘게횟수"));

            for (int i = 0; i < order; i++)
            {
                Bitmap tmp = (Bitmap)source;        // 중요! 한번 이미지 처리가 끝난 비트맵 source 는 clone 함수로 보내기 전에 다시 한번 (Bitmap) 처리 해줘야함, 이유는 잘 모르겠음
                // convert to 24 bits per pixel
                source = ImageProcess.Clone(tmp, PixelFormat.Format24bppRgb);
                // delete old image
                tmp.Dispose();

                Dilatation filter = new Dilatation();
                filter.ApplyInPlace(source);
            }

            return source;
        }
 public static Bitmap ApplyDilatation(Bitmap pBitmap)
 {
     Dilatation filter = new Dilatation();
     return filter.Apply(pBitmap);
 }
 public Closing()
 {
     this.errosion   = new Erosion();
     this.dilatation = new Dilatation();
 }
Beispiel #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Opening"/> class.
 /// </summary>
 ///
 /// <param name="se">Structuring element.</param>
 ///
 /// <remarks><para>See documentation to <see cref="Erosion"/> and <see cref="Dilatation"/>
 /// classes for information about structuring element constraints.</para></remarks>
 ///
 public Opening(short[,] se)
 {
     errosion   = new Erosion(se);
     dilatation = new Dilatation(se);
 }
Beispiel #15
0
        // 선 가늘게
        private void button36_Click(object sender, EventArgs e)
        {
            ///////////// ini 객체 생성 시작 /////////////////////////////////////////////////////
            //현재 프로그램이 실행되고 있는정보 가져오기: 디버깅 모드라면 bin/debug/프로그램명.exe
            FileInfo exefileinfo = new FileInfo(@"C:\Program Files\PLOCR\PLOCR.exe");
            string pathini = exefileinfo.Directory.FullName.ToString();  //프로그램 실행되고 있는데 path 가져오기
            string fileName = @"\PLOCRconfig.ini";  // 환경설정 파일명
            string filePath = pathini + fileName;   //ini 파일 경로
            DocumentAnalysis.IniUtil ini = new DocumentAnalysis.IniUtil(filePath);   // 만들어 놓았던 iniUtil 객체 생성(생성자 인자로 파일경로 정보 넘겨줌)
            //////////// ini 객체 생성 끝 /////////////////////////////////////////////////////////

            Bitmap source = Global.source;
            Bitmap tmp = source;
            // convert to 24 bits per pixel
            source = imageProcess.Clone(tmp, PixelFormat.Format24bppRgb);
            // delete old image
            tmp.Dispose();

            Dilatation filter = new Dilatation();
            filter.ApplyInPlace(source);

            Global.source = source;
            pictureBox1.Image = Global.source;
            pictureBox1.Invalidate();

            textBox25.Text = (int.Parse(textBox25.Text) + 1).ToString();
            ini.SetIniValue("선굵기값", "가늘게횟수", textBox25.Text.ToString());

            //string path = calculator.CreateFileCheck("C:\\Program Files\\PLOCR\\prescription.png");
            //pictureBox1.Image.Save(path);
        }