7

I want to replace the particular color of an image with other user selected color. While replacing color of image, I want to maintain the gradient effect of that original color. for example see the attached images. image1.png
image2.png

I have tried to do so with CoreGraphics & I got success to replace color. But the replacing color do not maintain the gradient effect of the original color in the image.

Can someone help me on this? Is the CoreGraphics is right way to do this? Thanks in advance.

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
  • 2
    Can you post the code that you've got so far please? – sam-w Jan 29 '13 at 11:48
  • you might need to look for a specific ratio of rgb instead of a specific colour, then replace all pixels that have a ratio of rgb similar to the target colour's ratio. would need a bit of a threshold on the ratio. never tried this but this is how i would tackle it – Fonix Jan 29 '13 at 11:51
  • 1
    Have a look at this question.. This may help you. http://stackoverflow.com/questions/8046643/how-to-change-a-particular-color-in-an-image – Aravindhan Jan 29 '13 at 11:52
  • 1
    It is not easy to do it without user intervention (i.e., no region delimitation and others), and the question mentioned above is very weak in that regard. At http://stackoverflow.com/a/14307006/1832154 there is a quickly discussed method to do it in an automatic form, and the resulting image is mostly acceptable. Also, note that in `image2.png`, one of the lamps turned green. – mmgp Jan 29 '13 at 13:25
  • I'm not sure how well this will work when the color you want to filter out/replace is white, but the new and improved Core Image Programming Guide includes an example of solving a pretty similar problem: http://developer.apple.com/library/mac/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_filer_recipes/ci_filter_recipes.html#//apple_ref/doc/uid/TP30001185-CH4-SW1 – Peter Hosey Jan 29 '13 at 18:24
  • 1
    @PeterHosey - I converted their original chroma keying example into a generic OpenGL ES shader: http://stackoverflow.com/a/12847774/19679 and it does tend to freak out around white values. It also either does or does not match the color, but I could see modifying it to replace chrominance while maintaining luminance. – Brad Larson Jan 29 '13 at 18:41
  • @er-mayur-sharma Can you please paste the code snippet with which you have replaced the color like above? – sach Jan 26 '17 at 07:03

2 Answers2

1

After some struggling almost with the same problem (but with NSImage), made a category for replacing colors in NSImage which uses ColorCube CIFilter.

https://github.com/braginets/NSImage-replace-color

inspired by this code for UIImage (also uses CIColorCube): https://github.com/vhbit/ColorCubeSample

Eugene Braginets
  • 856
  • 6
  • 16
-2

I do a lot of color transfer/blend/replacement/swapping between images in my projects and have found the following publications very useful, both by Erik Reinhard:

Unfortunately I can't post any source code (or images) right now because the results are being submitted to an upcoming conference, but I have implemented variations of the above algorithms with very pleasing results. I'm sure with some tweaks (and a bit of patience) you might be able to get what you're after!

EDIT:

Furthermore, the real challenge will lie in separating the different picture elements (e.g. isolating the wall). This is not unlike Photoshop's magic wand tool which obviously requires a lot of processing power and complex algorithms (and is still not perfect).

Ricardo RendonCepeda
  • 3,271
  • 4
  • 23
  • 30