//główne operacje na obrazie public void ocr() { //otworzenie pliku FileStream srcstream = new FileStream(pic_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //stworzenie bitmapy Bitmap source = new Bitmap(srcstream); //zmiana ustawień webform Panel1.Visible = false; Image1.Dispose(); Label2.Text = "Processing..."; Panel3.Visible = true; //Preperation code Bitmap ext = source; //AForge.Imaging.Filters. //Przekształcenie obrazu na skalę odcieni szarości - testować dla obrazów o różnej kolorystyce(opracować system wyznaczania parametrów filtru na podstawie RGB zdjęcia) AForge.Imaging.Filters.Grayscale grScl = new AForge.Imaging.Filters.Grayscale(0.2125, 0.0154, 0.0721 ); source = grScl.Apply(source); //Zwiększenie kontrastu AForge.Imaging.Filters.ContrastStretch conCor = new AForge.Imaging.Filters.ContrastStretch(); source = conCor.Apply(source); //Wyostrzenie AForge.Imaging.Filters.Sharpen shp = new AForge.Imaging.Filters.Sharpen(); source = shp.Apply(source); //Segmentation code bool procesed = false; // Image2.Width = 350; // Image2.Height = (int)((source.Height * 200) / source.Width); try { Emgu.CV.Image<Bgr, Byte> to_rec = new Emgu.CV.Image<Bgr, byte>(source); Do_ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_DEFAULT); try { Do_ocr.Recognize<Bgr>(to_rec); //recognizedText.Text = ocr.GetText(); PastOCRBox.Text = Do_ocr.GetText(); // StatusBox.Text = "Finished! Ready for next one..."; Do_ocr.Dispose(); to_rec.Dispose(); } catch (Exception exp) { Label2.Text = "Recognition error! " + exp.Message; Do_ocr.Dispose(); return; } } catch (Exception exp) { Label2.Text = "OCR engine failed! " + exp.Message; return; } //czyszczenie z plików tymczasowych // source.Save("D:\\test.bmp"); // ext.Save("D:\\testcor.bmp"); source.Dispose(); srcstream.Close(); srcstream.Dispose(); //System.IO.File.Delete(pic_file); System.IO.File.Delete(Server.MapPath("~/img/prev.bmp")); System.IO.File.Delete(Server.MapPath("~/img/tmp.bmp")); //przygotować wygląd strony po rozpoznawaniu Panel3.Visible = false; Label1.Visible = false; Panel0.Visible = false; Panel5.Visible = false; Panel4.Visible = true; }
public Bitmap Process(Bitmap image) { Bitmap filteredImage = convertFormatTo32(image); if (filterLevel >= 0) { filteredImage = resize(filteredImage, ImageSize.Width, ImageSize.Height); } if (filterLevel >= 1) { if (ContrastStretch) { AForge.Imaging.Filters.ContrastStretch stretcher = new AForge.Imaging.Filters.ContrastStretch(); filteredImage = stretcher.Apply(filteredImage); } if (Histogram) { AForge.Imaging.Filters.HistogramEqualization histogrammer = new AForge.Imaging.Filters.HistogramEqualization(); filteredImage = histogrammer.Apply(filteredImage); } if (Gaussian) { AForge.Imaging.Filters.GaussianBlur blurrer = new AForge.Imaging.Filters.GaussianBlur(); blurrer.Size = (int)GaussianStrength; filteredImage = blurrer.Apply(filteredImage); } if (ContrastAdjustment) { AForge.Imaging.Filters.ContrastCorrection contraster = new AForge.Imaging.Filters.ContrastCorrection(); contraster.Factor = (float)ContrastStrength; filteredImage = contraster.Apply(filteredImage); } if (Greyscale) { filteredImage = AForge.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(filteredImage); //Greyscale downgrades format // filteredImage = convertFormatTo32(filteredImage.InternalBitmap); } if (Threshold) { //filteredImage.InternalBitmap = convertFormatToGS(filteredImage.InternalBitmap); AForge.Imaging.Filters.Threshold thresholder = new AForge.Imaging.Filters.Threshold(); thresholder.ThresholdValue = (int)(((double)ThresholdStrength / 10.0) * 255.0); filteredImage = thresholder.Apply(filteredImage); } if (Bradley) { AForge.Imaging.Filters.BradleyLocalThresholding bradlifier = new AForge.Imaging.Filters.BradleyLocalThresholding(); filteredImage = bradlifier.Apply(filteredImage); } } return filteredImage; }