# encoding:UTF-8 import pickle import numpy as np from scipy.cluster.vq import * import img_tools from PIL import Image import matplotlib.pyplot as plt if __name__=='__main__': url = '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/selectedfontimages/a_selected_thumbs/' imlist = img_tools.get_imlist(url) imlist.sort() imnbr = len(imlist) url_pkl = '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/selectedfontimages/a_pca_modes.pkl' # img size 25*25=625 with open(url_pkl) as f: immean = pickle.load(f) # size 625 V = pickle.load(f) # size 66*625 pca的特征向量 图像乘以特征向量得到pca投影 # 66*625 original immatrix = np.array([np.array(Image.open(im)).flatten() for im in imlist], 'f') immean = immean.flatten() projected = np.array([np.dot(V[:60], immatrix[i]-immean) for i in range(imnbr)]) # 白化 去相关性 相当于去冗余 projected = whiten(projected) centroids, distortion = kmeans(projected, 4) code, distnce = vq(projected, centroids) for k in range(4): ind = np.where(code == k)[0] plt.figure() plt.gray() for i in range(np.minimum(len(ind), 40)):
import matplotlib.pylab as pylab def covert_img(files): for infile in files: # print os.path.splitext(infile) outfile = os.path.splitext(infile)[0] + ".jpg" if infile != outfile: try: Image.open(infile).save(outfile) except IOError: print "cannot covert", infile if __name__ == "__main__": files = tools.get_imlist("/home/auroua/workspace/PycharmProjects/data/pcv_img/avg/") # covert_img(files) img = Image.open(files[0]) # 图像缩放 # img.thumbnail((128,128)) # 裁剪区域,并将区域翻转后放置在指定区域 # box = (100, 100, 400, 400) # region = img.crop(box) # region = region.transpose(Image.ROTATE_180) # img.paste(region, box) # pylab.imshow(img) # image resize and rotate out = img.resize((128, 128))
# -*- coding: utf-8 -*- import img_tools as imtools import pca from PIL import Image, ImageDraw from pylab import * url = '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/selectedfontimages/a_selected_thumbs/' imlist = imtools.get_imlist(url) imnbr = len(imlist) # Load images, run PCA. immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f') V, S, immean = pca.pca(immatrix) # Project on 2 PCs. projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)]) # P131 Fig6-3左图 #projected = array([dot(V[[1, 2]], immatrix[i] - immean) for i in range(imnbr)]) # P131 Fig6-3右图 # 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)
import matplotlib.pylab as pylab def covert_img(files): for infile in files: # print os.path.splitext(infile) outfile = os.path.splitext(infile)[0] + '.jpg' if infile != outfile: try: Image.open(infile).save(outfile) except IOError: print 'cannot covert', infile if __name__ == '__main__': files = tools.get_imlist( '/home/auroua/workspace/PycharmProjects/data/pcv_img/avg/') # covert_img(files) img = Image.open(files[0]) #图像缩放 # img.thumbnail((128,128)) #裁剪区域,并将区域翻转后放置在指定区域 # box = (100, 100, 400, 400) # region = img.crop(box) # region = region.transpose(Image.ROTATE_180) # img.paste(region, box) # pylab.imshow(img) #image resize and rotate out = img.resize((128, 128))
# -*- coding: utf-8 -*- import img_tools as imtools import pca from PIL import Image, ImageDraw from pylab import * url = '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/selectedfontimages/a_selected_thumbs/' imlist = imtools.get_imlist(url) imnbr = len(imlist) # Load images, run PCA. immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f') V, S, immean = pca.pca(immatrix) # Project on 2 PCs. projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)]) # P131 Fig6-3左图 #projected = array([dot(V[[1, 2]], immatrix[i] - immean) for i in range(imnbr)]) # P131 Fig6-3右图 # 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