使用 OpenCV,如何在检测到的 shape/blob 的边缘内绘制轮廓?

Using OpenCV, how to draw contours INSIDE the edges of detected shape/blob?

以下是使用 OpenCV4Android 在 Android 应用中检测到的蓝色斑点图像。我用Core.inRange() and Imgproc.findContours() methods to find the contours, and Imgproc.drawContours()画了他们:

Mat mask = new Mat();
Core.inRange(rgbaMat, lowerThreshold, upperThreshold, mask);
...
contours = new ArrayList<>();
Imgproc.findContours(dilatedMat, contours, new Mat(), Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
...
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ ) { 
    Imgproc.drawContours ( rgbaMat, contours, contourIdx, new Scalar(0, 255, 0), 1);
}

轮廓(浅绿色边界)在检测到的形状之外。

因此,如您所见,它还包括检测到的蓝色斑点周围的一些白色区域。我希望轮廓边界位于蓝色 blob/shape 的边缘内。

我该怎么做?

我可以在您的代码中看到您将 "findContours" 应用于变量 "dilatedMat"(第 5 行)。我假设您在 "rgbaMat" 上应用了 "dilate" 过滤器(第 3 行的某处)。但是如果你想让轮廓在里面,你应该应用 "erode" 而不是 "dilate".

您可以在 drawContour thickness 参数中使用一些负值,这将在检测到的对象内部绘制轮廓。

Imgproc.drawContours ( rgbaMat, contours, contourIdx, new Scalar(0, 255, 0), -1);