Home > Tutorials > Image Processing – Contrast Image On The Fly

Image Processing – Contrast Image On The Fly


Please refer to these Wiki pages on Contrast images to get the concepts and ideas: Contrast(Vision) | Image Editing – Contrast

From the original image:

Original Image

Original Image

I take contrast on this image:

Contrast = 100

Contrast = 100

Contrast = 50

Contrast = 50

First, calculate the contrast value: contrast = ((value + 100) / 100) ^ 2 (square by 2, or power 2).

Then do this for each color channel:

+ Take pixel color divided by 255

+ Minus 0.5

+ Multiply contrast value calculated above.

+ Plus 0.5

+ Multiply by 255

Here the implementation:

	public static Bitmap createContrast(Bitmap src, double value) {
		// 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;
		// get contrast value
		double contrast = Math.pow((100 + value) / 100, 2);

		// 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);
				A = Color.alpha(pixel);
				// apply filter contrast for every channel R, G, B
				R = Color.red(pixel);
				R = (int)(((((R / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
				if(R < 0) { R = 0; }
				else if(R > 255) { R = 255; }

				G = Color.red(pixel);
				G = (int)(((((G / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
				if(G < 0) { G = 0; }
				else if(G > 255) { G = 255; }

				B = Color.red(pixel);
				B = (int)(((((B / 255.0) - 0.5) * contrast) + 0.5) * 255.0);
				if(B < 0) { B = 0; }
				else if(B > 255) { B = 255; }

				// set new pixel color to output bitmap
				bmOut.setPixel(x, y, Color.argb(A, R, G, B));
			}
		}

		// return final image
		return bmOut;
	}

Hope you enjoy it!

 

Cheers,
Pete Houston

  1. elvin
    May 8, 2014 at 5:17 pm

    thanks your tutorial is awesome! just one question, may i know what’s the ‘value’ u mean there?

  2. parin
    February 8, 2014 at 7:17 pm

    i cant get any effect in my program.why? please help me.

  3. nameless
    August 17, 2012 at 6:54 am

    Why the result is black and white?

    • mo
      August 17, 2012 at 8:14 am

      Because it assigns red color’s value to green and blue.
      G = Color.red(pixel); should be G = Color.green(pixel); and also
      B = Color.red(pixel); should be B = Color.blue(pixel);
      In addition to that calling pixel = src.getPixel(x, y); in a loop is too expensive. You should use getPixels funstion instead. It will increase the performance marginally.

  4. Noe Rodriguez
    June 14, 2012 at 3:54 am

    Hello! I’m learning a lot of all your tutorials, thanks for posting them. I have a question about this one, Why the result of the image comes in B/W?

    • Brill
      June 14, 2012 at 10:45 am

      Well Noe, I think the author is forcing it to be black and white, the values of G and B, are Color.red(pixel); if you want a contrast with colors just change to .green() and .blue() respectively.

  5. pavan
    January 30, 2012 at 3:29 pm

    Thanks nice tutorial.
    What is is difference between “contrast” and “Contrast Image On The Fly” ?
    I could see value range for contrast is -1 and 1 and effect is different as comapred with contrast image on fly.

    Is there any other name for this “contrast image on fly”?

  6. Muhammad faisal
    December 1, 2011 at 3:29 am

    Good Sample…. thanks

  7. luqutu
    June 22, 2011 at 9:43 am

    nice sample!

  8. June 22, 2011 at 8:50 am

    @Thiago: what do you mean by toycam? Don’t really get what you mean …._ _!

  9. Thiago
    June 22, 2011 at 2:18 am

    Hey, do you think on doing a tutorial for toycam results? I am really enjoying your tutorials on how to apply these effects!

  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: