我不知道你是否試過這個,但通常情況下,你可以通過首先處理圖像來獲得更好的結果。
1)應用GuassianBlur去除噪聲
2)應用AdaptiveThreshold - >將圖像轉換成黑白
3)應用Dilate操作,以填充裂縫
通過使用AdaptiveThreshold和Dilate操作的不同設置,您可能可以獲得封閉的輪廓...
我使用的示例如下所示:
// 1) Apply gaussian blur to remove noise
Imgproc.GaussianBlur(mGraySubmat, mIntermediateMat, new Size(11,11), 0);
// 2) AdaptiveThreshold -> classify as either black or white
Imgproc.adaptiveThreshold(mIntermediateMat, mIntermediateMat, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY, 5, 2);
// 3) Invert the image -> so most of the image is black
Core.bitwise_not(mIntermediateMat, mIntermediateMat);
// 4) Dilate -> fill the image using the MORPH_DILATE
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_DILATE, new Size(3,3), new Point(1,1));
Imgproc.dilate(mIntermediateMat, mIntermediateMat, kernel);
非常感謝@Entreco的答案。我會嘗試這個並報告。 – gartenabfall
@Entreco我們可以在封閉的輪廓上繪製一個矩形 –
如果黑色矩形對象沒有檢測到邊緣。 –