Home > Tutorials > Image Processing – Filter Color Channels

Image Processing – Filter Color Channels


Sometimes, you want to filter the color on the image, in which to display image by only one channel or combine the increase on every channel.

Using my original beauty:

Original Image

Original Image

Now, I convert it into every single channel:

Filter Image: Red = 100%

Filter Image: Red = 100%

Filter Image: Green = 100%

Filter Image: Green = 100%

Filter Image: Blue = 100%

Filter Image: Blue = 100%

Those image are filtered by using only one channel. What if I combine all channels?

FIlter Image: Red = Green = Blue = 50%

FIlter Image: Red = Green = Blue = 50%

Image looks a bit darker since the intensity decreases. If I try to increase to some level above normal,

FIlter Image: Red = Green = Blue = 150%

FIlter Image: Red = Green = Blue = 150%

Well, it’s weird in some way, the channels mixed and messed-up!

Anyway, the implementation is rather simpler than you think it would be:

	public static Bitmap doColorFilter(Bitmap src, double red, double green, double blue) {
		// image size
		int width = src.getWidth();
		int height = src.getHeight();
		// create output bitmap
		Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
		// color information
		int A, R, G, B;
		int pixel;

		// scan through all pixels
		for(int x = 0; x < width; ++x) {
			for(int y = 0; y < height; ++y) {
				// get pixel color
				pixel = src.getPixel(x, y);
				// apply filtering on each channel R, G, B
				A = Color.alpha(pixel);
				R = (int)(Color.red(pixel) * red);
				G = (int)(Color.green(pixel) * green);
				B = (int)(Color.blue(pixel) * blue);
				// set new color pixel to output bitmap
				bmOut.setPixel(x, y, Color.argb(A, R, G, B));
			}
		}

		// return final image
		return bmOut;
	}
}

 

Hope you like it!

 

Cheers,
Pete Houston

Advertisements
  1. July 8, 2016 at 4:07 pm

    Sorry, but how do you assign the image to ImageView?
    I’ve tried this but it doesn’t work 😦

    final ImageView demoImg = (ImageView)findViewById(R.id.demoImg);
    demoImg.setImageResource(R.drawable.demo);

    Button grayButt = (Button)findViewById(R.id.grayButt);
    grayButt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final Bitmap bm = BitmapFactory.decodeResource(Home.this.getResources(), R.drawable.demo);
    doGreyscale(bm);
    //demoImg.setImageBitmap(bm);
    }
    });

  2. August 17, 2013 at 1:08 pm

    There is a color filter in Android API, that should probably run a lot faster then your embedded loops. Take a look at http://developer.android.com/reference/android/graphics/ColorMatrixColorFilter.html to get to the next level 🙂

  3. raj mathauda
    April 22, 2013 at 4:38 pm

    plz…….give the full coding……….for color effects

  4. raj mathauda
    April 20, 2013 at 7:02 pm

    plz…show full example of color effects…….

  5. March 7, 2013 at 8:21 pm

    I also got confused at the beginning… 🙂
    100% – means 1

    So, if you would like to have Red color effect, you should send to the function:
    doColorFilter(bmpSource, 1, 0, 0);

    and for example, the last “crazy” effect – red=green=blue=150%, you should do:
    doColorFilter(bmpSource, 1.5, 1.5, 1.5);

    and so on…

    Best luck!

  6. February 6, 2013 at 7:11 pm

    How to get the last effect?Atleast tell what that means red=green=blue=150%
    what will be the value for red,green,blue

  7. Zala JanakSinh
    November 7, 2012 at 2:05 pm

    Please Reply me it`s urgent.

  8. Zala JanakSinh
    November 7, 2012 at 2:04 pm

    Same Question as a Kalpesh ask what the min and max value of rgb.

  9. Kalpesh
    August 20, 2012 at 2:11 pm

    Kalpesh :
    Here what is min and max value of red, green and blue…?…..It’s between [0,255]…?

  10. Kalpesh
    August 20, 2012 at 2:10 pm

    Here what is min and max value of red, green and blue…?…..It’s between [0,255]…?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: