示例#1
0
import matplotlib.pyplot as plt
import GMM
import numpy as np

DEBUG = True
#Y=np.loadtxt("GMMData.txt")
Y = np.loadtxt("kmeanstestset.txt")
matY = np.matrix(Y, copy=True)

K = 4  #k为分类的数量
mu, cov, alpha = GMM.GMM_EM(matY, K, 100)
N = Y.shape[0]
gamma = GMM.expectation(matY, mu, cov, alpha)
category = gamma.argmax(axis=1).flatten().tolist()[0]
class0 = np.array([Y[i] for i in range(N) if category[i] == 0])
class1 = np.array([Y[i] for i in range(N) if category[i] == 1])
class2 = np.array([Y[i] for i in range(N) if category[i] == 2])
class3 = np.array([Y[i] for i in range(N) if category[i] == 3])

plt.plot(class0[:, 0], class0[:, 1], 'rs', label="class0")
plt.plot(class1[:, 0], class1[:, 1], 'bo', label="class1")
plt.plot(class2[:, 0], class2[:, 1], 'gs', label="class2")
plt.plot(class3[:, 0], class3[:, 1], 'ko', label="class3")

plt.legend(loc="best")
plt.title("GMM")
plt.savefig("666", dpi=600)
plt.show()
示例#2
0
    data = []
    img = image.open(f)
    m, n = img.size
    for i in range(m):
        for j in range(n):
            x= img.getpixel( (i, j) )
           # print(x[0])
            data.append( [x[0] / 256.0] ) #只有一维的灰度值

    f.close()
    return np.mat(data), m, n

matY, row, col = loadData('1.jpg') #1.jpg是彩色图片
#matY, row, col = greyData('3.jpg')#3.jpg是灰度图片
K=3
mu,cov,alpha=GMM.GMM_EM(matY,K,20) #20代表EM算法迭代的次数

N=matY.shape[0]
gamma=GMM.expectation(matY,mu,cov,alpha)

category=gamma.argmax(axis=1).flatten().tolist()[0]
category=np.array(category)
category = category.reshape( [row, col] )

print(category)
pic_new = image.new( 'L', (row, col) )

print(pic_new)

for i in range(row):
    for j in range(col):