Home > Net >  ImageMagick/etc.: automated removal of unclean edges from scanning?
ImageMagick/etc.: automated removal of unclean edges from scanning?

Time:11-13

With a few thousand images like the following one, with complex edges from scanning (first a perfectly regular white one, then a very fuzzy gray/black'ish one), is there a good algorithm to figure out the coordinates of the area-to-keep? In the below example, going clockwise from the north-western corner, the optimal coordinates would be:

  • 121,18
  • 1934,18
  • 1934,1312
  • 121,1312

So that from this input image...

sample image

... can be cropped to remove anything tinted red, as shown here:

enter image description here

To clarify, I do know how to use ImageMagick or other tools to crop to that area. What I'm not sure about is how to figure out how to get to those above numbers? Do you just binarize the image and then go left to right until you hit "white" for the first time, then do the same from top to bottom, right to left and bottom to top? Or is there some library, script, etc. which already does that for you?

CodePudding user response:

I would use vertical and horizontal projections of the image. By a simple detection of the highest transition, you easily find the limit between the background and the paper.

enter image description here

CodePudding user response:

Your image has a border of white surrounding a border of black. So you can do two fuzzy -trim processes after padding with the appropriate color to ensure it goes all around the image. So in ImageMagick 6, it would be

Input:

enter image description here

convert book.jpg -bordercolor white -border 1 -fuzz 55% -trim  repage -bordercolor black -border 1 -trim  repage book_trimmed.jpg

Result:

enter image description here

  • Related