1

How can I change hue of an UIImage programmatically only in few parts? I have followed this link

How to programmatically change the hue of UIImage?

enter image description here

and used the same code in my application. It's working fine but the complete image hue is getting changed. According to my requirement I want to change only the tree color in the above snap. How can I do that?

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
Dee
  • 1,887
  • 19
  • 47

2 Answers2

1

This is a specific case of a more general problem of using masking. I assume you have some way of knowing what pixels are in the "tree" part, and which ones are not. (If not, that's a whole other question/problem).

If so, first draw the original to the result context, then create a mask (see here: http://mobiledevelopertips.com/cocoa/how-to-mask-an-image.html), and draw the changed-hue version with the mask representing the tree active.

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • I don't have an idea on which image (tree) will come next. So I don't have an idea on pixels. In this case how can I change only the hue color of a tree? – Dee Aug 13 '12 at 18:37
  • Suppose if I take human face as another example how can I change only his/her skin color? – Dee Aug 13 '12 at 18:39
  • I suppose you can use statistics to find the most dominant color and apply the hue change on some range of colors/ or on everything but the dominant range of colors – Kaan Dedeoglu Aug 13 '12 at 18:47
  • 2
    @Dee just as quixoto mentioned, finding specific regions of a picture to be manipulated indeed can be an extremely difficult problem to solve. Sometimes, in simple cases a flood fill alike algorithm for finding the pixels to be masked is fine - often enough that wont do. Your problem boils down into at least two subproblems. 1. create a mask (needs to find the areas to be manipulated), 2. change hue of unmasked part (you solved that one already - at least almost). – Till Aug 13 '12 at 18:48
0

I recommend you take a look at the CoreImage API and the CIColorCube or CIColorMap filter in particular. Now how to define the color cube or color map is where the real magic lies. You'll need to transform tree tones (browns, etc), though this will obviously transform all browns, not just your tree.

CSmith
  • 13,318
  • 3
  • 39
  • 42