3

I've got a few thousand scanned images that I am trying to crop. I'm writing a script that will crop the pictures accordingly if I can determine whether the source image is a 3x5 picture or a 4x6 picture from the uncropped border (scanner lid).

I've found command-line tools to compare and match entire images (using imagemagick convert), but not a designated region of an image:

convert img1.jpg  "img2.jpg" -compose difference -composite -colorspace gray miff:- | identify -verbose - | sed -n '/^.*mean: */{s//scale=2;/;s/(.*)//;s/$/*100\/32768/;p;q;}' | bc

(it's usually a match if the result < .10, but it is cpu-intensive)

Is there a tool or Python image library that will let me match compare certain regions from two images to see if they match? The uncropped regions are not plain white, as evidenced by the example images below (1 3x5, 1 4x6). All I need to match is the first 100 or so pixels, and obviously I can't match the entire image. I have considered copying and cropping the image and matching the crop to a reference image, but that seems less than optimal.

enter image description here

enter image description here

Hotloo Xiranood
  • 303
  • 1
  • 7
mrlitsta
  • 636
  • 8
  • 8
  • If I understood the problem correctly, I think you want to match the images even if they are not in the same size, and also with different background. Actually, I think there is no specific toolbox that you can use really use for yourself, as it is pretty problem specific. However, as far as I know, there is a toolbox call [PIL](http://stackoverflow.com/questions/94875/image-processing-in-python) for python that gives you some image processing capability. Have you ever tried that? Additionally, you should search for some computer vision algorithm instead of pure command line toolbox. – Hotloo Xiranood Dec 09 '11 at 15:11
  • Actually, all images will be the same size. If you look at the images above, the two images above will NOT match, but a second 3x5 photo will have the same background, and therefore be "matched" as a 3x5 photo. Notice the left 50 pixels of the first picture will be the same for any 3x5 images, and that is the region of the image I am interested in matching. – mrlitsta Dec 09 '11 at 17:49

1 Answers1

2

I am unaware of the existence of a specific command-line tool to do this, but it would be pretty trivial to write your own using numpy. The basic procedure would be:

  1. Have the plain image of the scanner's lid load into a ndarray.
  2. Load each photo/image in a ndarray.
  3. Compare the significant portion of the two and assign a score.
  4. If the score is above your threshold...

If performance is an issue, step #2 could be optimised by moving along the file with seek() and only reading portions of it to speed things up.

A proof-of-concept implementation of the numpy part:

>>> import numpy as np
>>> scanner_lid = np.ones((5, 5))
>>> scanner_lid
array([[ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.],
       [ 1.,  1.,  1.,  1.,  1.]])
>>> photo = np.random.randint(0, 2, (5, 5))
>>> photo
array([[0, 0, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 1, 1, 1],
       [1, 1, 0, 0, 1],
       [1, 0, 1, 1, 1]])
>>> matching_pixels = scanner_lid[0:2, 0:2] == photo[0:2, 0:2]  #compare the top-left 4 pixels
>>> matching_pixels
array([[False, False],
       [False,  True]], dtype=bool)
>>> np.sum(matching_pixels)
1

Of course in a real-world application you should probably measure the difference between the pixel values as intensity and colour balance might change from scan to scan, etc... Yet, I think it wouldn't require a lot of time to come up with something usable for your job.

HTH!

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
mac
  • 42,153
  • 26
  • 121
  • 131
  • that sounds like a great idea in the absence of a "put in the region and match" tool that I long for. One question, any tips how how to load a .jpg image into an array? I'll take a look and see what I find. – mrlitsta Dec 10 '11 at 16:02
  • Looks like I've got these: http://stackoverflow.com/questions/1109422/getting-list-of-pixel-values-from-pil http://stackoverflow.com/questions/138250/read-the-rgb-value-of-a-given-pixel-in-python-programaticly http://effbot.org/imagingbook/image.htm to give me a good start in writing my own. Thanks! – mrlitsta Dec 10 '11 at 16:05