<p>只要你修复了你的光照(下面列出的方法1),你就可以摆脱阈值,如果没有,你可能需要一个简单的分类器方法(例如聚类技术,方法2)结合连接的组件和假设植物的位置或颜色来将检测到的类分配给植物。你知道吗</p>
<pre><code>from scipy.misc import imread
import matplotlib.pyplot as plt
import matplotlib.patches as patches
%matplotlib inline
import matplotlib
import numpy as np
# read the image
img = imread('9v5wv.png')
# show the image
fig,ax = plt.subplots(1)
ax.imshow(img)
ax.grid('off')
# show the r,g,b channels separately.
for n,d in enumerate([('r',0),('g',1),('b',2)]):
k,v = d
plt.figure(n)
plt.subplot(131)
plt.imshow(arr[:,:,v],cmap='gray')
plt.grid('off')
plt.title(k)
plt.subplot(133)
_=plt.hist(arr[:,:,v].ravel(),bins=100)
# method 1, rgb thresholding will not work when lighting changes
arr = img
r_filter = lambda x: x[:,:,0] < 100
g_filter = lambda x: x[:,:,1] > 80
b_filter = lambda x: x[:,:,2] < 200
mask=np.logical_and(np.logical_and(r_filter(arr),g_filter(arr)),b_filter(arr))
plt.imshow(mask,cmap='gray')
plt.grid('off')
</code></pre>
<p><a href="https://i.stack.imgur.com/4jGui.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4jGui.png" alt="enter image description here"/></a></p>
<pre><code># method 2, kmeans clustering
from sklearn.cluster import KMeans
arr = matplotlib.colors.rgb_to_hsv(img[:,:,0:3])
# ignore v per Yves Daoust
data = np.array(arr[:,:,0:2])
x,y,z = data.shape
X = np.reshape(data,(x*y,z))
kmeans = KMeans(n_clusters=6, random_state=420).fit(X)
mask = np.reshape(kmeans.labels_,(x,y,))
plt.imshow(mask==0,cmap='gray')
plt.grid('off')
</code></pre>
<p><a href="https://i.stack.imgur.com/XPLGK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XPLGK.png" alt="enter image description here"/></a></p>