2016-06-25 102 views
0

以下是使用OpenCV4Android在Android应用中检测到的蓝色斑点图像。我用Core.inRange()Imgproc.findContours()方法来查找轮廓,并Imgproc.drawContours()绘制这些:使用OpenCV,如何绘制轮廓内部检测到的形状/斑点的边缘?

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

回答

1

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

0

您可以在drawContour厚度参数中使用一些负值,它会在检测到的物体内绘制轮廓。

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