2

On this similar thread they have been proposed solutions to convert the background color of some image to transparent.
But sometimes the background is a simple pattern, like in this case:

Square background pattern

Note the square background pattern.

When processing images, the background does often need to be removed or changed, but firstly you need to detect it (i.e: change its color, or making it transparent). For example, on the above image example, I would like to obtain:

Pattern changed to yellow

How can I detect/change a specified pattern inside an image?

GUI solutions accepted.
Open source solutions preferred (free at least required). The simplest solution will be preferred (I would like to avoid installing some hundreds of MB program).

Note: I was thinking about posting this question at Photography StackExchange site, but I would rather say the mind of a programmer (I could need to edit dozens of such images) is more close to what I want. I am not an artist.

Community
  • 1
  • 1
Sopalajo de Arrierez
  • 3,543
  • 4
  • 34
  • 52

1 Answers1

2

This is not a fully developed answer, but it does go some way towards thinking about a method - maybe someone else would like to develop it...

If, as you say, your pattern is specified, you know what it is - good, aren't I? So, you could look for the "Minimum Repeating Unit" of your pattern in your image. In your case it is a 16x16 grid like this:

enter image description here

Now you can search for that pattern in your image. I am using ImageMagick at the command-line, but you can use other tools if you prefer. ImageMagick is installed on most Linux distros and is available for OSX and Windows for free. So, I search for that grid in your globe image and ImageMagick gives me an output image showing white dots at the top-left corner of every location where the two images match:

compare -metric ae -dissimilarity-threshold 1.0 -compose src -subimage-search globe.gif grid.png res.png

That gets me this in file res-1.png

enter image description here

Now the white dots are where the 16x16 "Minimum Repeating Unit" is found in the image, but at the top-left corner so I shift them right and down by 8 pixels to the centre of the matching area, then I create a new output image where each pixel is the maximum pixel of the 16x16 grid in which it existed before:

convert res-1.png -roll +8+8 -statistic maximum 16x16  z.png

enter image description here

I can now invert that mask and then use it to set the opacity of the original image, thereby blanking areas that matched the "Minimum Repeating Unit":

convert globe.gif \( z.png -negate \) -compose copy_opacity -composite q.png

enter image description here

No, it's not perfect, but it is an idea for an approach that could be refined...

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432