Skip to content

Commit

Permalink
Allow changing the current region of interest without having to reloa…
Browse files Browse the repository at this point in the history
…d the entire image.
  • Loading branch information
charlesw committed May 17, 2014
1 parent 8313405 commit 1b92e22
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/Tesseract.Tests/EngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@ public void CanParseText()
}
}
}

[Test]
public void CanParseDifferentRegionsInSameImage()
{
using (var engine = new TesseractEngine(@"./tessdata", "eng", EngineMode.Default)) {
using(var img = Pix.LoadFromFile("./phototest.tif")) {
using(var page = engine.Process(img, Rect.FromCoords(0, 0, img.Width, 188))) {
var region1Text = page.GetText();

const string expectedTextRegion1 =
"This is a lot of 12 point text to test the\nocr code and see if it works on all types\nof file format.\n\n";

Assert.That(region1Text, Is.EqualTo(expectedTextRegion1));

page.RegionOfInterest = Rect.FromCoords(0, 188, img.Width, img.Height);

var region2Text = page.GetText();
const string expectedTextRegion2 =
"The quick brown dog jumped over the\nlazy fox. The quick brown dog jumped\nover the lazy fox. The quick brown dog\njumped over the lazy fox. The quick\nbrown dog jumped over the lazy fox.\n\n";

Assert.That(region2Text, Is.EqualTo(expectedTextRegion2));
}
}
}
}

[Test]
public void CanParseUznFile()
Expand Down
33 changes: 30 additions & 3 deletions src/Tesseract/Page.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,41 @@ namespace Tesseract
public sealed class Page : DisposableBase
{
private bool runRecognitionPhase;

private Rect regionOfInterest;


public TesseractEngine Engine { get; private set; }
public Pix Image { get; private set; }

internal Page(TesseractEngine engine)
internal Page(TesseractEngine engine, Pix image, Rect regionOfInterest)
{
Engine = engine;
Image = image;
RegionOfInterest = regionOfInterest;
}


/// <summary>
/// The current region of interest being parsed.
/// </summary>
public Rect RegionOfInterest {
get {
return regionOfInterest;
}
set {
if (value.X1 < 0 || value.Y1 < 0 || value.X2 > Image.Width || value.Y2 > Image.Height)
throw new ArgumentException("The region of interest to be processed must be within the image bounds.", "value");

if(regionOfInterest != value) {
regionOfInterest = value;

// update region of interest in image
Interop.TessApi.BaseApiSetRectangle(Engine.Handle, regionOfInterest.X1, regionOfInterest.Y1, regionOfInterest.Width, regionOfInterest.Height);

// request rerun of recognition on the next call that requires recognition
runRecognitionPhase = true;
}
}
}

public PageIterator AnalyseLayout()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Tesseract/TesseractEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public Page Process(Pix image, PageSegMode? pageSegMode = null)
/// <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);
return Process(image, null, region, pageSegMode);
}


Expand Down Expand Up @@ -227,11 +227,10 @@ public Page Process(Pix image, string inputName, Rect region, PageSegMode? pageS

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);
if(!String.IsNullOrEmpty(inputName)) {
Interop.TessApi.BaseApiSetInputName(handle, inputName);
}
var page = new Page(this);
var page = new Page(this, image, region);
page.Disposed += OnIteratorDisposed;
return page;
}
Expand Down

0 comments on commit 1b92e22

Please sign in to comment.