Beispiel #1
0
def MyFCM(img, ImageType, numClust):
    k = numClust
    num_features = img.shape[2]
    img_vector = np.zeros([img.shape[0] * img.shape[1], num_features])
    img_pixel = np.zeros([img.shape[0] * img.shape[1], 2], int)
    count = 0
    for i in xrange(0, img.shape[0]):
        for j in xrange(0, img.shape[1]):
            img_vector[count] = img[i][j]
            img_pixel[count] = np.array([i, j])
            count += 1
    num_samples = img_vector.shape[0]
    num_features = img_vector.shape[1]
    img_vector_T = img_vector.T
    cntr, u, u0, d, jm, p, fpc = fuzz.cmeans(img_vector_T,
                                             k,
                                             2.,
                                             error=0.05,
                                             maxiter=20)
    ClusterIm = np.zeros([img.shape[0], img.shape[1]], int)
    uT = u.T
    for i in xrange(0, num_samples):
        row = img_pixel[i][0]
        col = img_pixel[i][1]
        max_val = 0.
        max_cluster = 0
        for j in xrange(0, k):
            if uT[i][j] > max_val:
                max_val = uT[i][j]
                max_cluster = j + 1
        ClusterIm[row][col] = max_cluster

    ccImOneBase = getCCIM.getCCIM(ClusterIm, 4)
    return ClusterIm, ccImOneBase
Beispiel #2
0
def MyKmeans05(img1,ImageType,numClust):

    #normalizing image data
    img2 = img1.astype(float)
    img = img2 / 255

    #diff constants for use
    num_features = img.shape[2]
    num_samples=img.shape[0]*img.shape[1]
    img_height = img.shape[0]
    img_width = img.shape[1]

    #reshape img
    img_vector = np.reshape(img,(num_samples,num_features))

    #for hyper simply change the img_vector after pca with 3 dimensions
    if(ImageType=='Hyper'):
        pca = PCA(3)
        principalComponents = pca.fit_transform(img_vector)
        img_vector = principalComponents

    kmeans= sklearn.cluster.KMeans(n_clusters=numClust,init="k-means++",max_iter=300).fit(img_vector)
    ClusterIm = np.reshape(kmeans.labels_,(img_height,img_width))
    ccImOneBase = getCCIM.getCCIM(ClusterIm, 4)
    return ClusterIm, ccImOneBase
Beispiel #3
0
def MyKmeans5(img, ImageType, numClust):
    num_features = img.shape[2]
    k = numClust
    img_vector = np.zeros([img.shape[0] * img.shape[1], num_features])
    img_pixel = np.zeros([img.shape[0] * img.shape[1], 2], int)
    count = 0
    for i in xrange(0, img.shape[0]):
        for j in xrange(0, img.shape[1]):
            img_vector[count] = img[i][j]
            img_pixel[count] = np.array([i, j])
            count += 1
    num_samples = img_vector.shape[0]
    img_vector_T = img_vector.T

    kmeans = sklearn.cluster.KMeans(n_clusters=k,
                                    init="k-means++",
                                    max_iter=300).fit(img_vector)
    uT = kmeans.labels_
    ClusterIm = np.zeros([img.shape[0], img.shape[1]], int)
    for i in xrange(0, num_samples):
        row = img_pixel[i][0]
        col = img_pixel[i][1]
        ClusterIm[row][col] = uT[i] + 1

    ccImOneBase = getCCIM.getCCIM(ClusterIm, 4)
    return ClusterIm, ccImOneBase
Beispiel #4
0
def MySOM05(img1, ImageType, numClust):

    #normalizing image data
    img2 = img1.astype(float)
    img = img2 / 255

    #diff constants for use
    num_features = img.shape[2]
    num_samples = img.shape[0] * img.shape[1]
    img_height = img.shape[0]
    img_width = img.shape[1]

    #reshape img
    img_vector = np.reshape(img, (num_samples, num_features))

    #for hyper simply change the img_vector after pca with 3 dimensions
    if (ImageType == 'Hyper'):
        pca = PCA(3)
        principalComponents = pca.fit_transform(img_vector)
        img_vector = principalComponents
    #map sizeis 25X25
    N = 25
    som = SOM((N, N), img_vector)
    som.set_parameter(neighbor=0.1, learning_rate=0.2)
    output_map = som.train(100000)

    # print output_map.shape
    somMap = output_map.reshape([output_map.shape[0] * output_map.shape[1], 3])

    # kmeans to find new centroids
    kmeans2 = sklearn.cluster.KMeans(n_clusters=numClust,
                                     init="k-means++",
                                     max_iter=10).fit(somMap)
    uT = kmeans2.labels_
    new_centroids = kmeans2.cluster_centers_

    # use new_centroids for kmeans of 1 iteration only to find final output
    kmeans3 = sklearn.cluster.KMeans(n_clusters=numClust,
                                     init=new_centroids,
                                     n_init=1,
                                     max_iter=300).fit(img_vector)

    ClusterIm = np.reshape(kmeans3.labels_, (img_height, img_width))
    ccImOneBase = getCCIM.getCCIM(ClusterIm, 4)
    return ClusterIm, ccImOneBase
Beispiel #5
0
def MyFCM05(img1, ImageType, numClust):
    #normalizing image data
    img2 = img1.astype(float)
    img = img2 / 255

    #diff constants for use
    num_features = img.shape[2]
    num_samples = img.shape[0] * img.shape[1]
    img_height = img.shape[0]
    img_width = img.shape[1]

    #reshape img
    img_vector = np.reshape(img, (num_samples, num_features))
    img_vector_T = img_vector.T
    #for hyper simply change the img_vector after pca with 3 dimensions
    if (ImageType == 'Hyper'):
        pca = PCA(3)
        principalComponents = pca.fit_transform(img_vector)
        img_vector = principalComponents
    cntr, u, u0, d, jm, p, fpc = fuzz.cmeans(img_vector_T,
                                             numClust,
                                             2.,
                                             error=0.05,
                                             maxiter=20)
    uT = u.T
    output = np.zeros(num_samples)
    for i in xrange(0, num_samples):
        max_val = 0.
        max_cluster = 0
        for j in xrange(0, numClust):
            if uT[i][j] > max_val:
                max_val = uT[i][j]
                max_cluster = j + 1
        output[i] = max_cluster
    ClusterIm = np.reshape(output, (img_height, img_width))
    ClusterIm = ClusterIm.astype(int)
    #     imgplot = plt.imshow(img)
    #     plt.pause(2)
    #     plt.imshow(ClusterIm)
    #     plt.pause(5)

    ccImOneBase = getCCIM.getCCIM(ClusterIm, 4)
    return ClusterIm, ccImOneBase