Recent

Author Topic: Cropping scanned image: best approach?  (Read 12888 times)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Cropping scanned image: best approach?
« on: July 29, 2013, 05:44:01 pm »
Hi,

Part of my papertiger OCR project involves converting a scanned image of a document into a clean tiff image ready for OCR.

I used to use sane's scanimage --swcrop=yes; however that corrupted colour scanning for me, and lead to occasionally cropping too much. Apart from that, I'd like to be able to process images that are produced by a different method (e.g. another computer with another scanning package giving the "raw" output).

So now I'm looking for a software method to detect the difference between the page and the scanner in the image.

What approach do you guys suggest? I could mess with the Pascal imagemagick bindings to try, or look into another option (read in image with BGRA? process?).

I'm a graphics newbie, so suggestions about custom programs that already do this, algorithms, links to relevant sites etc are more than welcome as well.

Thanks.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Mike.Cornflake

  • Hero Member
  • *****
  • Posts: 1260
Re: Cropping scanned image: best approach?
« Reply #1 on: July 29, 2013, 08:32:36 pm »
When I did some work with imagemagick, I ended up ignoring the pascal bindings and used the ImageMagick command lines tools directly (ie with TProcess).  (I too am a graphics newbie, but found the imagemagick commandline tools extremely well documented and easy to use).  The imagemagick bindings on the hand were rather complex... It meant distributing the ImageMagick programs with my app, but their licence allows for that, I just had to be clear in my About.. 

http://www.imagemagick.org/script/command-line-tools.php

Convert is the tool you'll be chasing

http://www.imagemagick.org/script/convert.php

That's all I can help you with I'm afraid.  Good luck.
« Last Edit: July 29, 2013, 08:37:07 pm by Mike.Cornflake »
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

circular

  • Hero Member
  • *****
  • Posts: 4217
    • Personal webpage
Re: Cropping scanned image: best approach?
« Reply #2 on: July 29, 2013, 08:42:17 pm »
Well you need to analyse the image to some extent. Do you have a scanned image to show us how it looks like?
Conscience is the debugger of the mind

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Cropping scanned image: best approach?
« Reply #3 on: July 29, 2013, 08:55:36 pm »
Thanks guys.

@Mike: thanks, have been calling imagemagick's convert and exactimage's econvert already through tprocess, but thinking about moving to imagemagick bindings as I probably could combine several actions.

@circular: of course, please find it here:
http://ubuntuone.com/2HKyzOSfdSc4vXVV44zf23
Image is a blank piece of paper (A4 folded in two) with some pen lines on it, scanned in 300dpi lineart (black and white). Nice and difficult ;)

Edit: same image scanned in colour:
Code: [Select]
scanimage --mode=Color --resolution=300 --swdeskew=yes --format=tiff > color.tiffhttp://ubuntuone.com/11LG0Ci8dhaSXmsAEWOSRh
... probably easier to detect the edges from that.


However, that happens to be an image from my scanner; other scanners may be different - and I would like to have a generic solution so other people can use it, too.
Regardless, I can obviously start with an image from my scanner and add support for other scanners/generalize the approach later.

Papertiger source code:
https://bitbucket.org/reiniero/papertiger/
« Last Edit: July 29, 2013, 09:03:37 pm by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Cropping scanned image: best approach?
« Reply #4 on: July 29, 2013, 09:35:14 pm »
its been years since the last time I looked around for this never done anything more than looking though as far as I remember there was a library that had deskew functionality for the Envision Image Library searching around for it revealed that it is included in the envision library you can download the 2.57 from http://www.intervalsoftware.com/download.html which has the source code of the deskew and and for ocr as a starter is very good but I do not know how good the final results might be.

EDIT:
oops sorry I forgot the most significant of the post. All the deskew algorithms use the Hough transform algorithm to recognize lines in an image more on that at http://en.wikipedia.org/wiki/Hough_transform.

sorry, need some more sleep.

yeah sleep please, no comments this time, just get the pascal source from http://galfar.vevb.net/wp/2011/deskewing-scanned-documents/
« Last Edit: July 29, 2013, 09:55:32 pm by taazz »
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Cropping scanned image: best approach?
« Reply #5 on: July 30, 2013, 07:32:11 am »
taazz: hope you had a good rest - in your sleep-deprived state you did anticipate my next request (for deskewing). Perhaps the skew code you linked to will also help in detecting the image size and therefore help in cropping as well.

I'd rather use existing code instead of having to invent/adapt my own unless the latter is clearly superior... so I'll have a look at scantailor etc as well.

Edit: actually, I think I'll have a look at the sane backend driver for my scanner to see if I can translate some of the c code..

Thanks - and more suggestions welcome of course ;)
« Last Edit: July 30, 2013, 07:42:35 am by BigChimp »
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

circular

  • Hero Member
  • *****
  • Posts: 4217
    • Personal webpage
Re: Cropping scanned image: best approach?
« Reply #6 on: July 31, 2013, 01:26:14 am »
Maybe you can compute the gradient of HSL values, knowing that the background should have low values, and maybe H should not change at all.

You can also try to compute the contours of the page, supposing that it should be vertical and horizontal
Conscience is the debugger of the mind

hugoengel2003

  • New member
  • *
  • Posts: 8
Re: Cropping scanned image: best approach?
« Reply #7 on: October 01, 2014, 08:20:39 pm »
Excuse me,
I'm looking for a scan image with webcam and ocr documents package for Lazarus to use on Windows 7.
Does it exist? Does anybody have the download link or other idea to use it on Lazarus Windows projects?
Thanks a lot.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Cropping scanned image: best approach?
« Reply #8 on: October 02, 2014, 10:09:17 am »
You could look into my papertiger project for ideas.

This project is why I started this thread. Follow the bitbucket link in my signature.

Currently runs on Linux; requires tesseract but could be ported to Windows.

Alternatively, see the ocrivist project - also currently based on Linux.... I *think*...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

bracara

  • New Member
  • *
  • Posts: 20
Re: Cropping scanned image: best approach?
« Reply #9 on: October 03, 2014, 09:24:02 pm »
Alternatively, see the ocrivist project - also currently based on Linux.... I *think*...

I have had Ocrivist working on XP and Vista, but have not had access to a Win7/8 machine to compile for 64bit.
I'm sure Ocrivist would compile easily using Lazarus, but it would also mean setting up MinGW to compile Tesseract and Leptonica libraries for Win7/8

 

TinyPortal © 2005-2018