1
$\begingroup$

Suppose i have a image and I want to convert the portion of image that is red to blue(or any other color). One way to do this is extract red part of image using HSV or other color values and convert red hsv value to corresponding blue. Is there any other methods(better) for this. What sort of algorithms do Photoshop color replacement tool use?

$\endgroup$
  • 1
    $\begingroup$ Are you looking to replace regions of pure red with pure blue? Or are you looking to change the red component of each colour to blue (so an orange region becomes cyan)? Somewhere in between these two approaches, you could identify regions of the image that are "sufficiently red" and only apply the change to those regions. If you edit the question to describe more specifically what you require, we will be better able to suggest algorithms. $\endgroup$ – trichoplax Apr 18 '16 at 9:33
  • $\begingroup$ I want to change pure red with pure blue maintaing the lightning and shades of color $\endgroup$ – Sandiip Apr 18 '16 at 9:40
  • $\begingroup$ This sounds like you want to change more than just one precise colour. In this picture of an apple would you want all of the reddish parts to become bluish? Or would you want only the very reddest parts to become blue, and the rest of the reddish parts to remain reddish? $\endgroup$ – trichoplax Apr 18 '16 at 11:13
  • $\begingroup$ i want to all red to become blue? for both wouldn't the algorithm differ only in the threshold taken for red $\endgroup$ – Sandiip Apr 18 '16 at 11:33
  • $\begingroup$ Do you have an example before and after image? $\endgroup$ – trichoplax Apr 18 '16 at 13:31
4
$\begingroup$

In order to replace a color with another color, you need some sort of distance metric between the colors and a function for calculating the blending based on that distance.

Finding a distance between 2 colors can be tricky. RGB is a bad color space to measure the distance between colors for perceptual uses. HSV or YCbCr are better.

Once you have the colors in a color space you like, you need to decide how to calculate the distance. I generally calculate the hue distance for starters. If the hue of the color I'm considering replacing is within +/- some threshold (possibly set by the user), then I check to see if the saturation and value are within a certain range, too.

Once I've found a color that's within the ranges specified, I calculate a blending amount. Usually this will be 100% if the color in the image exactly matches the color I want to replace, and will fall off to 0 at the thresholds of hue, saturation and brightness.

You then need to calculate the color you're going to use as a replacement color. It's useful to offset the hue, saturation and value (or Y, Cb, and Cr components) by the same amount as the color in the image differs from the replacement color.

So now you have the original color in the image, the color to replace it with, and a blending amount. You still need to figure out which blend mode you want to use. I would start with what Photoshop calls "Normal" or "Over". That would be a linear mix of the two. If you have colors A and B with a mix amount of 25%, it would be calculated like this:

newColor = A * .25 + B * (1.0 - .25);

Here are some relevant answers I gave to similar questions on Stack Overflow:

How to Change the Hue of a Texture with GLSL

Better Image Coloring Logic/Algorithm

$\endgroup$
2
$\begingroup$

Assuming you are looking to transform a particular component (r, g or b) of any color, this can be easily done by simple vector/matrix multiplication.

The introduction on this page (Chapter 6.1) - from the most excellent immersivemath.com site - illustrates this perfectly: Chapter 6: The Matrix (Immersive Linear Algebra) | immersivemath.com

Think of it as the "Channel Mixer" command in PhotoShop.

edit

What I described above is one of many ways of changing a given color. The PS Color Replacement Tool simply applies that to any pixel that matches certain conditions defined by the user (source color, destination color, tolerance, etc...). The nifty thing they did is put it in the form of a brush tool, allowing the user artistic control over where to apply the color transform.

Basically it boils down to this: if a pixel under the brush matches the conditions set by the user the color transform is applied. If not, leave the pixel as is.

$\endgroup$
  • $\begingroup$ What I am trying to do is Color Replacement Tool in Photoshop. I donot know what Channel Mixer does. I am trying to replicate the Color Replacement Tool. $\endgroup$ – Sandiip Apr 19 '16 at 3:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.