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:
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:
Did I do any thing wrong?