Home > Tutorials > Image Processing – Black Filter (Increasing the Darkness)

Image Processing – Black Filter (Increasing the Darkness)


Base on randomizing image pixels, another filter is created, named “Black Filter“, or enhance the noise of darkness.

Original Image

Original Image

Image w/ Black Filter

Image w/ Black Filter

It looks like ink-dropping, isn’t it?

Basically, the algorithm is to generate a threshold number (0-255), if all R,G,B values of a pixel are less than the threshold, then set the pixel to black.

	public static Bitmap applyBlackFilter(Bitmap source) {
		// get image size
		int width = source.getWidth();
		int height = source.getHeight();
		int[] pixels = new int[width * height];
		// get pixel array from source
		source.getPixels(pixels, 0, width, 0, 0, width, height);
		// random object
		Random random = new Random();

		int R, G, B, index = 0, thresHold = 0;
		// iteration through pixels
		for(int y = 0; y < height; ++y) {
			for(int x = 0; x < width; ++x) {
				// get current index in 2D-matrix
				index = y * width + x;
				// get color
				R = Color.red(pixels[index]);
				G = Color.green(pixels[index]);
				B = Color.blue(pixels[index]);
				// generate threshold
				thresHold = random.nextInt(COLOR_MAX);
				if(R < thresHold && G < thresHold && B < thresHold) {
					pixels[index] = Color.rgb(COLOR_MIN, COLOR_MIN, COLOR_MIN);
				}
			}
		}
		// output bitmap
		Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
		return bmOut;
	}

Cheers,
Pete Houston

Advertisements
Categories: Tutorials Tags: , , , ,
  1. Sam
    May 7, 2012 at 2:36 pm

    Pete,

    Do you have any tutorial regarding “Cartoon effect on photo”?

  2. Sam
    May 4, 2012 at 6:39 pm

    Hi Pete,

    In above code what are the values of COLOR_MIN, COLOR_MAX.

    • May 5, 2012 at 1:35 pm

      Hi Sam,
      min = 0x00, max = 0xFF

      • Sam
        May 7, 2012 at 1:24 pm

        Thanks Pete,

  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: