Page 1 of 2

Replace color but only in some regions

Posted: 2018-10-18T13:53:00-07:00
by cgkas
Hi, as part of another project I ask if is possible the following.

In this image below you see a green arc. Well, the 2 black rectangles and the small red rectangle have a red line across them. The lines and the arc have the same type of green.
Image


I ask if is possible to replace only the color of the arc from green to purple and the green lines within the 3 rectangles remain green like this:
Image

Thanks in advance for any help.

Re: Replace color but only in some regions

Posted: 2018-10-18T14:16:49-07:00
by Bonzo
Is this an actual image you are going to be using? Sometimes users post an image and the forum users reply and in fact it is nothing like the actual image the OP is going to use. This means that time is wasted with incorrect answers.

Are the other images you are going to use similar?

If this is the actual image and all the others are the same you could crop the top half, modify the colour then paste it back together again; but as you can guess this will not be the same for an image where the green arc was at the bottom.

Re: Replace color but only in some regions

Posted: 2018-10-18T14:44:56-07:00
by cgkas
Hi Bonzo,

The green areas are not only on the top. The green lines I want to isolate have 1 pixel heigth. If there is way to identify these lines to change the colors only tho the lines to diferentiate from the rest of green areas, or replace colors of green areas except the 1 pixel lines.

A more representative input image is this:

Image

and output in this case like this:
Image

Thanks for any help.

Re: Replace color but only in some regions

Posted: 2018-10-18T15:17:09-07:00
by snibgo
Your second image has a PNG extension but is really a JPEG. This is lossy, which means some of your "green" pixels are really a different colour. Please don't post PNG images that are really JPEG. It confuses everyone.

What do you want to do about the antialiasing around the green arcs?

A way to approach the problem is:

1. Convert green pixels to white, and everything else to black.

2. From that, turn black all white pixels that have a black pixel above and below.

3. The result is now white where we want to replace green with purple, and black where we want no change.

4. So, we make an image with this change (or simply make a purple image) and do a masked composite over the input.

Re: Replace color but only in some regions

Posted: 2018-10-19T09:07:29-07:00
by cgkas
Hi snibgo,

Thanks for the ideas.

I was able to reach the step 3 of your suggestion, changing to purple only those regions I want and rest is black.

Now from here how can I do the mask to mix this purple and black image with the original input?

This is what I have so far:

Code: Select all

areas=$(convert input.png     \
-transparent '#4CFE00' \
-alpha extract -negate \
-threshold 50% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
out.png | awk '/x1\+/{print $2}')

for region in $areas; do
    convert out.png -region $region -fill '#000000' -colorize 100 out.png
done

convert out.png -fill "#7F006E" -opaque white out.png

Re: Replace color but only in some regions

Posted: 2018-10-19T09:39:22-07:00
by snibgo
Please post a link to input.png, and ensure it really is a PNG, not a JPEG.

Re: Replace color but only in some regions

Posted: 2018-10-19T10:07:13-07:00
by cgkas
I have saved as PNG. How can be sure not a JPEG?

Re: Replace color but only in some regions

Posted: 2018-10-19T11:00:00-07:00
by fmw42
you can use

Code: Select all

identify -verbose image.png
it will tell you the proper format.

Or use dropbox.com, which does not change your image format.

Re: Replace color but only in some regions

Posted: 2018-10-19T12:04:30-07:00
by cgkas
Attaching again input.png from Dropbox. I hope this time has the correct format.

Image

Re: Replace color but only in some regions

Posted: 2018-10-19T12:12:45-07:00
by fmw42
That new link does download a PNG image.

Re: Replace color but only in some regions

Posted: 2018-10-19T12:22:10-07:00
by cgkas
Attached again

Re: Replace color but only in some regions

Posted: 2018-10-19T12:30:51-07:00
by fmw42
Yes that is PNG also.

Re: Replace color but only in some regions

Posted: 2018-10-19T12:41:13-07:00
by cgkas
Yes. Snibgo told me to attach the input PNG ensuring that was not a JPEG. I'm confused what you need.

Re: Replace color but only in some regions

Posted: 2018-10-19T12:44:20-07:00
by fmw42
I think that was all he needed so that he or you could properly use his code. But I will defer to him.

Re: Replace color but only in some regions

Posted: 2018-10-19T13:39:55-07:00
by cgkas
I've been able to do the first 3 steps snibgo suggested. Now, I dont know how to do the last step. A masked composite over the input.

A way to approach the problem is:
1. Convert green pixels to white, and everything else to black.
2. From that, turn black all white pixels that have a black pixel above and below.
3. The result is now white where we want to replace green with purple, and black where we want no change.
4. So, we make an image with this change (or simply make a purple image) and do a masked composite over the input.