問題描述
Opencv:獲取圖像中的段大小並刪除小段 (Opencv: Get segments sizes in image and remove small segments)
我是 openCV 的新手(在 Android 上),找不到對二進製圖像進行基本分割並獲取片段(帶有黑色像素)大小的方法,然後從給定閾值的圖像中刪除小片段。請注意,我不需要只找到輪廓,我需要獲得完全連接的像素(段)大小。之後過濾小的。
示例圖像在下面
參考解法
方法 1:
I solved it with findContours (thanks to @Micka for pushing me).
Here are the code samples for parts which I needed. 1. I did inversion of binary image, because I needed to get black segments
2. Segmentation:
Imgproc.findContours(mat, contours, mHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
remove small segments
each = contours.iterator(); while (each.hasNext()) { MatOfPoint wrapper = each.next(); double area = Imgproc.contourArea(wrapper); if(area <= lengthThreshold){ each.remove(); } }
4. Draw new image with segments
Imgproc.drawContours(newImg, contours, ‑1, new Scalar(0, 0, 0), 1);
Imgproc.fillPoly(newImg, contours, new Scalar(0, 0, 0));
(by g.hakobyan、g.hakobyan)