Archive

Posts Tagged ‘filter’

Image Processing – Hue Filter

October 30, 2011 Leave a comment

The same concept of the Saturation Filter but apply for Hue.

Original Image

Original Image

Image w/ Hue Filter (Level 3)

Image w/ Hue Filter (Level 3)

Image w/ Hue Filter (Level 9)

Image w/ Hue Filter (Level 9)

Here the implementation:

	public static Bitmap applyHueFilter(Bitmap source, int level) {
		// get image size
		int width = source.getWidth();
		int height = source.getHeight();
		int[] pixels = new int[width * height];
		float[] HSV = new float[3];
		// get pixel array from source
		source.getPixels(pixels, 0, width, 0, 0, width, height);
		
		int index = 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;				
				// convert to HSV
				Color.colorToHSV(pixels[index], HSV);
				// increase Saturation level
				HSV[0] *= level;
				HSV[0] = (float) Math.max(0.0, Math.min(HSV[0], 360.0));
				// take color back
				pixels[index] |= Color.HSVToColor(HSV);
			}
		}
		// 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: , , ,

Image Processing – Saturation Filter

October 30, 2011 4 comments

By converting RGB to HSV, we can adjust pixel color in different way, in this case, I’d like to filter only Saturation value, I guess this technique is called “Saturation Filter“.

http://en.wikipedia.org/wiki/HSL_and_HSV#Saturation

Original Image

Original Image

Image w/ Saturation Filter (Level 1)

Image w/ Saturation Filter (Level 1)

It’s kinda similar to some technique of color enhancement, but I’m not really sure.

Here the implementation:

	public static Bitmap applySaturationFilter(Bitmap source, int level) {
		// get image size
		int width = source.getWidth();
		int height = source.getHeight();
		int[] pixels = new int[width * height];
		float[] HSV = new float[3];
		// get pixel array from source
		source.getPixels(pixels, 0, width, 0, 0, width, height);

		int index = 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;
				// convert to HSV
				Color.colorToHSV(pixels[index], HSV);
				// increase Saturation level
				HSV[1] *= level;
				HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
				// take color back
				pixels[index] |= Color.HSVToColor(HSV);
			}
		}
		// 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

Categories: Tutorials Tags: , , ,

Image Processing – Shading Filter

October 30, 2011 10 comments

Shading Filter” is a technique that use AND operator for the current pixel color and a desired-shading color.

Original Image

Original Image

Image w/ Shading (Sky Blue)

Image w/ Shading (Sky Blue)

Image /w Shading (Lawn Green)

Image /w Shading (Lawn Green)

Image w/ Shading (Violet)

Image w/ Shading (Violet)

I love her in Violet

Here the implementation:

	public static Bitmap applyShadingFilter(Bitmap source, int shadingColor) {
		// 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);

		int index = 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;
				// AND
				pixels[index] &= shadingColor;
			}
		}
		// 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

Categories: Tutorials Tags: , , ,

Image Processing – Black Filter (Increasing the Darkness)

October 30, 2011 4 comments

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

Categories: Tutorials Tags: , , , ,

Image Processing – Filter Color Channels

June 21, 2011 10 comments

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