首页 文章

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

提问于
浏览
0

以下是使用 OpenCV4Android 在Android应用中检测到的蓝色blob的图像 . 我使用Core.inRange()Imgproc.findContours()方法查找轮廓,并使用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);
}

轮廓(浅绿色边界)位于检测到的形状之外 .

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

我怎样才能做到这一点?

enter image description here

2 回答

  • 0

    我可以在你的代码中看到你在变量"dilatedMat"(第5行)上应用"findContours" . 我假设你在"rgbaMat"(第3行的某个地方)应用了"dilate"过滤器 . 但是如果你想要轮廓在里面,你应该应用 "erode" instead of "dilate" .

  • 1

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

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

相关问题