Example #1
0
def demo():
    import scipy.cluster.vq as vq

    ## load and show image
    img = Image.open('images/117054.jpg')
    K = 2
    pl.figure(str(K))
    pl.subplot(2, 3, 1)
    pl.imshow(img)
    pl.subplot(2, 3, 4)
    pl.imshow(img)

    ## extract features from image (step size = 7)
    X, L = pa2.getfeatures(img, 7)

    X = vq.whiten(X.T)
    print "Start Learning"
    C, Y = vq.kmeans2(X, K, iter=1000, minit='random')

    Y = Y + 1
    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(2, 3, 2)
    pl.imshow(segm)

    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    pl.subplot(2, 3, 3)
    pl.imshow(csegm)
    Y = GaussianMixture(n_components=K, covariance_type='full').fit(X)
    Y = Y.predict(X) + 1
    # Y = EMGMM.clustering(X, K)
    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(2, 3, 5)
    pl.imshow(segm)

    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    pl.subplot(2, 3, 6)
    pl.imshow(csegm)

    # # Y = MeanShift.clustering((vq.whiten(X.T)).T, 5)
    # Y = MeanShift(bandwidth=0.9).fit((vq.whiten(X.T)))
    # Y = Y.labels_ + 1
    # # make segmentation image from labels
    # segm = pa2.labels2seg(Y,L)
    # pl.subplot(3,3,8)
    # pl.imshow(segm)

    # # color the segmentation image
    # csegm = pa2.colorsegms(segm, img)
    # pl.subplot(3,3,9)
    # pl.imshow(csegm)
    pl.show()
Example #2
0
def main():
    exp_dict = {}

    for data in DATA:

        img = Image.open(os.path.join(IMGPATH, data))
        pl.subplot(1, 3, 1)
        pl.imshow(img)

        X_raw, L = pa2.getfeatures(img, 7)
        X = vq.whiten(X_raw.T)

        for bd in BANDS:

            clf = im.GaussianMeanShift(itera=5, bandwidth=bd)

            clf.fit_x(X)
            clf.cluster()

            Y = clf.get_result() + 1
            segm = pa2.labels2seg(Y, L)
            pl.subplot(1, 3, 2)
            pl.imshow(segm)
            csegm = pa2.colorsegms(segm, img)
            pl.subplot(1, 3, 3)
            pl.imshow(csegm)
            pl.savefig(
                os.path.join(OUTPATH,
                             data + '_GMS_' + str(bd) + '_processed.jpg'))
            pl.show()
    return
Example #3
0
def demo():
    import scipy.cluster.vq as vq

    ## load and show image
    img = Image.open('images/12003.jpg')
    pl.subplot(3,3,1)
    pl.imshow(img)
    pl.subplot(3,3,4)
    pl.imshow(img)
    pl.subplot(3,3,7)
    pl.imshow(img)

    ## extract features from image (step size = 7)
    X,L = pa2.getfeatures(img, 7)

    X = vq.whiten(X.T)

    for i in range(3):
      Y = MeanShift(bandwidth=0.9*(i+1)).fit(X)
      Y = Y.labels_ + 1 # Use matlab 1-index labeling
      # make segmentation image from labels
      segm = pa2.labels2seg(Y,L)
      pl.subplot(3,3,(3 * i + 2))
      pl.imshow(segm)
      
      # color the segmentation image
      csegm = pa2.colorsegms(segm, img)
      pl.subplot(3,3,(3 * i + 3))
      pl.imshow(csegm)
    pl.show()
Example #4
0
def demo():
    import scipy.cluster.vq as vq

    ## load and show image
    img = Image.open('images/12003.jpg')
    pl.subplot(1, 3, 1)
    pl.imshow(img)

    ## extract features from image (step size = 7)
    X, L = pa2.getfeatures(img, 7)

    ## Call kmeans function in scipy.  You need to write this yourself!
    C, Y = vq.kmeans2(vq.whiten(X.T), 2, iter=1000, minit='random')
    Y = Y + 1  # Use matlab 1-index labeling
    ##

    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(1, 3, 2)
    pl.imshow(segm)

    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    pl.subplot(1, 3, 3)
    pl.imshow(csegm)
    pl.show()
Example #5
0
def main():
    exp_dict = {}

    for data2 in DATA:

        img = Image.open(os.path.join(IMGPATH, data2))
        pl.subplot(1, 3, 1)
        pl.imshow(img)

        X_raw, L = pa2.getfeatures(img, 7)
        X = vq.whiten(X_raw.T)

        for bdp in BANDS:
            for bdc in BANDS:
                WKM = im.WeightGMeanshift(itera=5,
                                          chrominance_bandwidth=bdp,
                                          location_bandwidth=bdc)
                WKM.fit_x(X)
                WKM.cluster()
                Y = WKM.get_result() + 1
                segm = pa2.labels2seg(Y, L)
                pl.subplot(1, 3, 2)
                pl.imshow(segm)
                csegm = pa2.colorsegms(segm, img)
                pl.subplot(1, 3, 3)
                pl.imshow(csegm)
                pl.savefig(
                    os.path.join(
                        OUTPATH, data2 + '_WGMS_' + str(bdp) + '_' + str(bdc) +
                        '_processed.jpg'))
                pl.show()
    return
Example #6
0
def demo():
    import scipy.cluster.vq as vq

    ## load and show image
    img = Image.open('../images/12003.jpg')
    pl.subplot(1, 3, 1)
    pl.imshow(img)

    ## extract features from image (step size = 7)
    X, L = pa2.getfeatures(img, 7)

    ## Call kmeans function in scipy.  You need to write this yourself!
    # C,Y = vq.kmeans2(vq.whiten(X.T), 2, iter=1000, minit='random')

    d = 4
    K = 4
    N = X.shape[1]
    Mju = RandomMju(150, 155, K, d)
    ##########Kmeans##########
    #Y = Clustering_Kmeans(Mju, X, K, N, d, 1000, 0.000000000001)

    ##########EM#########
    #d = 4
    #K = 2
    #SIGMA = RandomSIGMA(100, 110, K, d)

    #Pi = [0.1, 0.1, 0.05, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.2]
    #Y = Clustering_EM(Mju, SIGMA, Pi, X, K, d, X.shape[1], 800, 0.00000000000001)
    ###################

    #####Meanshift####
    h = 45

    maxRound = 30
    limitation = 0.00001
    cluster_gap = 8
    X_peak = Peak_MeanShift(X, d, N, h, maxRound, limitation)
    cluster = Clustering_MeanShift(X, X_peak, N, d, cluster_gap)
    Y = cluster[1]
    ##########################

    # Use matlab 1-index labeling

    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(1, 3, 2)
    pl.imshow(segm)

    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    picname = "seastar"
    title = "Menshift, h=" + str(h)
    pl.title(title)
    pl.subplot(1, 3, 3)
    pl.imshow(csegm)
    pl.savefig('/Users/gaobrook/Desktop/Clustering/problem2/' + title + '_' +
               picname + '.png',
               dpi=200)
    pl.show()
Example #7
0
def pic_prod(Y, pic, pic_label):
    Y = Y + 1  # Use matlab 1-index labeling
    ##
    pl.figure(pic)
    pl.subplot(1, 3, 1)
    pl.title('original')
    pl.imshow(img)
    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(1, 3, 2)
    pl.imshow(segm)

    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    pl.title(pic_label)
    pl.subplot(1, 3, 3)
    pl.imshow(csegm)
def main():
	import scipy.cluster.vq as vq
	## load and show image
	img = Image.open('images/resized.jpg')

	pl.subplot(1,3,1)
	pl.imshow(img)
    
	## extract features from image (step size = 7)
	X,L = pa2.getfeatures(img, 9)
	

	X_T = np.transpose(X)
	print(X_T)
	
	"""
	Change the methods to one of them: KMeans(X_T, k), EM_GMM(X_T, k), MeanShift(X_T, h)
	"""
	ms = EM_GMM(X_T, k=5)
	ms.fit()
	Y = ms.evaluate()

	"""
	# Sample code, using the methods in scipy.
	# Call kmeans function in scipy.  You need to write this yourself!
	C,Y = vq.kmeans2 (vq.whiten(X.T), 2, iter=1000, minit='random')
	Y = Y + 1 # Use matlab 1-index labeling
	## 
	"""

	# make segmentation image from labels
	segm = pa2.labels2seg(Y,L)
	pl.subplot(1,3,2)
	pl.imshow(segm)
    
	# color the segmentation image
	csegm = pa2.colorsegms(segm, img)
	pl.subplot(1,3,3)
	pl.imshow(csegm)
	pl.show()
Example #9
0
def main():
    exp_dict = {}

    for data in DATA:

        img = Image.open(os.path.join(IMGPATH, data))
        pl.subplot(1, 3, 1)
        pl.imshow(img)

        X_raw, L = pa2.getfeatures(img, 7)
        X = vq.whiten(X_raw.T)

        for method in METHODS[:2]:
            for k in K:
                if (method == 'KM'):
                    clf = im.Kmeans(k=k)
                if (method == 'EMGMM'):
                    clf = im.EMGMM(k=k)
                clf.fit_x(X)
                clf.cluster()

                Y = clf.get_result() + 1
                segm = pa2.labels2seg(Y, L)
                pl.subplot(1, 3, 2)
                pl.imshow(segm)
                csegm = pa2.colorsegms(segm, img)
                pl.subplot(1, 3, 3)
                pl.imshow(csegm)
                pl.savefig(
                    os.path.join(
                        OUTPATH,
                        data + '_' + method + '_' + str(k) + '_processed.jpg'))
                pl.show()

    for data2 in DATA:

        img = Image.open(os.path.join(IMGPATH, data2))
        pl.subplot(1, 3, 1)
        pl.imshow(img)

        X_raw, L = pa2.getfeatures(img, 7)
        X = vq.whiten(X_raw.T)

        for l in Lambda:
            for k in K:
                WKM = im.WeightedKmeans4D(k=k, Lambda=l)
                WKM.fit_x(X)
                WKM.cluster()
                Y = WKM.get_result() + 1
                segm = pa2.labels2seg(Y, L)
                pl.subplot(1, 3, 2)
                pl.imshow(segm)
                csegm = pa2.colorsegms(segm, img)
                pl.subplot(1, 3, 3)
                pl.imshow(csegm)
                pl.savefig(
                    os.path.join(
                        OUTPATH, data2 + '_WKM_' + str(k) + '_' + str(l) +
                        '_processed.jpg'))
                pl.show()

    return
def demo():
    import scipy.cluster.vq as vq

    ## load and show image
    img = Image.open('../images/12003.jpg')
    pl.subplot(1, 3, 1)
    pl.imshow(img)
    
    ## extract features from image (step size = 7)
    X, L = pa2.getfeatures(img, 7)


    ## Call kmeans function in scipy.  You need to write this yourself!
   # C,Y = vq.kmeans2(vq.whiten(X.T), 2, iter=1000, minit='random')

    d = 4
    K = 4
    N = X.shape[1]
    Mju = RandomMju(150, 155, K, d)
    ##########Kmeans##########
    #Y = Clustering_Kmeans(Mju, X, K, N, d, 1000, 0.000000000001)


     ##########EM#########
    #d = 4
    #K = 2
    #SIGMA = RandomSIGMA(100, 110, K, d)

    #Pi = [0.1, 0.1, 0.05, 0.1, 0.1, 0.1, 0.1, 0.1, 0.05, 0.2]
    #Y = Clustering_EM(Mju, SIGMA, Pi, X, K, d, X.shape[1], 800, 0.00000000000001)
     ###################


    #####Meanshift####
    h = 45

    maxRound = 30
    limitation = 0.00001
    cluster_gap = 8
    X_peak = Peak_MeanShift(X, d, N, h, maxRound, limitation)
    cluster = Clustering_MeanShift(X, X_peak, N, d, cluster_gap)
    Y = cluster[1]
    ##########################



    # Use matlab 1-index labeling

    # make segmentation image from labels
    segm = pa2.labels2seg(Y, L)
    pl.subplot(1, 3, 2)
    pl.imshow(segm)
    
    # color the segmentation image
    csegm = pa2.colorsegms(segm, img)
    picname ="seastar"
    title = "Menshift, h="+str(h)
    pl.title(title)
    pl.subplot(1, 3, 3)
    pl.imshow(csegm)
    pl.savefig('/Users/gaobrook/Desktop/Clustering/problem2/'+title+'_'+picname+'.png', dpi=200)
    pl.show()