JIA*_*ANG 4 python image-processing
我正在尝试计算此图像中的墨滴数量以及这些墨滴所覆盖区域的覆盖率.我试图将这个图像转换为黑白图像,但这些图像的中心颜色看起来与背景太相似了.所以我只得到第二张图片.有没有办法解决这个问题或任何更好的想法?非常感谢.


小智 7
您可以使用来填充二进制图像的孔scipy.ndimage.binary_fill_holes。我还建议使用自动阈值方法,例如Otsu(在中可用scikit-image)。
from skimage import io, filters
from scipy import ndimage
import matplotlib.pyplot as plt
im = io.imread('ba3g0.jpg', as_grey=True)
val = filters.threshold_otsu(im)
drops = ndimage.binary_fill_holes(im < val)
plt.imshow(drops, cmap='gray')
plt.show()
Run Code Online (Sandbox Code Playgroud)
对于滴数,您可以使用另一个功能 scikit-image
from skimage import measure
labels = measure.label(drops)
print(labels.max())
Run Code Online (Sandbox Code Playgroud)
而对于覆盖
print('coverage is %f' %(drops.mean()))
Run Code Online (Sandbox Code Playgroud)
我使用以下代码使用openCV和python检测图像中的轮廓数.
img = cv2.imread('ba3g0.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,1)
contours,h = cv2.findContours(thresh,1,2)
for cnt in contours:
cv2.drawContours(img,[cnt],0,(0,0,255),1)
Run Code Online (Sandbox Code Playgroud)
为了更好地去除另一个轮廓内的轮廓,您需要遍历整个列表并比较和删除内部轮廓.之后,"轮廓"的大小将给你计数