Finding multiple subimages

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
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Finding multiple subimages

Post by jumpjack »

I am in trouble finding multipe instances of same sub-image inside this image:

Image

"compare" confuses "1" with "2" in IM V6, and in V7 it simply fails saying the two images are too different.

Image

Image

I tried several command lines, for example:

Code: Select all

compare -verbose -subimage-search -metric AE base-image2.png 2r.png  diff.png
Tried with IM 6.6.6 and IM 7.0.8 in Windows 10.

I need to ignore color differences, hence this image would have 2 instances of each number.

I also had a look at this page but I don't know how to translate BASH to DOS, and word wrap does not help...
https://www.imagemagick.org/Usage/compare/#difference

edit: added sub-images
Last edited by jumpjack on 2019-07-14T09:01:29-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding multiple subimages

Post by snibgo »

Please link to your subimage 2r.png.

On using IM under Windows, see http://www.imagemagick.org/Usage/windows/
snibgo's IM pages: im.snibgo.com
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

Sorry I forgot the subimages. I just added them, anyway they're just croppings of the main image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding multiple subimages

Post by snibgo »

No, they are not just croppings. They are much larger than in the main image. IM will not find any exact matches.
snibgo's IM pages: im.snibgo.com
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

I don't understand why the subimages look bigger in the browser, they are just 8x10 images!
This is what I see on my PC, maybe I messed up something while uploading?!?
Image
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

I added "dimensions" column to explorer window... and something weird appeared!
I'll do a global check to eventually have all images properly sized and I'll report... :?
Image
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

Ok now I got the right cropped images:
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image

Full image:
Image

I tried with "2": while there are "2" in the base image, I get good results; but after each finding I delete the "2" and restart the search; after the "2" are finished, IM starts "finding" 1, 7 and other numbers.

So, I have two questions:
- how do I automatically get all instances of same number searched, rather than manually deleting each found number from original image and searching again?
- how do I find only exact matches, or at least 95% matches?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding multiple subimages

Post by snibgo »

Code: Select all

f:\web\im>%IM%identify 2r.jpg
2r.jpg JPEG 14x20 14x20+0+0 8-bit sRGB 638B 0.000u 0:00.015

f:\web\im>%IM%identify numeri-tutti.png
numeri-tutti.png PNG 80x30 80x30+0+0 8-bit sRGB 4405B 0.000u 0:00.000
So 2r.png is 20 pixels high, and numeri-tutti.png is only 30 pixels high. This is less than twice the height of the subimage, but it should be more than twice.
snibgo's IM pages: im.snibgo.com
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

the 2r.png I uploaded was wrong, please look at new uploaded images: 0g.png to 9g.png
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding multiple subimages

Post by snibgo »

You said "I need to ignore color difference" but IM won't ignore the colours. If you want each subimage to be found twice, you need to pre-process the images so that each subimage actually does occur twice in the main image.
snibgo's IM pages: im.snibgo.com
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

As I have just two colors, I think it will be faster replicating the check for each color.
So now we can assume for now that I only want to find yellow numbers.
Same questions apply:

- how do I automatically get all instances of same number searched, rather than manually deleting each found number from original image and searching again?
- how do I find only exact matches, or at least 95% matches?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Finding multiple subimages

Post by snibgo »

jumpjack wrote:- how do I automatically get all instances of same number searched, rather than manually deleting each found number from original image and searching again?
"compare -subimage-search" finds the best match. If you want the next-best match, you can overpaint the pixels in the main image where the subimage was found, and run the search again.
jumpjack wrote:- how do I find only exact matches, or at least 95% matches?
"compare -subimage-search" finds the best match, and gives a score. For example:

Code: Select all

f:\web\im>%IMG7%magick compare -subimage-search -metric RMSE numeri-tutti.png 4g.png NULL:

0 (0) @ 32,20
The best match has the top-left of the subimage at (32,20) in the main image. The score (in parentheses) is 0, which for RMSE means a perfect match. The scrore is between 0 and 1. Your script might reject any score worse than 0, or worse than 0.05, or whatever.
snibgo's IM pages: im.snibgo.com
jumpjack
Posts: 69
Joined: 2010-12-10T05:29:16-07:00
Authentication code: 8675308

Re: Finding multiple subimages

Post by jumpjack »

snibgo wrote: 2019-07-14T10:38:10-07:00 If you want the next-best match, you can overpaint the pixels in the main image[...]
This is what I am doing, I hoped there was a cleaner method...
snibgo wrote: 2019-07-14T10:38:10-07:00
jumpjack wrote:- how do I find only exact matches, or at least 95% matches?
[...]. The score (in parentheses) is 0, which for RMSE means a perfect match. The scrore is between 0 and 1. Your script might reject any score worse than 0, or worse than 0.05, or whatever.
thanks
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Finding multiple subimages

Post by fmw42 »

If you are using a Unix variant of Imagemagick, then see my script maxima at my link below. It will find multiple instances of matches that have the top compare subimage-search scores. It is a bash unix shell script that does something similar to what user snibgo just suggested.

Also see https://imagemagick.org/script/identify.php

Code: Select all

magick identify -precision 5 -define identify:locate=maximum -define identify:limit=3 image.png
You can apply that to the compare score image from subimage-search of compare. The second output image.

That will find the top 3 values in the compare score image. But you have to mask out the locations of each match so the it does not just find the one pixel offset with a high score. So you have to write a loop, find the max, mask over that region with black (as in my script), then use identify again. You do not have to run compare each time.
Post Reply