我正在嘗試計算此圖像中的滴數以及這些滴圖所覆蓋區域的覆蓋百分比。 我試圖將這個圖像轉換成黑色和白色,但這些滴的中心顏色似乎與背景太相似。所以我只有第二張照片。 有什麼辦法來解決這個問題或任何更好的想法? 非常感謝。如何使用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. 使用matlab計算圖像中的圓形對象
- 4. 如何使用python來計算噪點圖像暗區的百分比
- 5. 如何使用javascript計算對象值
- 6. 如何在Python中使用圖像處理來查找對象的直徑?
- 7. 使用pdfbox計算pdf中的圖像
- 8. 如何計算JavaScript中的json對象
- 9. 如何使用CoreGraphics對圖像原始數據進行計算?
- 10. 通過在Python中使用多處理來計算死像素
- 11. 如何擴展一維FFT代碼來計算Python中圖像(2D)的FFT?
- 12. 如何使用串行對象中的模式來計算字符串?
- 13. 如何計算圖像
- 14. 如何使用MATLAB計算圖像中的人數?
- 15. 如何使用OpenCV計算圖像中所有點的SURF點?
- 16. 如何在javascript中計算對象
- 17. 如何從方法中返回一個對象來計算它?
- 18. 如何免除Matlab繪圖極限計算中的對象
- 19. 圖像中的Tensorflow計數對象
- 20. 有效計算圖像python的方差
- 21. 如何使用global_step來計算對tf.cond調用的謂詞?
- 22. 使用Javascript圖像對象來處理圖像
- 23. 使用OpenCV計算對象的面積
- 24. 使用類和對象來計算學生的GPA
- 25. 使用類來計算平均值python
- 26. 使用Python來計算長序列
- 27. 如何通過使用django-hitcount計算的流行度來返回對象
- 28. 從python中提取圖像的對象
- 29. Python:分類圖像中的對象
- 30. 如何使用計算的值來計算另一個
您可以使用[霍夫圈(HTTP://文檔.opencv.org/2.4/modules/imgproc/doc/feature_detection.html?highlight = houghcircles#houghcircles)對於其中大多數 – Pedro