public void EventLayoutChanged() { if (PageFileSelected == null) { return; } bitmapSource = new Bitmap(PageFileSelected); if (bitmapSource == null) { return; } if ((bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) || (bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { bitmapSource = ToolsConvolution.ConvertTo24(bitmapSource); } PageLayout.Save(); //Rotate var bitmapRot = new RotateBilinear(PageLayout.Rotation.Value, true).Apply(bitmapSource); var linesH = ToolsFindLine.FindLinesH(bitmapRot.Width, bitmapRot.Height, PageLayout.LineSize.Value, PageLayout.LineOffset.Value, PageLayout.LineCount.Value); var linesV = ToolsFindLine.FindLinesV(bitmapRot.Width, bitmapRot.Height, PageLayout.ColSize.Value, PageLayout.ColOffset.Value, PageLayout.ColCount.Value); List <RenderLine> rl_list = new List <RenderLine>(); rl_list.AddRange(linesH); rl_list.AddRange(linesV); //var linesV = ToolsFindLine.FindLinesV(bitmapRot.Width, bitmapRot.Height, PageLayout.LineSize.Value, PageLayout.LineOffset.Value); ImageLayout = RenderBitmap(bitmapRot, rl_list); }
static void Main(string[] args) { Bitmap bitmap_0 = new Bitmap(@"D:\Projects\hisdocpro\HisDocPro\HisDocProUI\bin\Debug\tokens\t_0_c000.png"); //Bitmap bitmap_0 = new Bitmap(PageFileSelected); Bitmap bitmapTemp = new Grayscale(0.2125, 0.7154, 0.0721).Apply(bitmap_0); bitmapTemp = new Threshold(100).Apply(bitmapTemp); bitmapTemp = new Invert().Apply(bitmapTemp); double[,] image = ToolsConvolution.BitMapToDoubleArray(bitmapTemp, 0.5); double[,] kernel = ToolsConvolution.BitMapToDoubleArray(bitmapTemp, 0.5); Print(image); Print(kernel); //double[,] image = new double[6, 6]; //image[2, 1] = 1; //image[2, 2] = 1; //image[2, 3] = 1; //image[2, 4] = 1; //double[,] kernel = new double[3, 3]; //kernel[0, 0] = 1; //kernel[0, 1] = 1; //kernel[0, 2] = 1; //kernel[2, 1] = 1; //Print(image); //Print(kernel); //double[,] result = ToolsConvolution.CorrelationFFT(image, kernel); //Print(result); //Console.ReadLine(); double [,] result = ToolsConvolution.CorrelationFFT(image, kernel); Print(result); //List<string> page = ToolsPDF.Convert(@"D:\Projects\hisdocpro\page.pdf"); }
public ModelToken(IModelApplication application, string filePathImage, string filePathJson) { _application = application; _filePathImage = filePathImage; _filePathJson = filePathJson; _bitmapSource = new Bitmap(filePathImage); if ((_bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) || (_bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { _bitmapSource = ToolsConvolution.ConvertTo24(_bitmapSource); } Bitmap bitmapTemp = new Grayscale(1, 0, 0).Apply(_bitmapSource); bitmapTemp = new Erosion().Apply(bitmapTemp); bitmapTemp = new Threshold(160).Apply(bitmapTemp); bitmapTemp = new Invert().Apply(bitmapTemp); _kernel = ToolsConvolution.BitMapToDoubleArray(bitmapTemp, 0.5); _threshold = new ModelValueDouble(application.EventTokenChanged, 0); _weigth = new ModelValueDouble(application.EventTokenChanged, 0); ModelRendererBitmapSource renderer = new ModelRendererBitmapSource(_bitmapSource); renderer.Render(); Image = renderer.RenderedImage; ModelRendererBitmapSource rendererProcessed = new ModelRendererBitmapSource(bitmapTemp); rendererProcessed.Render(); ImageProcessed = rendererProcessed.RenderedImage; if (File.Exists(filePathJson)) { Load(); } else { this._threshold = new ModelValueDouble(_application.EventTokenChanged, 0.8); this._weigth = new ModelValueDouble(_application.EventTokenChanged, 1.0); this._label = filePathImage.Split('_')[1]; Save(); } }
public static Bitmap Filter(Bitmap imageBin, int minSize) { int [,] image = ToolsConvolution.BitMapToIntArray(imageBin); List <ConnectedComponent> component_list = GetConnectedComponents(image); foreach (var component in component_list) { if (component.Component.Count < minSize) { foreach (var pixel in component.Component) { image[pixel.Item1, pixel.Item2] = 0; } } } return(ToolsConvolution.IntArrayToBitMap(image, imageBin.PixelFormat)); }
private List <ConnectedComponent> GetTokens(Bitmap bitmapTemp) { List <ConnectedComponent>[] component_list_array = new List <ConnectedComponent> [TokenList.Count]; double[,] imageDouble = ToolsConvolution.BitMapToDoubleArray(bitmapTemp, 0.5); Parallel.For(0, TokenList.Count, tokenIndex => { ModelToken token = TokenList[tokenIndex]; var corr = ToolsConvolution.CorrelationFFT(imageDouble, token.GetKernel()); component_list_array[tokenIndex] = ToolsConnectedComponent.GetConnectedComponents(corr, token.Threshold.Value, token); }); List <ConnectedComponent> component_list = new List <ConnectedComponent>(); foreach (List <ConnectedComponent> list in component_list_array) { component_list.AddRange(list); } return(component_list); }
private void ExecuteParseAll() { if (PageFileSelected == null) { return; } bitmapSource = new Bitmap(PageFileSelected); if (bitmapSource == null) { return; } if ((bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) || (bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)) { bitmapSource = ToolsConvolution.ConvertTo24(bitmapSource); } //Rotate var bitmapRot = new RotateBilinear(PageLayout.Rotation.Value, true).Apply(bitmapSource); var linesH = ToolsFindLine.FindLinesH(bitmapRot.Width, bitmapRot.Height, PageLayout.LineSize.Value, PageLayout.LineOffset.Value, PageLayout.LineCount.Value); var linesV = ToolsFindLine.FindLinesV(bitmapRot.Width, bitmapRot.Height, PageLayout.ColSize.Value, PageLayout.ColOffset.Value, PageLayout.ColCount.Value); List <RenderLine> rl_list = new List <RenderLine>(); rl_list.AddRange(linesH); rl_list.AddRange(linesV); //Convert to gray Bitmap bitmapTemp = new Grayscale(1, 0, 0).Apply(bitmapRot); bitmapTemp = new Erosion().Apply(bitmapTemp); bitmapTemp = new Threshold(160).Apply(bitmapTemp); bitmapTemp = new Invert().Apply(bitmapTemp); //Tuple<List<ConnectedComponent>, double, double>[] componentSectionList = new Tuple<List<ConnectedComponent>, double, double>[1]; //Bitmap[] BitmapArray = new Bitmap[1]; Tuple <List <ConnectedComponent>, double, double>[,] componentTable = new Tuple <List <ConnectedComponent>, double, double> [PageLayout.LineCount.Value, PageLayout.ColCount.Value]; for (int lineIndex = 0; lineIndex < PageLayout.LineCount.Value; lineIndex++) { for (int colIndex = 0; colIndex < PageLayout.ColCount.Value; colIndex++) { //TODO add margin? int x = (int)((PageLayout.ColSize.Value * colIndex) + PageLayout.ColOffset.Value); // - PageLayout.Margin.Value; int y = (int)((PageLayout.LineSize.Value * lineIndex) + PageLayout.LineOffset.Value) - PageLayout.Margin.Value; int w = (int)PageLayout.ColSize.Value; // + (PageLayout.Margin.Value * 2); int h = (int)PageLayout.LineSize.Value + (PageLayout.Margin.Value * 2); Bitmap bitmap = new Crop(new Rectangle(x, y, w, h)).Apply(bitmapTemp); List <ConnectedComponent> lineComponentList = new List <ConnectedComponent>(); lineComponentList = GetTokens(bitmap); componentTable[lineIndex, colIndex] = new Tuple <List <ConnectedComponent>, double, double>(lineComponentList, x, y); } } //ImageParse = RenderBitmap(bitmapRot, rl_list, componentTable); ImageLayout = RenderBitmap(bitmapRot, rl_list, componentTable); //Create output string[,] string_table = new string[PageLayout.LineCount.Value, PageLayout.ColCount.Value]; for (int i = 0; i < PageLayout.LineCount.Value; i++) { for (int j = 0; j < PageLayout.ColCount.Value; j++) { string_table[i, j] = TokensToString(componentTable[i, j].Item1, 3, " "); } } string csvFileSelected = LayoutFileSelected.Replace(".png", ".csv"); ToolsIOCSV.WriteCSVFile(csvFileSelected, string_table, KozzionCore.IO.CSV.Delimiter.SemiColon); }