Page 1 of 1

Finding multiple subimages

Posted: 2019-07-14T08:23:52-07:00
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

Re: Finding multiple subimages

Posted: 2019-07-14T08:43:22-07:00
by snibgo
Please link to your subimage 2r.png.

On using IM under Windows, see http://www.imagemagick.org/Usage/windows/

Re: Finding multiple subimages

Posted: 2019-07-14T09:01:59-07:00
by jumpjack
Sorry I forgot the subimages. I just added them, anyway they're just croppings of the main image.

Re: Finding multiple subimages

Posted: 2019-07-14T09:06:26-07:00
by snibgo
No, they are not just croppings. They are much larger than in the main image. IM will not find any exact matches.

Re: Finding multiple subimages

Posted: 2019-07-14T09:11:59-07:00
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

Re: Finding multiple subimages

Posted: 2019-07-14T09:15:59-07:00
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

Re: Finding multiple subimages

Posted: 2019-07-14T09:32:25-07:00
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?

Re: Finding multiple subimages

Posted: 2019-07-14T09:32:56-07:00
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.

Re: Finding multiple subimages

Posted: 2019-07-14T09:35:57-07:00
by jumpjack
the 2r.png I uploaded was wrong, please look at new uploaded images: 0g.png to 9g.png

Re: Finding multiple subimages

Posted: 2019-07-14T09:39:06-07:00
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.

Re: Finding multiple subimages

Posted: 2019-07-14T09:41:55-07:00
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?

Re: Finding multiple subimages

Posted: 2019-07-14T10:38:10-07:00
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.

Re: Finding multiple subimages

Posted: 2019-07-14T10:43:10-07:00
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

Re: Finding multiple subimages

Posted: 2019-07-14T11:18:41-07:00
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.