complex color replacing for map

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
Riticulus

complex color replacing for map

Post by Riticulus »

Good evening..

I hope someone can shed some light at my problem:

The image you see is part of a map which is created by my GIS server. You see different areas seperated by a grey border.
I would like to change the border color to the color of the surronding area. Should a border be between two different colors it doesnt really matter wether it become green or yellow.

At the end I would like not to see any border at all. Is this possible?

I tried to create a very large image before and hoped that after resizing the image to a smaller size would make the border vanish, but it didnt give me any good results.

Has anyone done this before, or could someone push me in the right direction?

before:
Image

how it should look like after the operation:

Image
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: complex color replacing for map

Post by fmw42 »

Note that your histogram shows four colors! There are five odd colored pixels that appear to be more in the green area:

Histogram:
82796: (255,255, 0) #FFFF00 yellow
75818: ( 40,215, 97) #28D761 rgb(40,215,97) <- green
3669: (218,218,218) #DADADA rgb(218,218,218) <- gray roads
5: ( 19,224,124) #13E07C rgb(19,224,124) <- odd colored pixels

Here is one solution:
Change green and yellow to black and white and road gray to middle gray, then average 3x3, then threshold at 50%, then change black and white back to green and yellow

convert sample.gif \
-fill white -opaque yellow \
-fill black -opaque "rgb(40,215,97)" \
-fill black -opaque "rgb(19,224,124)" \
-fill "rgb(128,128,128)" -opaque "rgb(218,218,218)" \
-convolve "1,1,1,1,1,1,1,1,1" -threshold 50% \
-fill yellow -opaque white \
-fill "rgb(40,215,97)" -opaque black \
sample_new.gif
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: complex color replacing for map

Post by anthony »

Note however that this changes each border pixel to the largest number of green or yellow pixels that surround it on a case by case bases.

that is one border pixel could become yellow, while the neighbor border pixel could become green.

It is however probably the best and simplest solution, better than a similar but flawed solution I first thought of.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Riticulus

Re: complex color replacing for map

Post by Riticulus »

wow many thanks to you for your help. I nearly lost hope to find a solution for that. I really do owe you something.

My best wishes from switzerland.
Riticulus

Re: complex color replacing for map

Post by Riticulus »

dear all,

There is one more question regarding this issue i would like to bother you with:

The maps can have up to 5 fill colors:

white
Image
green
Image
yellow
Image
orange
Image
red
Image

so in order to make your sample work, would i assign those colors different shades of grey to make the convolve command work?

many thanks again.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: complex color replacing for map

Post by fmw42 »

I am not sure that will work. You can try it, but then you have multiple thresholds to select. This gets very complicated. The solution I supplied earlier depended upon only two colors plus the gray road. I suspect that you may have to create transparency masks so that you can work on pairs of colors at a time and then repeat for each color combination.

Post an example and if I get some time, I will look at it further.
Riticulus

Re: complex color replacing for map

Post by Riticulus »

I think I found a solution for that:

I produce the image with a width of 3072 pix
After that I am using the -median filter with a radius value of 2. Then I resize the image back to 1024pix.

The result is exactly what i need.

Many thanks again for your time!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: complex color replacing for map

Post by anthony »

That may not produce exact colors, but near ones.

After reducing the image back, use

Code: Select all

   +dither -map colors.gif
where "colors.gif" is a image only containg those 5 colors (only needs to be a 5 pixel image).

That will ensure that colors map back to the nearest given color in the colormap (in RGB color space distances).

See IM Examples, Quantization and Dithering
http://imagemagick.org/Usage/quantize/#map
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply