2

I have a code that turns a RGB bitmap into a bitmap of black and white colors, using this code:

  public static Bitmap setDefaultValues(Bitmap bmp) {

    Mat srcMat = new Mat();
    org.opencv.android.Utils.bitmapToMat(bmp, srcMat, true);


    final Bitmap bitmap = Bitmap.createBitmap(srcMat.clone().width(), srcMat.clone().height(), Bitmap.Config.ARGB_8888);

    Imgproc.cvtColor(srcMat, srcMat, Imgproc.COLOR_BGR2GRAY, 0);

    Mat srcMat1 = srcMat;
    Imgproc.GaussianBlur(srcMat1, srcMat1, new Size(3, 3), 0);

    //Mat srcMat1 = new Mat(srcMat.rows(), srcMat.cols(), CV_8UC1);
    //int kernalsize = 3;
    //Imgproc.bilateralFilter(srcMat, srcMat1, kernalsize, kernalsize * 2, kernalsize / 2);

    srcMat1.convertTo(srcMat1, 0, 1.9, -120);
    srcMat1.convertTo(srcMat1, CvType.CV_8U, 1.9, -120);
    Imgproc.cvtColor(srcMat1, srcMat1, Imgproc.COLOR_GRAY2RGBA, 4);

    org.opencv.android.Utils.matToBitmap(srcMat, bitmap, true);
    return bitmap;

}

I have implement this code for convert RGB image into black and white. this is return me as right, but my question is here i cant remove shadow from image.

also i have compare other application this is convert perfectly, i don't understand where i am wrong.

this is original Image : enter image description here

this is my application output enter image description here

this is other application output enter image description hereenter image description here

So please help me how can i achieve my goal.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Umesh AHIR
  • 738
  • 6
  • 20
  • You don't even need OpenCV. ColorMatrices are very fast. And can easily do GreyScale and Brightness/Contrast operations. – Phantômaxx Oct 13 '17 at 11:07
  • but i want back and White output not GreyScale. also i have upload sample image to want. – Umesh AHIR Oct 13 '17 at 11:10
  • It's the same concept: First greyscale the image, then shoot its brightness and contrast. – Phantômaxx Oct 13 '17 at 11:11
  • I have implement like that but it is return with shadow (means can't remove shadow, as per my requirement) ColorMatrix cm = new ColorMatrix(new float[] { 1.0f, 0, 0, 0, 55.0f, 0, 1.0f, 0, 0, 55.0f, 0, 0, 1.0f, 0, 55.0f, 0, 0, 0, 1.0f, 0 }); Canvas canvas = new Canvas(srcBtm); Paint paint = new Paint(); paint.setColorFilter(new ColorMatrixColorFilter(cm)); canvas.drawBitmap(srcBtm, 0, 0, paint); return srcBtm; – Umesh AHIR Oct 13 '17 at 13:04
  • Executing a single ColorMatrix is not enough. You must run them both, one after another. Do some experiments on which are the best values for boosting the Brightness and Contrast. This will eliminate most of the greys, leaving mostly black and white. Be mindful, though, that you will still **need** at least **some** of the greys, to get an antialiasing effect. Otherwise, the lines will be very jagged. Meaning the diagonal and the curved ones. – Phantômaxx Oct 13 '17 at 13:08
  • This seems a nice job for an [adaptive threshold](https://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga72b913f352e4a1b1b397736707afcde3) – Miki Oct 13 '17 at 14:01

3 Answers3

0

Please use following code for convert your color image to black and white.

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;}
Akshay More
  • 359
  • 2
  • 13
  • 2
    Scanning the image pixel by pixel is **sloooooooooooooooooow**!! Use ColorMatrices, instead. They work on **the whole image at once**, in a single pass. – Phantômaxx Oct 13 '17 at 11:19
  • It take to time for convert, (ex. when i send 3 image it take 10 to 12 seconds) also when we increase contrast values it effect what i want. – Umesh AHIR Oct 13 '17 at 12:41
0

Please try this if you will get the solution

public static Bitmap test(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;
    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;
}
Akshay More
  • 359
  • 2
  • 13
  • (Quality is degrade) this is return me like that.. Link :- https://imgur.com/4FSAb2Z but i want you can see on my question ... – Umesh AHIR Oct 14 '17 at 04:32
0

Please see the answer on this thread. He has explained and provide a good result in output. @Threshold image using opencv (Java)

H Arif
  • 431
  • 4
  • 7