private static void CreateTrainingData(OCR.ShapeNet ShapeNet, List <Example> Examples) { NeuralNetwork.LearningData oLearningData; OCR.PageComponent PageComponent; sNeuralInput newInput; foreach (Example Example in Examples) { PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(Example.Filename); ExtractFeatures.ExecuteExtractFeatures(PageComponent, true); PageComponent.Position.Ascent = ShapeNet.ShapeList[Example.ShapeId].Position.Ascent; PageComponent.Position.Height = ShapeNet.ShapeList[Example.ShapeId].Position.Height; PageComponent.Position.Center = ShapeNet.ShapeList[Example.ShapeId].Position.Center; PageComponent.Position.Base = ShapeNet.ShapeList[Example.ShapeId].Position.Base; PageComponent.Position.Descent = ShapeNet.ShapeList[Example.ShapeId].Position.Descent; //Fill the oLearningData = new NeuralNetwork.LearningData(); oLearningData.oInput.fInputs.Clear(); oLearningData.oOutput.fOutputs.Clear(); newInput = RecogniseComponent.CalculateNetworkInput(PageComponent); oLearningData.oInput = newInput; for (long lIndex2 = 0; lIndex2 < ShapeNet.ShapeList.Count; lIndex2++) { if (Example.ShapeId == lIndex2) { oLearningData.oOutput.fOutputs.Add(1); } else { oLearningData.oOutput.fOutputs.Add(0); } } ShapeNet.NeuralNetwork.AddSituation(oLearningData); } ShapeNet.NeuralNetwork.ComputeInputRatios(); }
private void RunThinning_Click(object sender, RoutedEventArgs e) { OCRStubControls.CharacterGrid[] CharacterGrids = new OCRStubControls.CharacterGrid[7]; OCR.PageImage PageImage; OCR.PageComponent PageComponent; //Initialize charactergrids CharacterGrids[0] = ThinningStep1; CharacterGrids[1] = ThinningStep2; CharacterGrids[2] = ThinningStep3; CharacterGrids[3] = ThinningStep4; CharacterGrids[4] = ThinningStep5; CharacterGrids[5] = ThinningStep6; CharacterGrids[6] = ThinningStep7; PageImage = new OCR.PageImage(); PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtThinningSample.Text); for (int x = 0; x < PageComponent.Width; x++) { for (int y = 0; y < PageComponent.Height; y++) { PageComponent.BinaryBytes[x, y] = PageComponent.Bytes[x, y]; } } ExtractFeatures.CreateCompareMatrixWithoutPruning(PageComponent); for (int x = 0; x < PageComponent.Width; x++) { for (int y = 0; y < PageComponent.Height; y++) { PageComponent.BinaryBytes[x, y] = (PageComponent.Bytes[x, y] == 0xFF ? (Byte)0xFF : (Byte)0x00); } } CharacterGrids[0].Bytes = new byte[32, 32]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { CharacterGrids[0].Bytes[x, y] = PageComponent.CompareMatrix[x, y]; } } CharacterGrids[0].InvalidateVisual(); for (int i = 0; i < 6; i++) { CharacterGrids[i + 1].Bytes = new byte[32, 32]; for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { CharacterGrids[i + 1].Bytes[x, y] = PageComponent.CompareMatrix[x, y]; } } switch (ThinningAlgorithm.SelectedIndex) { case 0: //Standard ExtractFeatures.Thinning(CharacterGrids[i + 1].Bytes, 0, 0xFF, i + 1); break; case 1: //Erode ExtractFeatures.ErodeThinning(CharacterGrids[i + 1].Bytes, 0, 0xFF, i + 1); break; case 2: //Middle ExtractFeatures.MiddleThinning(CharacterGrids[i + 1].Bytes, 0, 0xFF); break; case 3: //Condensed PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtThinningSample.Text); ExtractFeatures.CondensedThinning(PageImage, PageComponent, 0, 0xFF, i + 1); for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { CharacterGrids[i + 1].Bytes[x, y] = PageComponent.CompareMatrix[x, y]; } } break; case 4: //Pruning PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtThinningSample.Text); for (int x = 0; x < PageComponent.Width; x++) { for (int y = 0; y < PageComponent.Height; y++) { PageComponent.BinaryBytes[x, y] = PageComponent.Bytes[x, y]; } } ExtractFeatures.ThinningPruningOnOriginalImage(PageComponent, PageComponent, 0, i + 1); ExtractFeatures.CreateCompareMatrixWithoutPruning(PageComponent); for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { CharacterGrids[i + 1].Bytes[x, y] = PageComponent.CompareMatrix[x, y]; } } break; case 5: //Pruning original PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtThinningSample.Text); ExtractFeatures.ThinningPruningOnOriginalImage(PageImage, PageComponent, 2, i + 1); ExtractFeatures.CreateCompareMatrixWithoutPruning(PageComponent); for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { CharacterGrids[i + 1].Bytes[x, y] = PageComponent.CompareMatrix[x, y]; } } break; } CharacterGrids[i + 1].InvalidateVisual(); } }
private void btnTest_Click(object sender, RoutedEventArgs e) { OCR.ShapeNet SelectedShapeNet; OCR.PageComponent PageComponent; SelectedShapeNet = (OCR.ShapeNet)lstNeuralNetworks.SelectedItem; PageComponent.newID = 0; PageComponent = new OCR.PageComponent(); PageComponent.LoadBitmap(txtSample.Text); ExtractFeatures.ExecuteExtractFeatures(PageComponent, PageComponent, true); RecogniseComponent.Recognise(SelectedShapeNet, PageComponent); //Recognise the component lstResults.Items.Clear(); String ResultText = ""; foreach (RecognitionResult Result in PageComponent.RecognitionResults) { ResultText = Result.Content + " (" + Result.Probability + ")"; lstResults.Items.Add(ResultText); } //Build the original image PageComponent Original = new PageComponent(); Original.LoadBitmap(txtSample.Text); ExtractFeatures.CreateCompareMatrixWithoutPruning(Original, Original.Bytes); imgSample.Bytes = Original.CompareMatrix; for (int index = 0; index < 256; index++) { imgSample.GridBrushes[index] = new SolidColorBrush(System.Windows.Media.Color.FromRgb((byte)index, (byte)index, (byte)index)); } imgSample.InvalidateVisual(); ExtractFeatures.CreateCompareMatrixWithoutPruning(PageComponent); //Build the thinned and stroked image for (int x = 0; x < 32; x++) { for (int y = 0; y < 32; y++) { if (PageComponent.StrokeMatrix[x, y] == 0xFF && PageComponent.CompareMatrix[x, y] != 0xFF) { PageComponent.StrokeMatrix[x, y] = 0x04; } if (PageComponent.PixelTypeMatrix[x, y] == ePixelType.End) { PageComponent.StrokeMatrix[x, y] = 0xFE; } if (PageComponent.PixelTypeMatrix[x, y] == ePixelType.Junction) { PageComponent.StrokeMatrix[x, y] = 0xFD; } } } imgProjection.Bytes = PageComponent.StrokeMatrix; imgProjection.GridBrushes[0] = new SolidColorBrush(Colors.Black); imgProjection.GridBrushes[4] = new SolidColorBrush(Colors.LightGray); imgProjection.GridBrushes[11] = new SolidColorBrush(Colors.Brown); imgProjection.GridBrushes[12] = new SolidColorBrush(Colors.Maroon); imgProjection.GridBrushes[13] = new SolidColorBrush(Colors.Magenta); imgProjection.GridBrushes[14] = new SolidColorBrush(Colors.Lime); imgProjection.GridBrushes[15] = new SolidColorBrush(Colors.LightCyan); imgProjection.GridBrushes[16] = new SolidColorBrush(Colors.Purple); imgProjection.GridBrushes[32] = new SolidColorBrush(Colors.Blue); imgProjection.GridBrushes[48] = new SolidColorBrush(Colors.Green); imgProjection.GridBrushes[64] = new SolidColorBrush(Colors.Yellow); imgProjection.GridBrushes[253] = new SolidColorBrush(Colors.Red); imgProjection.GridBrushes[254] = new SolidColorBrush(Colors.Red); imgProjection.InvalidateVisual(); lblStrokes.Content = "#Strokes: " + Convert.ToString(PageComponent.Strokes); //Bitmap StrokeMatrixBitmap = OCR.DebugTrace.DebugTrace.CreateBitmapFromByteArray(PageComponent.StrokeMatrix, new System.Drawing.Size(32, 32)); //StrokeMatrixBitmap.Save("d:\\ocr\\temp.bmp"); //imgProjection.Source = new BitmapImage(new Uri("d:\\ocr\\temp.bmp")); pbHorizontal.Clear(); int ProjectionValue; for (int x = 0; x < 3; x++) { ProjectionValue = 0; for (int y = 0; y < 3; y++) { ProjectionValue += PageComponent.PixelTypeProjectionJunction[x, y]; ProjectionValue += PageComponent.PixelTypeProjectionEndpoint[x, y]; } pbHorizontal.AddValue(ProjectionValue); } pbVertical.Clear(); for (int y = 0; y < 3; y++) { ProjectionValue = 0; for (int x = 0; x < 3; x++) { ProjectionValue += PageComponent.PixelTypeProjectionJunction[x, y]; ProjectionValue += PageComponent.PixelTypeProjectionEndpoint[x, y]; } pbVertical.AddValue(ProjectionValue); } pbStrokeHorizontal.Clear(); foreach (int Value in PageComponent.lStrokeDirectionX) { pbStrokeHorizontal.AddValue(Value); } pbStrokeVertical.Clear(); foreach (int Value in PageComponent.lStrokeDirectionY) { pbStrokeVertical.AddValue(Value); } }