Take a monochrome image (black and white) with Android

I would like to take a picture in true black and white in my application. I was looking for solutions (on this site too), but I always found a solution to put the photo on a gray scale (for example, in this section ), but this is not what I am looking for ...

I also found a topic suggesting this:

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 bmOut; } 

But the image quality is terrible ...

Does anyone have any ideas?

thanks

+4
source share
2 answers

If you like the image to be 1 bit black / white, you can use a simple (and slow) threshold algorithm

 public static Bitmap createBlackAndWhite(Bitmap src) { 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); A = Color.alpha(pixel); R = Color.red(pixel); G = Color.green(pixel); B = Color.blue(pixel); int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B); // use 128 as threshold, above -> white, below -> black if (gray > 128) gray = 255; else gray = 0; // set new pixel color to output bitmap bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray)); } } return bmOut; } 

But depending on the fact that it will not look good, for the best results you need a smoothing algorithm, see Overview of the algorithm - this is a threshold method.


For 256 levels of gray conversion:

according to http://www.mathworks.de/help/toolbox/images/ref/rgb2gray.html you calculate the gray value of each pixel as gray = 0.2989 * R + 0.5870 * G + 0.1140 * B , which translates to

 public static Bitmap createGrayscale(Bitmap src) { 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); A = Color.alpha(pixel); R = Color.red(pixel); G = Color.green(pixel); B = Color.blue(pixel); int gray = (int) (0.2989 * R + 0.5870 * G + 0.1140 * B); // set new pixel color to output bitmap bmOut.setPixel(x, y, Color.argb(A, gray, gray, gray)); } } return bmOut; } 

But this is pretty slow, since you have to do this for millions of pixels separately.

fooobar.com/questions/324173 / ... has a much better way to achieve the same.

 // code from that answer put into method from above public static Bitmap createGrayscale(Bitmap src) { int width = src.getWidth(); int height = src.getHeight(); Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmOut); ColorMatrix ma = new ColorMatrix(); ma.setSaturation(0); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(ma)); canvas.drawBitmap(src, 0, 0, paint); return bmOut; } 
+15
source

G = Color.red (pixel);

G = color. green (pixel);

B = Color.red (pixel);

B = Color. blue (pixel);

See if it helps (bold).

+1
source

All Articles