private void niblackToolStripMenuItem_Click(object sender, EventArgs e) { Image <Gray, Byte> temp = Binarization.Niblack(this.picture); picture = temp; imageBox.Image = picture; }
private PreProcessor Configure(Ocr ocr) { ocr.License = license; ocr.TempFolder = Path.Combine(tempPath, Guid.NewGuid().ToString()); ocr.Language = SupportedLanguages.English; ocr.EnableDebugOutput = true; ocr.GetAdvancedOCRData = true; ocr.LogToFile = true; //ocr.HandleExceptionsInternally = false; PreProcessor preProcessor = new PreProcessor(); preProcessor.Deskew = true; preProcessor.Autorotate = false; preProcessor.KeepOriginalImage = true; //Create new binarization object Binarization binarize = new Binarization(); //Enable binarization binarize.Binarize = true; //Assign it to PreProcessor preProcessor.Binarization = binarize; return(preProcessor); //ocr.Recognize(preProcessor); }
public WriteableBitmap GetImage(string _strImgName) { WriteableBitmap bitmap = null; switch (m_strCurImgName) { case ComInfo.IMG_NAME_EDGE_DETECTION: EdgeDetection edge = (EdgeDetection)m_imgProc; if (edge != null) { bitmap = edge.WriteableBitmap; } break; case ComInfo.IMG_NAME_GRAY_SCALE: GrayScale gray = (GrayScale)m_imgProc; if (gray != null) { bitmap = gray.WriteableBitmap; } break; case ComInfo.IMG_NAME_BINARIZATION: Binarization binarization = (Binarization)m_imgProc; if (binarization != null) { bitmap = binarization.WriteableBitmap; } break; case ComInfo.IMG_NAME_GRAY_SCALE_2DIFF: GrayScale2Diff gray2Diff = (GrayScale2Diff)m_imgProc; if (gray2Diff != null) { bitmap = gray2Diff.WriteableBitmap; } break; case ComInfo.IMG_NAME_COLOR_REVERSAL: ColorReversal colorReversal = (ColorReversal)m_imgProc; if (colorReversal != null) { bitmap = colorReversal.WriteableBitmap; } break; case ComInfo.IMG_NAME_GRAY_SCALE_DIFF: GrayScaleDiff grayDiff = (GrayScaleDiff)m_imgProc; if (grayDiff != null) { bitmap = grayDiff.WriteableBitmap; } break; default: break; } return(bitmap); }
private void TreshloldButtonOkClick(object sender, RoutedEventArgs e) { Close(); MainWindow.ModifiedImgSingleton.Source = Binarization.ManualBinarisation((BitmapSource)MainWindow.ModifiedImgSingleton.Source, (int)ThesholdSlider.Value); }
private void PreviewImageUpdate(int treshlold) { try { PreviewImage.Source = Binarization.ManualBinarisation((BitmapSource)MainWindow.ModifiedImgSingleton.Source, treshlold); } catch (Exception e) { Debug.WriteLine(e.StackTrace); } }
private void Binarisation_LocalTresholdNiblack(object sender, RoutedEventArgs e) { if (Binarization.IsImageColoured((BitmapSource)ModifiedImgSingleton.Source)) { MessageBox.Show("Obraz musi być czarno-biały", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error); return; } var niblack = new Niblack(); niblack.Show(); }
private void Binarisation_OwnTreshold(object sender, RoutedEventArgs e) { if (Binarization.IsImageColoured((BitmapSource)ModifiedImgSingleton.Source)) { MessageBox.Show("Obraz musi być czarno-biały", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error); return; } var tresholdWindow = new BinarisationOwnTreshold(); tresholdWindow.Show(); }
/// <summary> /// Threshold adjustment /// </summary> private void trackBarThreshold_Scroll(object sender, EventArgs e) { TrackBar ctrl = (TrackBar)sender; toolTipValue.SetToolTip(ctrl, ctrl.Value.ToString()); if (picOutput.Image != null) { picOutput.Image.Dispose(); } picOutput.Image = Binarization.Threshold((Bitmap)picInput.Image, _rect, trackBarThreshold.Value); }
private void Binarisation_AutomaticThesholdOtsu(object sender, RoutedEventArgs e) { if (Binarization.IsImageColoured((BitmapSource)ModifiedImgSingleton.Source)) { MessageBox.Show("Obraz musi być czarno-biały", "Błąd", MessageBoxButton.OK, MessageBoxImage.Error); return; } var threshold = Binarization.GetWariancjaMiędzyklasowa((BitmapSource)ModifiedImgSingleton.Source); ModifiedImgSingleton.Source = Binarization.ManualBinarisation((BitmapSource)ModifiedImgSingleton.Source, threshold); }
/// <summary> /// Search region of mokkans in image /// </summary> public static unsafe Rectangle SearchMokkanRegion(Bitmap image, Rectangle rect) { Bitmap bin = Binarization.Threshold(image, rect, MkaDefine.THRESHOLD); BitmapData bmd = bin.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, bin.PixelFormat); // calculate projection profiles int wid = image.Width; int hei = image.Height; int stride = bmd.Stride; int[] hProj = new int[wid]; int[] vProj = new int[hei]; int left = rect.Left; int right = rect.Right; int top = rect.Top; int bottom = rect.Bottom; // search vertical range byte *ptr = (byte *)bmd.Scan0.ToPointer(); Histogram.VerHistB(ptr, bmd.Stride, rect, vProj); SearchMokkanRegion(vProj, ref top, ref bottom); // remove upper part ptr = (byte *)bmd.Scan0.ToPointer(); for (int y = 0; y <= top; y++) { SystemTools.SetUnmanagedMemory(ptr, 255, wid); ptr += wid; } // remove lower part ptr = (byte *)bmd.Scan0.ToPointer() + (bottom - 1) * stride; for (int y = 0; y < hei - bottom; y++) { SystemTools.SetUnmanagedMemory(ptr, 255, wid); ptr += wid; } // search horizontal range ptr = (byte *)bmd.Scan0.ToPointer(); Histogram.HorHistB(ptr, bmd.Stride, rect, hProj); SearchMokkanRegion(hProj, ref left, ref right); bin.UnlockBits(bmd); bin.Dispose(); return(Rectangle.FromLTRB(left, top, right, bottom)); }
public bool SelectGoImgProc(ComImgInfo _comImgInfo, CancellationToken _token) { bool bRst = true; switch (_comImgInfo.CurImgName) { case ComInfo.IMG_NAME_EDGE_DETECTION: EdgeDetection edge = (EdgeDetection)m_imgProc; bRst = edge.GoImgProc(_token); break; case ComInfo.IMG_NAME_GRAY_SCALE: GrayScale gray = (GrayScale)m_imgProc; bRst = gray.GoImgProc(_token); break; case ComInfo.IMG_NAME_BINARIZATION: Binarization binarization = (Binarization)m_imgProc; binarization.Thresh = _comImgInfo.BinarizationInfo.Thresh; bRst = binarization.GoImgProc(_token); break; case ComInfo.IMG_NAME_GRAY_SCALE_2DIFF: GrayScale2Diff gray2Diff = (GrayScale2Diff)m_imgProc; bRst = gray2Diff.GoImgProc(_token); break; case ComInfo.IMG_NAME_COLOR_REVERSAL: ColorReversal colorReversal = (ColorReversal)m_imgProc; bRst = colorReversal.GoImgProc(_token); break; case ComInfo.IMG_NAME_GRAY_SCALE_DIFF: GrayScaleDiff grayDiff = (GrayScaleDiff)m_imgProc; bRst = grayDiff.GoImgProc(_token); break; default: break; } return(bRst); }
private void Execute() { async Task InitializeLP() //initialize methods that use Get/Set Pixel { MeasureTime measureLP = new MeasureTime(); Bitmaps.BinarizeLPImage = BitmapConversion.CreateNonIndexedImage(new Bitmap(Bitmaps.Filepath)); Bitmaps.BinarizeLPImageView = await Task.Run(() => Binarization.LowPerformance(new Bitmap(Bitmaps.Filepath), Bitmaps.BinarizeLPImage, measureLP)); Bitmaps.KMMLP = await Task.Run(() => KMMLowPerformanceMain.Init(Bitmaps.BinarizeLPImage, measureLP)); Bitmaps.TimeElapsedLP = measureLP.TimeElapsedMs; } async Task InitializeHP() //initialize methods with lockbits, marshall copy { MeasureTime measureHP = new MeasureTime(); Bitmaps.BinarizeHPImage = await Task.Run(() => Binarization.HighPerformance(new Bitmap(Bitmaps.Filepath), measureHP)); Bitmaps.BinarizeHPImageView = BitmapConversion.Bitmap2BitmapImage(Bitmaps.BinarizeHPImage); Bitmaps.KMMHP = await Task.Run(() => BitmapConversion.Bitmap2BitmapImage(KMMHighPerformanceMain.Init(Bitmaps.BinarizeHPImage, measureHP))); Bitmaps.TimeElapsedHP = measureHP.TimeElapsedMs; Bitmaps.TimeElapsedHPTicks = measureHP.TimeElapsedTicks; } try { var task1 = Task.Run(() => InitializeLP()); var task2 = Task.Run(() => InitializeHP()); Task.WaitAll(task1, task2); } catch (Exception ex) { if (ex is ArgumentNullException || ex is AggregateException) { System.Windows.MessageBox.Show("There is no image to Apply KMM"); } } }
public override bool EnterParameters() { ProcessingAlgorithm algorithm; switch (Program.paramState.algorithmOption) { case "1": algorithm = new MeanFilterImplementation(); algorithm.PreProcessingEvent.Subscribe(new MeanPreProcessingCommand()); algorithm.PostProcessingEvent.Subscribe(new MeanPostProcessingCommand()); algorithm.ProcessingStepEvent.Subscribe(new MeanProcessingStepCommand()); break; case "2": algorithm = new Binarization(); algorithm.PreProcessingEvent.Subscribe(new BinarizationPreProccesingCommand()); algorithm.PostProcessingEvent.Subscribe(new BinarizationPostProccesingCommand()); algorithm.ProcessingStepEvent.Subscribe(new BinarizationProccessingStepCommand()); break; case "3": algorithm = new Inverse(); algorithm.PreProcessingEvent.Subscribe(new InversePreProcessingCommand()); algorithm.PostProcessingEvent.Subscribe(new InversePostProcessingCommand()); break; default: return(false); } var parameters = new Dictionary <String, String>(); foreach (var parameter in algorithm.ExpectedParameters) { Console.Write($"{parameter} = "); var value = Console.ReadLine(); parameters.Add(parameter, value); } Program.builder.WriteImage(algorithm.Process(Program.image, parameters), "result.png"); Program.SetMachineState(Program.unloadedState); return(true); }
private async void NiblackOnClick(object sender, RoutedEventArgs e) { try { _size = Convert.ToInt32(WindowSize.Text); _parameter = Convert.ToDouble(TresholdingParameter.Text, CultureInfo.InvariantCulture); } catch (Exception exception) { Debug.WriteLine(exception.StackTrace + " " + exception.Message); } WindowSize.IsEnabled = false; TresholdingParameter.IsEnabled = false; DoNiblackButton.IsEnabled = false; var b = new Binarization(); MainWindow.ModifiedImgSingleton.Source = await b.PerformNiblack(_parameter, _size, _progress); Close(); }
public void Apply(string inputPath, string outputPath, string intermediatePath, RGBThreshold thd, int tnum) { try { if (thd.upperBlueColorThd == thd.lowerBlueColorThd || thd.upperGreenColorThd == thd.lowerGreenColorThd || thd.upperRedColorThd == thd.lowerRedColorThd) { Binarization.ApplyBradleyLocalThresholding(inputPath, outputPath); } else { Binarization.ApplyRGBColorThresholding(inputPath, outputPath, thd, tnum); } } catch (Exception e) { Log.WriteLine("ExtractTextLayerFromMapWorker:" + e.Message); Log.WriteLine(e.Source); Log.WriteLine(e.StackTrace); throw; } }
public Bitmap SelectGetBitmap(string _strImgName) { Bitmap bitmap = null; switch (_strImgName) { case ComInfo.IMG_NAME_EDGE_DETECTION: EdgeDetection edge = (EdgeDetection)m_imgProc; bitmap = edge.BitmapAfter; break; case ComInfo.IMG_NAME_GRAY_SCALE: GrayScale gray = (GrayScale)m_imgProc; bitmap = gray.BitmapAfter; break; case ComInfo.IMG_NAME_BINARIZATION: Binarization binarization = (Binarization)m_imgProc; bitmap = binarization.BitmapAfter; break; case ComInfo.IMG_NAME_GRAY_SCALE_2DIFF: GrayScale2Diff gray2Diff = (GrayScale2Diff)m_imgProc; bitmap = gray2Diff.BitmapAfter; break; case ComInfo.IMG_NAME_COLOR_REVERSAL: ColorReversal colorReversal = (ColorReversal)m_imgProc; bitmap = colorReversal.BitmapAfter; break; default: break; } return(bitmap); }
private void TurnImageBlackWhiteOnClick(object sender, RoutedEventArgs e) { ModifiedImgSingleton.Source = Binarization.TurnImageBlackWhite((BitmapSource)ModifiedImgSingleton.Source); HistogramTools.CalculateHistograms(); }
/// <summary> /// Binarization method selection /// </summary> private void rdbBinarization_CheckedChanged(object sender, EventArgs e) { _changed = !_changed; if (_changed) { return; } lblThreshold.Enabled = false; trackBarThreshold.Enabled = false; if (picOutput.Image != null) { picOutput.Image.Dispose(); } if (rdbBinDA.Checked) { picOutput.Image = TakakuraImplement.DiscriminantAnalysis((Bitmap)picInput.Image, _rect); } else if (rdbBinSIS.Checked) { picOutput.Image = Binarization.SISThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinOtsu.Checked) { picOutput.Image = Binarization.OtsuThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinLi.Checked) { picOutput.Image = Binarization.LiThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinHuang.Checked) { picOutput.Image = Binarization.HuangThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinYen.Checked) { picOutput.Image = Binarization.YenThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinMean.Checked) { picOutput.Image = Binarization.MeanThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinPercentile.Checked) { picOutput.Image = Binarization.PercentileThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinIterative.Checked) { picOutput.Image = Binarization.IteractiveThreshold((Bitmap)picInput.Image, _rect, 0, 180); } else if (rdbBinMoments.Checked) { picOutput.Image = Binarization.MomentsThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinIsoData.Checked) { picOutput.Image = Binarization.IsoDataThreshold((Bitmap)picInput.Image, _rect); } else if (rdbBinAdjust.Checked) { lblThreshold.Enabled = true; trackBarThreshold.Enabled = true; picOutput.Image = Binarization.Threshold((Bitmap)picInput.Image, _rect, trackBarThreshold.Value); } else if (!_leave && picInput.Image != null) { picOutput.Image = (Bitmap)picInput.Image.Clone(); } }
/// <summary> /// Binarization by Disriminant Analysis Method /// </summary> /// <param name="image"></param> /// <param name="rect"></param> /// <returns></returns> public static unsafe Bitmap DiscriminantAnalysis(Bitmap image, Rectangle rect) { Bitmap dest = Binarization.Grayscale(image, rect); BitmapData bmd = dest.LockBits(rect, ImageLockMode.ReadWrite, dest.PixelFormat); int thresholdValue = 0; // get start and stop X-Y coordinates int startX = rect.Left; int startY = rect.Top; int stopX = startX + rect.Width; int stopY = startY + rect.Height; int offset = bmd.Stride - rect.Width; // histogram array int[] hist = new int[256]; int t, max_t, t_limit; int i, n1, n2, count, sum; long tmp; double ave; double ave1, ave2, var1, var2, max; double var_w, var_b, r; max_t = 0; t_limit = 255; // collect histogram first byte *ptr = (byte *)bmd.Scan0.ToPointer(); // align pointer to the first pixel to process ptr += (startY * bmd.Stride + startX); // calculate histogram sum = count = 0; // for each line for (int y = startY; y < stopY; y++) { // for each pixel for (int x = startX; x < stopX; x++, ptr++) { hist[*ptr]++; sum += *ptr; count++; } ptr += offset; } ave = sum / count; max = -1.0; for (t = 0; t < t_limit; t++) { n1 = n2 = 0; ave1 = ave2 = 0; var1 = var2 = 0; tmp = 0; for (i = 0; i < t; i++) { n1 = n1 + hist[i]; tmp = tmp + hist[i] * i; } // Average of class 1 if (n1 != 0) { ave1 = (double)tmp / (double)n1; } for (i = 0; i < t; i++) { var1 = var1 + (i - ave1) * (i - ave1) * hist[i]; } // Variance of class 1 if (n1 != 0) { var1 = var1 / (double)n1; } tmp = 0; for (i = t; i < t_limit; i++) { n2 = n2 + hist[i]; tmp = tmp + hist[i] * i; } // Average of class 2 if (n2 != 0) { ave2 = (double)tmp / (double)n2; } for (i = t; i < t_limit; i++) { var2 = var2 + (i - ave2) * (i - ave2) * hist[i]; } // Variance of class 2 if (n2 != 0) { var2 = var2 / (double)n2; } var_w = (n1 * var1 + n2 * var2); if (var_w == 0) { var_w = 1; } var_b = n1 * (ave1 - ave) * (ave1 - ave) + n2 * (ave2 - ave) * (ave2 - ave); r = var_b / var_w; if (r > max) { max = r; max_t = t; } } if (max_t < 0 || max_t > 255) { return(dest); } else { thresholdValue = max_t; } // do the job ptr = (byte *)bmd.Scan0; // align pointer to the first pixel to process ptr += (startY * bmd.Stride + startX); // for each line for (int y = startY; y < stopY; y++) { // for each pixel for (int x = startX; x < stopX; x++, ptr++) { *ptr = (byte)((*ptr >= thresholdValue) ? 255 : 0); } ptr += offset; } dest.UnlockBits(bmd); return(dest); }
/// <summary> /// Change thresholding method /// </summary> private void ChangeThreshold() { if (_init) { return; } this.Cursor = Cursors.WaitCursor; if (_binBmp != null) { _binBmp.Dispose(); } if (rdbHSL.Checked) { _binBmp = MokkanExtraction.HSLFilter(_oriBmp, _bndRec, _hue, _sat, _lum); } else if (rdbYCbCr.Checked) { _binBmp = MokkanExtraction.YCbCrFilter(_oriBmp, _bndRec, _y, _cb, _cr); } else { Binarization.ExtractBackground = true; switch (cmbThresholding.SelectedItem.ToString()) { case "MaxEntropy法": _binBmp = Binarization.MaxEntropyThreshold(_bndBmp, _bndRec); break; case "Otsu法": _binBmp = Binarization.OtsuThreshold(_bndBmp, _bndRec); break; case "SIS法": _binBmp = Binarization.SISThreshold(_bndBmp, _bndRec); break; case "Huang法": _binBmp = Binarization.HuangThreshold(_bndBmp, _bndRec); break; case "Yen法": _binBmp = Binarization.YenThreshold(_bndBmp, _bndRec); break; case "Li法": _binBmp = Binarization.LiThreshold(_bndBmp, _bndRec); break; case "Mean法": _binBmp = Binarization.MeanThreshold(_bndBmp, _bndRec); break; case "Moment法": _binBmp = Binarization.MomentsThreshold(_bndBmp, _bndRec); break; case "Iterative法": _binBmp = Binarization.IteractiveThreshold(_bndBmp, _bndRec, 0, 128); break; case "IsoData法": _binBmp = Binarization.IsoDataThreshold(_bndBmp, _bndRec); break; case "Percentile法": _binBmp = Binarization.PercentileThreshold(_bndBmp, _bndRec); break; case "閾値設定": grbThreshold.Enabled = true; _binBmp = Binarization.Threshold(_bndBmp, _bndRec, (int)nupThreshold.Value); break; } Binarization.ExtractBackground = false; } ChangeExtraction(); this.Cursor = Cursors.Default; }