Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android using drawContours to fill region

It must be simple to find contours and fill them with specific color.I want to find contours in this simple image:

image description

So I used this snippet of code(here iv is an instance of ImageView and bmp is a Bitmap created from above image):

Mat src = new Mat();
Utils.bitmapToMat(bmp, src);
Imgproc.cvtColor(src, src, Imgproc.COLOR_RGBA2GRAY);

Imgproc.Canny(src, src, 50, 200);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat hierarchy = new Mat();
// find contours:
Imgproc.findContours(src, contours, hierarchy, Imgproc.RETR_TREE,Imgproc.CHAIN_APPROX_SIMPLE);
for (int contourIdx = 0; contourIdx < contours.size(); contourIdx++) {
    Imgproc.drawContours(src, contours, contourIdx, new Scalar(0, 0, 255), -1);
}
// create a blank temp bitmap:
Bitmap tempBmp1 = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),
bmp.getConfig());

Utils.matToBitmap(src, tempBmp1);
iv.setImageBitmap(tempBmp1);

But the result is a totally black image like this:

image description

Did I do any thing wrong?