Esempio n. 1
0
def save_vis(filename_list, number_list, pred_pca, output_hcluster, output_PCA):

    imlist = filename_list
    imnbr = len(imlist)

    # Load images, run PCA.
    immatrix = array(pred_pca)
    V, S, immean = pca.pca(immatrix)

    # Project on 2 PCs.
    projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)])

    # height and width
    h, w = 1200, 1200
    
    # create a new image with a white background
    img = Image.new('RGB', (w, h), (255, 255, 255))
    draw = ImageDraw.Draw(img)

    # draw axis
    draw.line((0, h/2, w, h/2), fill=(255, 0, 0))
    draw.line((w/2, 0, w/2, h), fill=(255, 0, 0))

    # scale coordinates to fit
    scale = abs(projected).max(0)
    scaled = floor(array([(p/scale) * (w/2 - 20, h/2 - 20) + (w/2, h/2)
                          for p in projected])).astype(int)

    # paste thumbnail of each image
    for i in range(imnbr):
        nodeim = Image.open(imlist[i])
        nodeim.thumbnail((25, 25))
        ns = nodeim.size
        box = (scaled[i][0] - ns[0] // 2, scaled[i][1] - ns[1] // 2,
             scaled[i][0] + ns[0] // 2 + 1, scaled[i][1] + ns[1] // 2 + 1)
        img.paste(nodeim, box)

    tree = hcluster.hcluster(projected)
    hcluster.draw_dendrogram(tree,imlist,filename=output_hcluster)

    for i, num in enumerate(number_list):
        if num < 8:
            color1 = mod(370*num,255)
            color2 = mod(170*num,255)
            color3 = mod(270*num,255)
            draw.text((scaled[i][0],scaled[i][1]),str(num),fill = (color1,color2,color3))
        else:
            draw.text((scaled[i][0],scaled[i][1]),str(num),fill = (255,0,0))

    figure()
    imshow(img)
    axis('off')
    img.save(output_PCA)
    show()
Esempio n. 2
0
#    
#    clusters = tree.extract_clusters(5)
#    
#    print len(clusters)
#    for c in clusters:
#        print c.get_cluster_elements()
    path = r'D:\Python_Computer_Vision\data\sunsets\flickr-sunsets-small'
    imlist= [ os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')]
    
    features = zeros([len(imlist),512])
    for i,f in enumerate(imlist):
        im=array(Image.open(f))
        h,edges=histogramdd(im.reshape(-1,3), 8, normed=True, range=[(0,255),(0,255),(0,255)])
        features[i]=h.flatten()
        
    tree = hcluster.hcluster(features)
    
    hcluster.draw_dendrogram(tree, imlist, filename='sunset.pdf')
    
    #visualize clusters with some threshold
    clusters = tree.extract_clusters(0.23* tree.distance)
    
    for c in clusters:
        elements = c.get_cluster_elements()
        nbr_elements = len(elements)
        if nbr_elements>3:
            figure()
            for p in range(minimum(nbr_elements,20)):
                subplot(4,5,p+1)
                im = array(Image.open(imlist[elements[p]]))
                imshow(im)
Esempio n. 3
0
#img_path = 'C:/chromium/src/autoxd3/python/cnn_boll/img_labels/imgs/'
img_path = 'img_labels/imgs/'
# create a list of images
path = img_path
imlist = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.png')]
imlist = agl.array_shuffle(imlist)
imlist = imlist[:2000]
# extract feature vector (8 bins per color channel)
features = zeros([len(imlist), 512])
for i, f in enumerate(imlist):
    im = array(Image.open(f))
    # multi-dimensional histogram
    h, edges = histogramdd(im.reshape(-1, 3), 8, normed=True, range=[(0, 255), (0, 255), (0, 255)])
    features[i] = h.flatten()
tree = hcluster.hcluster(features)

# visualize clusters with some (arbitrary) threshold
clusters = tree.extract_clusters(img_distance * tree.distance)
base_dir = 'datas/' 
# plot images for clusters with more than 3 elements
for i,c in enumerate(clusters):
    elements = c.get_cluster_elements()
    nbr_elements = len(elements)
    if nbr_elements >= clusters_num:
        for e in elements:
            print(imlist[e], i, c.get_depth(), c.get_height())
            dst = base_dir + str(i)
            if not os.path.isdir(dst):
                os.makedirs(dst)
            src = imlist[e]
Esempio n. 4
0
# create a new image with a white background
img = Image.new('RGB', (w, h), (255, 255, 255))
draw = ImageDraw.Draw(img)

# draw axis
draw.line((0, h/2, w, h/2), fill=(255, 0, 0))
draw.line((w/2, 0, w/2, h), fill=(255, 0, 0))

# scale coordinates to fit
scale = abs(projected).max(0)
scaled = floor(array([(p/scale) * (w/2 - 20, h/2 - 20) + (w/2, h/2)
                      for p in projected])).astype(int)

# paste thumbnail of each image
for i in range(imnbr):
  nodeim = Image.open(imlist[i])
  nodeim.thumbnail((25, 25))
  ns = nodeim.size
  box = (scaled[i][0] - ns[0] // 2, scaled[i][1] - ns[1] // 2,
         scaled[i][0] + ns[0] // 2 + 1, scaled[i][1] + ns[1] // 2 + 1)
  img.paste(nodeim, box)

tree = hcluster.hcluster(projected)
hcluster.draw_dendrogram(tree,imlist,filename='fonts.png')

figure()
imshow(img)
axis('off')
img.save('./pca_font.png')
show()