如何计算阈值化后检测到的形状数量

How to calculate the number of shapes detected after thresholding

我使用 java OpenCV 对我的图像进行了颜色分割,阈值图像可以显示为图像 1 :

我想计算阈值图像中的白点数。我研究了 findcontour() 函数并尝试获取白点的计数。但是我失败了。请帮我。我的代码在这里。

Imgproc.findContours(destination, contours, hierarchy,
         Imgproc.RETR_EXTERNAL,Imgproc.CHAIN_APPROX_SIMPLE);
for(int j=0;j<contours.size();j++){
  sum=sum+contours.size();
}
System.out.println("Sum"+sum);

我应用了形态学操作。

使用此代码:opening = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)

kernel 是一个由 1 组成的 5x5 数组。

我得到了这个:

Jeru 对于这个案例的回答是正确的。如果你遇到噪声较大的情况,形态学操作无法将它们消除,你可以使用轮廓大小进行截断,例如

for contour in contours
    if cv2.contourArea(contour) > minimal_length

计数前