Message Boards Message Boards

Convert image from one color to another?

Posted 8 years ago

I would like to convert an RGB image to a different color. I know how to convert to grayscale using ColorConvert, but I can't figure out how to change it to a custom RGB color.

enter image description here

I'm assuming it's fairly straightforward, and I'm just missing something. Thanks in advance.

POSTED BY: Bryan Lettner
6 Replies
Posted 8 years ago

Wow Vitaliy, great stuff. I'm going to be using all of the above techniques to post some wicked images soon!

POSTED BY: Bryan Lettner

@Kuba Podkalicki and @Marco Thiel gave excellent answers. I'd like to make a short note. When you would like to change the color you basically asking for a function mapping from existing RGB channels' values to new ones. The most general function that does it is ImageApply. Look through numerous examples there. You can apply function directly to the channels vector {R,G,B} or different or mixed functions of separate channels. My favorite example is selective-coloring or color-splashing. Imagine you have an image:

enter image description here

I will import it back in WL and convert everything to grayscale but the most striking red:

img = Import["https://wolfr.am/c6vProOq"];
ImageApply[If[#[[1]] > #[[2]] + #[[3]], #, ConstantArray[Mean[#], 3]] &, img]

enter image description here

Of course you can act on all channels simultaneously too, - and going back to summer:

ImageApply[RotateRight, img] 

enter image description here

Another neat way is color transfer, see docs:

enter image description here

POSTED BY: Vitaliy Kaurov

Hi Bryan,

I am not sure whether I can answer that question. In your image there are many different shades of colours. If you save that image in the variable "img" you get:

DominantColors[img, 10]

enter image description here

I can of course use one of the colours and substitute it by another RGB colour:

ColorReplace[img, DominantColors[img][[-1]] -> RGBColor[1, 0, 0]]

enter image description here

If you have a large range of colours and you want to change them to different RGB combinations, you obviously need some kind of rule to convert all colours.

Image[ImageData[img] /. {r_, g_, b_} /; {r, b, g} != {1, 1, 1} -> 0.75 Norm[{r, g, b}]*{0.99, 0.9, 0.7}]

enter image description here

It's probably not what you want, but might point in a direction.

Cheers,

M.

POSTED BY: Marco Thiel
Posted 8 years ago

Marco this is a great solution for me. Much appreciated!

POSTED BY: Bryan Lettner
pic = Import@
  "http://community.wolfram.com//c/portal/getImageAttachment?filename=\
multseg1-8combinedwithreflection250pixel.jpg&userId=167076";

Maybe it can be done quicker, don't know :)

Colorize[
   ColorConvert[pic, "GrayScale"], 
   ColorFunction -> (Blend[{Red, White}, #] &)
]

enter image description here

POSTED BY: Kuba Podkalicki
Posted 8 years ago

Very helpful, Thank you Kuba!

POSTED BY: Bryan Lettner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract