private static void Main(string[] args) { Console.Write("Input image path:"); string input = Console.ReadLine(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); T.TesseractEngine engine = new T.TesseractEngine("tessdata", "tur", T.EngineMode.TesseractAndLstm); stopwatch.Stop(); Console.WriteLine("Engine creation :" + stopwatch.ElapsedMilliseconds.ToString() + " ms"); stopwatch.Restart(); T.Pix image = T.Pix.LoadFromFile(input); T.Page page = engine.Process(image); string text = page.GetText(); stopwatch.Stop(); Console.WriteLine("Process time :" + stopwatch.ElapsedMilliseconds.ToString() + " ms"); Console.Write("Result: " + text); Console.Read(); }
internal Page(TesseractEngine engine, Pix image, string imageName, Rect regionOfInterest, PageSegMode pageSegmentMode) { Engine = engine; Image = image; ImageName = imageName; RegionOfInterest = regionOfInterest; PageSegmentMode = pageSegmentMode; }
private PixelFormat GetPixelFormat(Pix pix) { switch (pix.Depth) { //case 1: return PixelFormat.Format1bppIndexed; //case 2: return PixelFormat.Format4bppIndexed; // case 4: return PixelFormat.Format4bppIndexed; case 8: return PixelFormat.Format8bppIndexed; case 16: return PixelFormat.Format16bppGrayScale; case 32: return PixelFormat.Format32bppArgb; default: throw new InvalidOperationException(String.Format("Pix depth {0} is not supported.", pix.Depth)); } }
/// <summary> /// Processes the specific image. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, Rect region, PageSegMode? pageSegMode = null) { if (image == null) throw new ArgumentNullException("image"); if (region.X1 < 0 || region.Y1 < 0 || region.X2 > image.Width || region.Y2 > image.Height) throw new ArgumentException("The image region to be processed must be within the image bounds.", "region"); if (processCount > 0) throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the page once your finished with it."); processCount++; Interop.TessApi.BaseAPISetPageSegMode(handle, pageSegMode.HasValue ? pageSegMode.Value : DefaultPageSegMode); Interop.TessApi.BaseApiSetImage(handle, image.Handle); Interop.TessApi.BaseApiSetRectangle(handle, region.X1, region.Y1, region.Width, region.Height); var page = new Page(this); page.Disposed += OnIteratorDisposed; return page; }
private void CopyColormap(Bitmap img, Pix pix) { var imgPalette = img.Palette; var imgPaletteEntries = imgPalette.Entries; var pixColormap = PixColormap.Create(pix.Depth); try { for (int i = 0; i < imgPaletteEntries.Length; i++) { if (!pixColormap.AddColor((PixColor)imgPaletteEntries[i])) { throw new InvalidOperationException(String.Format("Failed to add colormap entry {0}.", i)); } } pix.Colormap = pixColormap; } catch (Exception) { pixColormap.Dispose(); throw; } }
public Bitmap Convert(Pix pix, bool includeAlpha = false) { var pixelFormat = GetPixelFormat(pix); var depth = pix.Depth; var img = new Bitmap(pix.Width, pix.Height, pixelFormat); BitmapData imgData = null; PixData pixData = null; try { // TODO: Set X and Y resolution // transfer pixel data if ((pixelFormat & PixelFormat.Indexed) == PixelFormat.Indexed) { TransferPalette(pix, img); } // transfer data pixData = pix.GetData(); imgData = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, pixelFormat); if (depth == 32) { TransferData32(pixData, imgData, includeAlpha ? 0 : 255); } else if (depth == 16) { TransferData16(pixData, imgData); } else if (depth == 8) { TransferData8(pixData, imgData); } else if (depth == 1) { TransferData1(pixData, imgData); } return img; } catch (Exception) { img.Dispose(); throw; } finally { if (imgData != null) { img.UnlockBits(imgData); } } }
/// <summary> /// Converts the specified <paramref name="pix"/> to a Bitmap. /// </summary> /// <param name="pix">The source image to be converted.</param> /// <returns>The converted pix as a <see cref="Bitmap"/>.</returns> public static Bitmap ToBitmap(Pix pix) { return pixConverter.Convert(pix); }
/// <summary> /// Processes a specified region in the image using the specified page layout analysis mode. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param> /// <param name="region">The image region to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, string inputName, Rect region, PageSegMode? pageSegMode = null) { if (image == null) throw new ArgumentNullException("image"); if (region.X1 < 0 || region.Y1 < 0 || region.X2 > image.Width || region.Y2 > image.Height) throw new ArgumentException("The image region to be processed must be within the image bounds.", "region"); if (processCount > 0) throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the page once your finished with it."); processCount++; var actualPageSegmentMode = pageSegMode.HasValue ? pageSegMode.Value : DefaultPageSegMode; Interop.TessApi.Native.BaseAPISetPageSegMode(handle, actualPageSegmentMode); Interop.TessApi.Native.BaseApiSetImage(handle, image.Handle); if(!String.IsNullOrEmpty(inputName)) { Interop.TessApi.Native.BaseApiSetInputName(handle, inputName); } var page = new Page(this, image, inputName, region, actualPageSegmentMode); page.Disposed += OnIteratorDisposed; return page; }
public PageDisposalHandle(Page page, Pix pix) { this.page = page; this.pix = pix; page.Disposed += OnPageDisposed; }
/// <summary> /// Processes a specified region in the image using the specified page layout analysis mode. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, Rect region, PageSegMode?pageSegMode = null) => Process(image, null, region, pageSegMode);
/// <summary> /// Processes a specified region in the image using the specified page layout analysis mode. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, Rect region, PageSegMode? pageSegMode = null) { return Process(image, null, region, pageSegMode); }
public Pix GetBinaryImage(PageIteratorLevel level) { return(Pix.Create(Interop.TessApi.PageIteratorGetBinaryImage(handle, level))); }
public Pix GetImage(PageIteratorLevel level, int padding, out int x, out int y) { return(Pix.Create(Interop.TessApi.PageIteratorGetImage(handle, level, padding, out x, out y))); }
internal PixData(Pix pix) { Pix = pix; Data = LeptonicaApi.Native.pixGetData(Pix.Handle); WordsPerLine = LeptonicaApi.Native.pixGetWpl(Pix.Handle); }
internal PixData(Pix pix) { Pix = pix; Data = LeptonicaPrimitives.Api.pixGetData(pix.Handle); WordsPerLine = LeptonicaPrimitives.Api.pixGetWpl(pix.Handle); }
/// <summary> /// Processes a specified region in the image using the specified page layout analysis mode. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, Rect region, PageSegMode? pageSegMode = null) { return Process(image, null, new Rect(0, 0, image.Width, image.Height), pageSegMode); }
internal PixData(Pix pix) { Pix = pix; Data = Interop.LeptonicaApi.Native.pixGetData(Pix.Handle); WordsPerLine = Interop.LeptonicaApi.Native.pixGetWpl(Pix.Handle); }
public static Bitmap ToBitmap(Pix pix) => pixConverter.Convert(pix);
private void TransferPalette(Pix pix, Bitmap img) { var pallete = img.Palette; var maxColors = pallete.Entries.Length; var colormap = pix.Colormap; if (colormap != null && colormap.Count <= maxColors) { var colormapCount = colormap.Count; for (int i = 0; i < colormapCount; i++) { pallete.Entries[i] = (SD.Color)colormap[i]; } } else { for (int i = 0; i < maxColors; i++) { var value = (byte)(i % maxColors); pallete.Entries[i] = SD.Color.FromArgb(value, value, value); } } // This is required to force the palette to update! img.Palette = pallete; }
internal Page(TesseractEngine engine, Pix image, Rect regionOfInterest) { Engine = engine; Image = image; RegionOfInterest = regionOfInterest; }
/// <summary> /// Converts the specified <paramref name="pix"/> to a Bitmap. /// </summary> /// <param name="pix">The source image to be converted.</param> /// <returns>The converted pix as a <see cref="Bitmap"/>.</returns> public static Bitmap ToBitmap(Pix pix) { return(pixConverter.Convert(pix)); }
/// <summary> /// Processes a specified region in the image using the specified page layout analysis mode. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="region">The image region to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> /// <returns>A result iterator</returns> public Page Process(Pix image, Rect region, PageSegMode?pageSegMode = null) { return(Process(image, null, region, pageSegMode)); }
/// <summary> /// Processes the specific image. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> public Page Process(Pix image, string inputName, PageSegMode?pageSegMode = null) { return(Process(image, inputName, new Rect(0, 0, image.Width, image.Height), pageSegMode)); }
/// <summary> /// Processes the specific image. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> public Page Process(Pix image, string inputName, PageSegMode? pageSegMode = null) { return Process(image, inputName, new Rect(0, 0, image.Width, image.Height), pageSegMode); }
/// <summary> /// Processes the specific image. /// </summary> /// <remarks> /// You can only have one result iterator open at any one time. /// </remarks> /// <param name="image">The image to process.</param> /// <param name="pageSegMode">The page layout analyasis method to use.</param> public Page Process(Pix image, PageSegMode?pageSegMode = null) => Process(image, null, new Rect(0, 0, image.Width, image.Height), pageSegMode);