如何仅在黑色物体上绘制轮廓,并在背景白色中填充其他所有内容?我的代码目前能够在图像上绘制轮廓:
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);
1 回答
你应该创建并应用掩码,就像回答this问题一样 . 例如,您可以通过这种方式执行此操作(在
Imgproc.findContours()
调用之后插入代码而不是for (int contourIdx = ...
:然后在
Utils.matToBitmap()
调用masked
中更改gray
mat: