Change color of some regions

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
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Change color of some regions

Post by cgkas »

Hello to all,

I have an image like this (I get this image applying convert input.png -alpha extract -negate out.png):

Image

I would like
1- to change the internal black squares to red
2- change the white rectangles to red. These white rectangles have some pixels in black inside.

The output I´m looking for is like this:

Image

Is there a way to make this?

Thanks in advance for any help.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of some regions

Post by snibgo »

Please, always tell us the version of IM you use, on what platform.

I suggest you use a floodfill. See http://www.imagemagick.org/script/comma ... #floodfill
snibgo's IM pages: im.snibgo.com
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Change color of some regions

Post by cgkas »

Hello snibgo,

Thanks for answer.

IM version is 6.9.10-11 Q16 x86_64.

I didn't say in my original post but I'd like to change to red those regions automatically, if possible, without say the x,y position, because my final goal is to be able to get the coordinates of the red regions.

Thanks again.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of some regions

Post by snibgo »

What other constraints are there on these images?

For this image, "-connected-components" can be used to find the bounding boxes and centroids.

Code: Select all

magick small.png -threshold 50% -define connected-components:verbose=true -define connected-components:area-threshold=10 -connected-components 4 NULL:

Objects (id: bounding-box centroid area mean-color):
  0: 30x30+0+0 14.6,15.1 509 srgb(0,0,0)
  12: 10x9+3+14 7.5,18.0 90 srgb(0,0,0)
  13: 10x9+17+14 21.5,18.0 90 srgb(0,0,0)
  1: 12x5+2+2 7.5,4.0 57 srgb(255,255,255)
  2: 11x5+16+2 21.0,4.0 49 srgb(255,255,255)
  10: 12x14+2+10 7.5,17.1 48 srgb(255,255,255)
  11: 12x14+16+10 21.5,17.1 48 srgb(255,255,255)
  4: 1x2+19+3 19.0,3.5 2 srgb(0,0,0)
  6: 2x1+8+4 8.5,4.0 2 srgb(0,0,0)
  9: 1x1+23+5 23.0,5.0 1 srgb(0,0,0)
  8: 1x1+20+5 20.0,5.0 1 srgb(0,0,0)
  5: 1x1+22+3 22.0,3.0 1 srgb(0,0,0)
  3: 1x1+7+3 7.0,3.0 1 srgb(0,0,0)
  7: 1x1+21+4 21.0,4.0 1 srgb(0,0,0)
srgb(0,0,0) is black and srgb(255,255,255) is white.

The two black entries size 90 pixels, and the two largest white entries, are the ones you want.
snibgo's IM pages: im.snibgo.com
cgkas
Posts: 42
Joined: 2018-10-10T23:36:52-07:00
Authentication code: 1152

Re: Change color of some regions

Post by cgkas »

Excellent snibgo. Thanks so much.

Actually I tested in real image that have several colors (not only black and white) and it seems to print the info for the squares I want to isolate.

It seems your code sees only white and black, having this I don't need 2 extra commands I sent previously to convert the image in black and white only.

The only thing I see is the command "magick" doesn't work for me (command not found). I tried with convert intead of magick and it works but the print is shorter.

Code: Select all

$ convert small.png -threshold 50% -define connected-components:verbose=true -define connected-components:area-threshold=10 -connected-components 4 NULL:
Objects (id: bounding-box centroid area mean-color):
  0: 30x30+0+0 14.6,15.1 509 srgb(0,0,0)
  13: 10x9+17+14 21.5,18.0 90 srgb(0,0,0)
  12: 10x9+3+14 7.5,18.0 90 srgb(0,0,0)
  1: 12x5+2+2 7.5,4.0 60 srgb(255,255,255)
  2: 11x5+16+2 21.0,4.0 55 srgb(255,255,255)
  10: 12x14+2+10 7.5,17.1 48 srgb(255,255,255)
  11: 12x14+16+10 21.5,17.1 48 srgb(255,255,255)
One question, since this is a part of a bigger script. Do you know if this command will work with RMagick and its equivalent?
Or would be more compatible some solution with python than Ruby?

Thanks
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Change color of some regions

Post by snibgo »

Sorry, I forgot you are using v6. Yes, the v6 program is "convert".

I know nothing about Ruby or RMagick, and only a little Python. All my IM work is either command-line or C.
snibgo's IM pages: im.snibgo.com
Post Reply