private void ProcessImage() { try { if (bitmap == null) { throw new Exception("Chưa chọn ảnh nguồn"); } var opt = new SegmentOptions { ThreshHold = double.Parse(editNguong.EditValue.ToString()), MinSize = int.Parse(editMinSize.EditValue.ToString()) }; if (opt.MinSize < 0) { throw new Exception("Minsize phải >= 0"); } if (opt.ThreshHold < 0 || opt.ThreshHold > 50) { throw new Exception("Threshold phải lớn hơn hoặc bằng 0 và nhỏ hơn hoặc bằng 50"); } var newBm = bitmap.Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), PixelFormat.Format24bppRgb); newBm = new AForge.Imaging.Filters.Sharpen().Apply(newBm); newBm = new GraphImageSegmentation(this, opt).Apply(newBm); picResult.Image = newBm; GC.Collect(); } catch (Exception exc) { MessageBox.Show(this, exc.Message, "Có lỗi xảy ra", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void Sharpen() { // create filter AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen(); // apply the filter filter.ApplyInPlace(image); }
private void Go_Click(object sender, RoutedEventArgs e) { try { System.Windows.Media.Imaging.BitmapImage imagesource = imageBox.Source as System.Windows.Media.Imaging.BitmapImage; if (imageBox.Source == null) { System.Windows.MessageBox.Show("Add your image to the Image 1 box!"); return; } System.Drawing.Bitmap image = BmpImage2Bmp(imagesource); System.Windows.Media.Imaging.BitmapImage final = new System.Windows.Media.Imaging.BitmapImage(); if (!AForge.Imaging.Image.IsGrayscale(image)) { AForge.Imaging.Filters.ExtractChannel Grayer = new AForge.Imaging.Filters.ExtractChannel(0); image = Grayer.Apply(image); } if (Threshold.IsChecked == true) { imageBox.Source = null; AForge.Imaging.Filters.Threshold threshold = new AForge.Imaging.Filters.Threshold((int)slider.Value); threshold.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (GaussianFilter.IsChecked == true) { AForge.Imaging.Filters.GaussianBlur Gauss = new AForge.Imaging.Filters.GaussianBlur(); Gauss.Sigma = GaussSigma_Slide.Value; Gauss.Size = (int)GaussSize_Slide.Value; AForge.Imaging.UnmanagedImage unmanagedImage = AForge.Imaging.UnmanagedImage.FromManagedImage(image); AForge.Imaging.UnmanagedImage Dst = unmanagedImage.Clone(); Gauss.Apply(unmanagedImage, Dst); final = Bmp2BmpImage(Dst.ToManagedImage()); } else if (HiPass.IsChecked == true) { AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Erode.IsChecked == true) { AForge.Imaging.Filters.Erosion filter = new AForge.Imaging.Filters.Erosion(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Invert.IsChecked == true) { AForge.Imaging.Filters.Invert filter = new AForge.Imaging.Filters.Invert(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (EdgeDetector.IsChecked == true) { AForge.Imaging.Filters.CannyEdgeDetector filter = new AForge.Imaging.Filters.CannyEdgeDetector(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (Median.IsChecked == true) { AForge.Imaging.Filters.Median filter = new AForge.Imaging.Filters.Median(); filter.Size = (int)GaussSize_Slide.Value; filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } else if (More.IsChecked == true) { if (Dilate.IsSelected) { AForge.Imaging.Filters.Dilatation filter = new AForge.Imaging.Filters.Dilatation(); filter.ApplyInPlace(image); final = Bmp2BmpImage(image); } } imageBox.Source = final; TransformImage = image; boxWidth = imageBox.RenderSize.Width; boxHeight = imageBox.RenderSize.Height; } catch (Exception exc) { System.Windows.MessageBox.Show(exc.ToString()); } }
//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; }
private void btn_uygula_Click(object sender, EventArgs e) { if (comboBox1.SelectedIndex == 0) // Gri seçildi. { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap gri = griYap(image); pictureBox2.Image = gri; } if (comboBox1.SelectedIndex == 1) // Binary seçildi. { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap binary = binaryYap(image); pictureBox2.Image = binary; } if (comboBox1.SelectedIndex == 2) // Sobel Kenar seçildi. { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap sobel = sobelEdgeDetection(image); pictureBox2.Image = sobel; } if (comboBox1.SelectedIndex == 3) // Sobel Kenar seçildi. { Bitmap image = new Bitmap(pictureBox1.Image); Bitmap median = medianFilter(image); pictureBox2.Image = median; } if (comboBox1.SelectedIndex == 4) // Sepia Fitreleme seçildi. { // load an image System.Drawing.Bitmap image = new Bitmap(pictureBox1.Image); // create filter AForge.Imaging.Filters.Sepia filter = new AForge.Imaging.Filters.Sepia(); // apply filter System.Drawing.Bitmap newImage = filter.Apply(image); pictureBox2.Image = newImage; } if (comboBox1.SelectedIndex == 5) // Blur Fitreleme seçildi. { // load an image System.Drawing.Bitmap image = new Bitmap(pictureBox1.Image); // create filter AForge.Imaging.Filters.Blur filter = new AForge.Imaging.Filters.Blur(); // apply filter System.Drawing.Bitmap newImage = filter.Apply(image); pictureBox2.Image = newImage; } if (comboBox1.SelectedIndex == 6) // Keskinleştirme Fitreleme seçildi. { // load an image System.Drawing.Bitmap image = new Bitmap(pictureBox1.Image); // create filter AForge.Imaging.Filters.Sharpen filter = new AForge.Imaging.Filters.Sharpen(); // apply filter System.Drawing.Bitmap newImage = filter.Apply(image); pictureBox2.Image = newImage; } if (comboBox1.SelectedIndex == 7) // Hue Modifier Fitreleme seçildi. { // load an image System.Drawing.Bitmap image = new Bitmap(pictureBox1.Image); // create filter AForge.Imaging.Filters.HueModifier filter = new AForge.Imaging.Filters.HueModifier(); // apply filter System.Drawing.Bitmap newImage = filter.Apply(image); pictureBox2.Image = newImage; } }