3

I have an ImageView loading a bitmap. In this bitmap, I want to replace all pixels of a given color with another color. I've searched extensively for this, but most answers seem to either refer to using a standard ColorFilter (which applies the tint to all pixels) or manually looping through all pixels and replacing the color if it matches a given input color.

Is there really not a more standardized way of applying a filter only to a given single color? Or creating a mask from a bitmap and a given single color and then apply the filter using that mask? I would be very surprised if I'd had to manually mutate the bitmap and write code to alter every pixel!? Note that I also want to make sure antialiasing etc is not destroyed. When using standard ColorFilter/android:tint, antialiasing in rounded corners etc is preserved. I simply want to apply the filter selectively for a given color or range of colors. Is there any API for this?

JHH
  • 8,567
  • 8
  • 47
  • 91

1 Answers1

0

Open CV for Android seems like something you need: https://opencv.org/platforms/android/

Here you can find examples of finding pixels of given color: OpenCV Android Green Color Detection

If your bitmap is some sort of real photo and you are looking for something that will catch some part of color spectre than OpenCV is probably the best choice.

muminers
  • 1,190
  • 10
  • 19
  • Thanks. Actually, I don't need to support any photo; it's sufficient if I can replace e.g. all white portions of an icon that contains a few distinct colors (while still preserving anti-aliasing). As an example, imagine an image containing a white shape with some grey text and some green symbols on it - I want to be able to replace white with an arbitrary color while not affecting the grey or green. – JHH Mar 19 '18 at 20:57
  • Well if we are talking about very simple picture than I don't know any better solution than something like this: https://xjaphx.wordpress.com/2011/09/28/image-processing-pixel-color-replacement/ Which is iterating thourgh pixels and changing their color manually as you descibed it. – muminers Mar 19 '18 at 21:01
  • Yeah, I have a feeling that will create severe edge artifacts though. I was hoping there was a standard Android API that I had overlooked to produce a mask for a given color, with a given tolerance, but it seems not. – JHH Mar 19 '18 at 22:39
  • At least nothing I know. And yes, such simple solution will not deal in any acceptable manner with shades or anything like this. But openCV does. Choice is yours – muminers Mar 19 '18 at 22:43