search subimage in pre-defined region of a large image

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
timaios
Posts: 1
Joined: 2018-03-20T12:26:00-07:00
Authentication code: 1152

search subimage in pre-defined region of a large image

Post by timaios »

Hallo!

I would like to implement a fast way to get the position (XY-coordinates) of a defined subimage in a large image. I find ImageMagick to be a quite professional and versatile tool, while this forum offers very good feedback!

The subimage is usually an arbitrary printed mark that is located at pre-defined areas at the corner of a large image. (After getting the coordinates of three of those marks I than cut the image to get the "normalized" content of the scanned paper.)

subimage:
http://maketwowishes.ddns.net/content_static/justUR.tif

The actual size of an original large image is about 2340x3500 pixel, 1-bit b/w tiff-image, and derives from big amounts (10.000+) of scanned pages.
Sample (bottom-right area) of a large image:
http://maketwowishes.ddns.net/content_s ... sample.tif

I have been successful to get the coordinates with the following command line (Linux, ImageMagick 6.9.7-4 Q16 x86_64):

Code: Select all

compare -subimage-search -metric RMSE -similarity-threshold 0.3 -dissimilarity-threshold 1.0 S10_sample.tif justUR.tif null: 2>&1
Threshold is necessary since the scanned marks differ slightly (pixel-wise) from each other.
To speed up the process I would like to crop the corner of the large image in order to only subimage-search that area on-the-fly. To avoid unnecessary I/O operations I would like to use a virtual image (w/o acutal writing it to file).
PerlMagick seems fit to that task.
Unfortunately that interface does not seem to have the compare -subimage-search method.
  • Is there an equivalent PerlMagick method?
  • Is there a way of streaming in shell? Like e.g. so (does not work, just to illustrate the idea):

    Code: Select all

    convert S10.tif -crop 230x300+2100+3200 - | compare -subimage-search -metric RMSE -similarity-threshold 0.3 -dissimilarity-threshold 1.0 - justUR.tif null: 2>&1
  • What are the options to further speed up the process - preferably to <= 1s ? Different algorithm/ strategy/ tool?
  • Is the Q8-Version of IM more appropriate in terms of speed for this task?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: search subimage in pre-defined region of a large image

Post by fmw42 »

try

Code: Select all

convert S10.tif -crop 230x300+2100+3200 [color=#FF0000]miff:-[/color] | compare -subimage-search -metric RMSE -similarity-threshold 0.3 -dissimilarity-threshold 1.0 - justUR.tif null: 2>&1
You may need/want to add +repage after the crop before the miff:-
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: search subimage in pre-defined region of a large image

Post by snibgo »

I don't know PerlMagick.

You can crop and compare within a single "convert" command.

For greater performance, you can use a iterative resizing method. This can be done in a script (see Searching an image) or as a process module (see multi-scale image search).

Q8 will probably be faster than Q16.
snibgo's IM pages: im.snibgo.com
Post Reply