Skip to main content
edited tags
Link
Jeru Luke
  • 21.3k
  • 13
  • 83
  • 91
specify tags
Link
Andrii Omelchenko
  • 13.4k
  • 12
  • 46
  • 83
format code
Source Link
Andrii Omelchenko
  • 13.4k
  • 12
  • 46
  • 83

How do i draw contours on only black object, and filling everything else in the background white? My code currently is able to draw contours on an image:

Bitmap b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
            srcMat= new Mat();
            Utils.bitmapToMat(b,srcMat);
            
            Mat gray = new Mat();
            Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
            
            Imgproc.Canny(gray, gray, 20, 20*3, 3, true);
            List<MatOfPoint> contours = new ArrayList<>();
            Mat hierarchy = new Mat();
             
Imgproc.findContours(gray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
                Imgproc.drawContours(srcMat, contours, contourIdx, new Scalar(0, 0, 255), -1);

 
            }
             
Utils.matToBitmap(gray, b);
 
            imgR.setImageBitmap(b);

How do i draw contours on only black object, and filling everything else in the background white? My code currently is able to draw contours on an image:

Bitmap b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
            srcMat= new Mat();
            Utils.bitmapToMat(b,srcMat);
            
            Mat gray = new Mat();
            Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
            
            Imgproc.Canny(gray, gray, 20, 20*3, 3, true);
            List<MatOfPoint> contours = new ArrayList<>();
            Mat hierarchy = new Mat();
            Imgproc.findContours(gray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
            for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
                Imgproc.drawContours(srcMat, contours, contourIdx, new Scalar(0, 0, 255), -1);

 
            }
            Utils.matToBitmap(gray, b);
 
            imgR.setImageBitmap(b);

How do i draw contours on only black object, and filling everything else in the background white? My code currently is able to draw contours on an image:

Bitmap b = BitmapFactory.decodeByteArray(getIntent().getByteArrayExtra("byteArray"),0,getIntent().getByteArrayExtra("byteArray").length);
srcMat= new Mat();
Utils.bitmapToMat(b,srcMat);
            
Mat gray = new Mat();
Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
            
Imgproc.Canny(gray, gray, 20, 20*3, 3, true);
List<MatOfPoint> contours = new ArrayList<>();
Mat hierarchy = new Mat();
 
Imgproc.findContours(gray,contours,hierarchy,Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
    Imgproc.drawContours(srcMat, contours, contourIdx, new Scalar(0, 0, 255), -1);
}
 
Utils.matToBitmap(gray, b);
imgR.setImageBitmap(b);
Source Link
Tix
  • 449
  • 1
  • 8
  • 27
Loading