OpenCV (android) : convert colored image to black and white

asked 2019-12-23 00:43:40 -0500

Akhil Patel gravatar image

updated 2019-12-23 00:55:07 -0500

I am trying to convert colored image to black & white using adaptive threshold. Is it correct way to convert colored image to black & white if not than please guide me.

Here is my code :-

       BitmapFactory.Options options = new BitmapFactory.Options();
       options.inScaled = false; // Leaving it to true enlarges the decoded image size.
       Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.part3, options);

       Mat adaptiveTh = new Mat();
       Utils.bitmapToMat(original, adaptiveTh);
       Imgproc.cvtColor(adaptiveTh, adaptiveTh, Imgproc.COLOR_BGR2GRAY);

       Imgproc.medianBlur(adaptiveTh, adaptiveTh, 15);

       Imgproc.adaptiveThreshold(adaptiveTh, adaptiveTh, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, 
       Imgproc.THRESH_BINARY, 9, 2);

       Bitmap imgBitmap = Bitmap.createBitmap(adaptiveTh.cols(), adaptiveTh.rows(),                     
       Bitmap.Config.ARGB_8888);
       Utils.matToBitmap(adaptiveTh, imgBitmap);

       ImageView imageView = findViewById(R.id.opencvImg);
       imageView.setImageBitmap(imgBitmap);
       saveBitmap(imgBitmap, "adaptive_threshold");
edit retag flag offensive close merge delete