我正在尝试计算此图像中的滴数以及这些滴图所覆盖区域的覆盖百分比。 我试图将这个图像转换成黑色和白色,但这些滴的中心颜色似乎与背景太相似。所以我只有第二张照片。 有什么办法来解决这个问题或任何更好的想法? 非常感谢。如何使用python来计算图像中的对象?
回答
我用下面的代码来检测使用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)
这个想法是隔离背景形式的内部看起来像背景的滴。 因此,我发现连接组件的背景和内部下降采取了最大的连接组件,并将其值改变为像前景值,这留给我一个图像,他内部下降作为一个不同的背景值。 比我用这张图片来填写原始的门槛图像。 最终使用填充图像我计算了相关的值
import cv2
import numpy as np
from matplotlib import pyplot as plt
# Read image
I = cv2.imread('drops.jpg',0);
# Threshold
IThresh = (I>=118).astype(np.uint8)*255
# Remove from the image the biggest conneced componnet
# Find the area of each connected component
connectedComponentProps = cv2.connectedComponentsWithStats(IThresh, 8, cv2.CV_32S)
IThreshOnlyInsideDrops = np.zeros_like(connectedComponentProps[1])
IThreshOnlyInsideDrops = connectedComponentProps[1]
stat = connectedComponentProps[2]
maxArea = 0
for label in range(connectedComponentProps[0]):
cc = stat[label,:]
if cc[cv2.CC_STAT_AREA] > maxArea:
maxArea = cc[cv2.CC_STAT_AREA]
maxIndex = label
# Convert the background value to the foreground value
for label in range(connectedComponentProps[0]):
cc = stat[label,:]
if cc[cv2.CC_STAT_AREA] == maxArea:
IThreshOnlyInsideDrops[IThreshOnlyInsideDrops==label] = 0
else:
IThreshOnlyInsideDrops[IThreshOnlyInsideDrops == label] = 255
# Fill in all the IThreshOnlyInsideDrops as 0 in original IThresh
IThreshFill = IThresh
IThreshFill[IThreshOnlyInsideDrops==255] = 0
IThreshFill = np.logical_not(IThreshFill/255).astype(np.uint8)*255
plt.imshow(IThreshFill)
# Get numberof drops and cover precntage
connectedComponentPropsFinal = cv2.connectedComponentsWithStats(IThreshFill, 8, cv2.CV_32S)
NumberOfDrops = connectedComponentPropsFinal[0]
CoverPresntage = float(np.count_nonzero(IThreshFill==0)/float(IThreshFill.size))
# Print
print "Number of drops = " + str(NumberOfDrops)
print "Cover precntage = " + str(CoverPresntage)
这是什么语言? MATLAB?围绕你的答案的一些背景将会有所帮助。还要记住这个问题是用Python标记的。提出问题的人可能无法使用其他语言的解决方案。 –
我修复它。谢谢你的评论。 –
非常清晰,大大改善。谢谢。 –
您可以填写你的二值图像的使用scipy.ndimage.binary_fill_holes
孔。我还推荐使用自动阈值方法,如Otsu's(可在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()
对于您可以使用scikit-image
from skimage import measure
labels = measure.label(drops)
print(labels.max())
另一个功能下降的数量和覆盖面
print('coverage is %f' %(drops.mean()))
非常感谢您的帮助。 –
- 1. 计算图像中对象的数量
- 2. 计算图像中的对象
- 3. 我们如何使用wso2esb来计算Iterator中的对象
- 4. 使用matlab计算图像中的圆形对象
- 5. Python如何使用osgeo.ogr.Geometry对象计算多边形的周长
- 6. 如何使用python来计算噪点图像暗区的百分比
- 7. 如何使用javascript计算对象值
- 8. 如何在Python中使用图像处理来查找对象的直径?
- 9. 算法来计算图像的代码
- 10. 如何使用python来计算函数中发生的次数
- 11. 如何计算JavaScript中的json对象
- 12. 使用Python中的pyFITS计算图像的均值
- 13. 使用pdfbox计算pdf中的图像
- 14. 如何使用CoreGraphics对图像原始数据进行计算?
- 15. 通过在Python中使用多处理来计算死像素
- 16. 如何扩展一维FFT代码来计算Python中图像(2D)的FFT?
- 17. 如何使用串行对象中的模式来计算字符串?
- 18. Python - 计算图像的直方图
- 19. 如何使用计算列来计算同一视图中的另一列
- 20. 如何使用Python计算图像上的彩色像素区域?
- 21. 在android中如何计算图像中使用openCV的行数
- 22. 如何计算图像中的纹理?
- 23. 如何在Matlab中计算图像中匹配对象的数量
- 24. 使用Python计算图像中两条线之间的夹角
- 25. 如何计算图像
- 26. 如何使用python opencv2减去两个图像来获取前景对象
- 27. 如何使用命令行计算JSON对象中的项目?
- 28. 如何使用MATLAB计算视频中的对象数
- 29. 如何计算类对象在C++中使用的内存量?
- 30. 如何使用OpenCV计算图像中所有点的SURF点?
您可以使用[霍夫圈(HTTP://文档.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight = houghcircles#houghcircles)对于其中大多数 – Pedro