我想根据给定数量的类的输入训练样本对RGB图像执行像素分类。所以我有4个类包含像素(r,g,b),因此目标是将图像分为四个阶段。Python中的最大似然像素分类opencv
我发现python opencv2具有可以完成这项工作的期望最大化算法。但不幸的是,我没有找到任何教程或材料可以解释我(因为我是初学者)如何使用该算法。
你能否提出任何一种可以作为出发点的教程?
更新...的另一种方法为以下代码:
**def getsamples(img):
x, y, z = img.shape
samples = np.empty([x * y, z])
index = 0
for i in range(x):
for j in range(y):
samples[index] = img[i, j]
index += 1
return samples
def EMSegmentation(img, no_of_clusters=2):
output = img.copy()
colors = np.array([[0, 11, 111], [22, 22, 22]])
samples = getsamples(img)
#em = cv2.ml.EM_create()
em = cv2.EM(no_of_clusters)
#em.setClustersNumber(no_of_clusters)
#em.trainEM(samples)
em.train(samples)
x, y, z = img.shape
index = 0
for i in range(x):
for j in range(y):
result = em.predict(samples[index])[0][1]
#print(result)
output[i][j] = colors[result]
index = index + 1
return output
img = cv2.imread('00.jpg')
smallImg = small = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
output = EMSegmentation(img)
smallOutput = cv2.resize(output, (0,0), fx=0.5, fy=0.5)
cv2.imshow('image', smallImg)
cv2.imshow('EM', smallOutput)
cv2.waitKey(0)
cv2.destroyAllWindows()**
简而言之:ģO_O GLE。长:谷歌“期望最大化算法”。一旦你有足够的关于这个概念的信息,谷歌这个:“期望最大化算法opencv”。请注意,opencv是一个库,而不是一本书。您需要研究研究论文,书籍,维基或任何您觉得最适合的来源的技术。 downvote的原因: - 我在第一次尝试中找到了多个教程。如果仍然有问题,谷歌YouTube的相同,你会发现相同的视频教程。 – saurabheights
搜索opencv 2.4.x和3.0之间的等效函数,对于我的代码使用来源:https://subokita.com/2014/03/24/image-segmentation-using-opencvs-expectation-maximization/ – eyllanesc