Does anybody have a suggestion for a java library that performs automatic cropping and deskewing of images (like those retrieved from a flatbed scanner)?
5 Answers
Deskewing
Take a look at Tess4j (Java JNA wrapper for Tesseract).
You can combine ImageDeskew.getSkewAngle() with ImageHelper.rotate(BufferedImage image, double angle).
There is an example on how to use it on the test folder of the tess4j project Tesseract1Test.java
public void testDoOCR_SkewedImage() throws Exception {
logger.info("doOCR on a skewed PNG image");
File imageFile = new File(this.testResourcesDataPath, "eurotext_deskew.png");
BufferedImage bi = ImageIO.read(imageFile);
ImageDeskew id = new ImageDeskew(bi);
double imageSkewAngle = id.getSkewAngle(); // determine skew angle
if ((imageSkewAngle > MINIMUM_DESKEW_THRESHOLD || imageSkewAngle < -(MINIMUM_DESKEW_THRESHOLD))) {
bi = ImageHelper.rotateImage(bi, -imageSkewAngle); // deskew image
}
String expResult = "The (quick) [brown] {fox} jumps!\nOver the $43,456.78 <lazy> #90 dog";
String result = instance.doOCR(bi);
logger.info(result);
assertEquals(expResult, result.substring(0, expResult.length()));
}
- 2,264
- 1
- 28
- 26
ImageMagick can do that; you can use the ImageMagick Java bindings. The auto-crop operator is probably what you're looking for. Automatic deskewing is a much harder problem and involves some significant image processing; I'm not sure if ImageMagick can handle that. If you can figure out the skewing parameters using something else, ImageMagick can definitely unskew it for you.
- 375,615
- 96
- 501
- 581
I wrote a note that simple port of a very good deskewer. It works best if you have some text in the image.
- 1,238
- 15
- 22
- 359
- 2
- 8
I've written a simple image deskew app, includes source. Available at:
- 1
- 1
-
1Where can I found the source? – Laszlo Nov 21 '13 at 10:03
-
1@roland-quast The link is broken, can you updated it? – delkant Mar 27 '16 at 12:29
I'd imagine that someone has built a library on top of the Java Advanced Imaging API for doing this. You could try Googling for "Java Advanced Imaging deskew".
- 4,217
- 3
- 23
- 37
-
as of 2012, this page is now the top hit for that search on google. – Ian McLaird Jun 04 '12 at 20:55
-
@IanMcLaird Hee hee. That's wonderfully circular. Have you ever tried searching for "recursion" on Google? – Matt Passell Jun 05 '12 at 13:51