Android、OpenCV:检测颜色和绘制轮廓
Android, OpenCV: Detect color & Draw Contours
如何只在黑色物体上绘制轮廓,而将背景中的其他所有物体填充为白色?
我的代码目前能够在图像上绘制轮廓:
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);
您应该创建并应用遮罩,就像回答 this 问题一样。例如,您可以通过这种方式执行此操作(在您的 Imgproc.findContours()
调用而不是 for (int contourIdx = ...
:
之后插入下面的代码
// create Mat for mask
Mat mask = new Mat(new Size(srcMat.cols(), srcMat.rows() ), CvType.CV_8UC1);
mask.setTo(new Scalar(0.0));
// create Scalar for color of mask objects
Scalar white = new Scalar(255, 255, 255);
// draw contours border and fill them
Imgproc.drawContours(mask, contours, -1, white, 10);
for (MatOfPoint contour: contours) {
Imgproc.fillPoly(mask, Arrays.asList(contour), white);
}
// create mat foe masked image
Mat masked = new Mat();
// apply mask to srcMat and set result to masked
srcMat.copyTo(masked, mask);
然后将 Utils.matToBitmap()
调用中的 gray
垫更改为 masked
:
Utils.matToBitmap(masked, b);
imgR.setImageBitmap(b);
如何只在黑色物体上绘制轮廓,而将背景中的其他所有物体填充为白色? 我的代码目前能够在图像上绘制轮廓:
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);
您应该创建并应用遮罩,就像回答 this 问题一样。例如,您可以通过这种方式执行此操作(在您的 Imgproc.findContours()
调用而不是 for (int contourIdx = ...
:
// create Mat for mask
Mat mask = new Mat(new Size(srcMat.cols(), srcMat.rows() ), CvType.CV_8UC1);
mask.setTo(new Scalar(0.0));
// create Scalar for color of mask objects
Scalar white = new Scalar(255, 255, 255);
// draw contours border and fill them
Imgproc.drawContours(mask, contours, -1, white, 10);
for (MatOfPoint contour: contours) {
Imgproc.fillPoly(mask, Arrays.asList(contour), white);
}
// create mat foe masked image
Mat masked = new Mat();
// apply mask to srcMat and set result to masked
srcMat.copyTo(masked, mask);
然后将 Utils.matToBitmap()
调用中的 gray
垫更改为 masked
:
Utils.matToBitmap(masked, b);
imgR.setImageBitmap(b);