Page 1 of 1

auto-crop multiple old photographs from scanned image?

Posted: 2008-03-02T01:27:38-07:00
by dsj
Im attempting a project that might prove to be insane - scanning in all my old paper photographs.
you know the drill... theres typically 3 to 5 images that can fit on my flatbed scanner and they
vary in size and position in the scanned image.

Id like to be able to programatically auto-crop the individual images out of the larger scan image.

has anyone here done such a thing? can I use imagemagick APIs to do it? I can code but Im not
familiar with image processing techniques (yet). any help pointing me in the right direction would
be greatly appreciated - i really dont want to manually crop them one by one in photoshop! :-(

thanx!

.dsj.

ps - a secondary goal would be to auto-detect orientation and rotate them programatically if
its possible. being right 50%+ of the time is still a help. anyone ever done this? again, these are
scanned images so no EXIF info to help.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-03-02T23:52:55-07:00
by anthony
Post a URL to an example image in a new topic in the "User" Forum.

I am sure someone can figure out something with an example (preferable smallish) to work from.

Make it a 'difficult' image!

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-03-03T13:12:21-07:00
by fmw42
If the background color around the scanned image is relatively constant, you can try my two scripts autotrim and unrotate. You can get them at

http://www.fmwconcepts.com/imagemagick/index.html

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-03-04T02:37:01-07:00
by dsj
anthony - posted to User forum as you suggested. thankyou.

fmw42 - thankyou very much for offering your help and time. autotrim returned basically the same
image. maybe I used it wrong?!?!? and I think unrotate will be invaluable once I get the individual
pix cropped out of the larger scanned image.

i noticed you also have a magicwand script. my first thought to solve this problem was I need
something like photoshop's magicwand with a 10 or so pixel threshold to identify the deadspace
between photos and then do an inverse select to select all the photos and crop from there. but
how to identify "deadspace"? could I use a scan without pictures as a sample of the lid of my
scanner to establish the base color to "find"? Im wondering if your magicwand will get me close if
I can use a color range instead of a position as position is different on each scan. once I have
seperate photos it straight forward to unrotate and run auto-levels on each pic.

again, I appreciate your time helping me with my problem. :-)

.dsj.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-04-24T21:47:07-07:00
by fmw42
Sorry, I did not see your post until now. Perhaps you can post a link to an example image, if you have not already solved your problem.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-04-24T23:12:57-07:00
by dsj
I havent solved the problem yet. here's a sample image...
http://jonespix.com/imagemagick_example ... an0058.jpg

thanx. .dsj.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2008-04-25T15:19:24-07:00
by fmw42
My autotrim and unrotate scripts only work on one image in a relatively constant background color.

Anthony has some script somewhere where he presumably extracted multiple pictures, but I think they had to be in one column. There was a post about that at one time some time ago. You might search the archives.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2013-06-04T17:35:31-07:00
by anthony
My method of extracting multiple images from a scan was based on fred's "multicrop" script. images could
be anywhere on the page.

But I added a some extra mask preparation code to make the separation of the images from non-images
better. I did submit that code to Fred, but I don't know if it is included or not.

=======8<--------
# # fill from the given coolrdinate point
# convert $tmp/IN.mpc -fuzz ${fuzzval}% -fill none \
# -draw "matte $coords floodfill" \
# -fill red +opaque none \
# $tmp/MASK.mpc

# add a border to set the background color (and/or flood fill from all edges)
# 'replace' is used instead of floodfill, with a morphology Open to avoid
# problems with 'thin decorative lines' that box or link images. It also
# tends to remove most 'text' and other 'too small' hits.

convert $tmp/IN.mpc -fuzz ${fuzzval}% -fill none \
-bordercolor $bgcolor -border 1x1 \
-draw "matte $coords replace" \
-shave 1x1 -fill red +opaque none \
-channel all -morphology Open Square:3 \
$tmp/MASK.mpc

=======8<--------

After the mask is prepared the script does a grid search of the mask looking for parts that are large enough to be an 'image'. When it finds one it crops the same area from the original image.

Works very well once you have the mask right.

This was for actual extraction of images from a magazine that liked adding the occasional thin decorative lines and boxes to a page.

After this I still needed to scan through the results as it could not separate two slightly overlapping images. But I did not really need to do much more.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2013-06-04T17:56:06-07:00
by fmw42
Perhaps an example image will help. But look at my script, multicrop, at the link below.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2013-07-25T16:09:33-07:00
by anthony
I would like to say that while doing this is well and good. If you need to rotate the image, you will be distorting the image, and that means the image will become degraded to some extent.

The same happens for the most common type of distortion, resize. Though resizing smaller also averages more pixels, so while the resize degraded, it also in a sense sharpens and improves the image quality at the smaller scale.

So it is better if the images being scanned were not rotated, but properly aligned with the scanning matrix. That is why scanners have these edges, that you can push images against. It lines up the edge to match the scanning matrix. For multiple photos you can always add some extra 'edges' across the scanning area so you can place more images into a single scan. As a bonus the image location also becomes 'known', making auto-split and crop easier to handle.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2013-07-25T22:52:54-07:00
by Emerico
Well, most of the old photos are not half as crisp and clear as anything a cheap digital camera would produce nowadays. I've yet to see any distortion owing to rotation. There is an interpolation mechanism to make sure that high contrast parts don't look jagged after resize/rotate.
Lining up photos against the edges is a good idea though.

Re: auto-crop multiple old photographs from scanned image?

Posted: 2013-07-25T23:26:12-07:00
by anthony
Not distortion per-say. It is more typically a general blurring coming from a step that is easily avoided.

More often than not, when you resize the image smaller, (for display or other use), any such extra blurring is of little consequence.