예제 #1
0
def start_kmeans(args):
    imlist = imtools.get_imlist(args.data_dir)

    imnbr = len(imlist)  # get the number of images
    print("The number of images is %d" % imnbr)

    # Create matrix to store all flattened images
    immatrix = np.array([np.array(cv2.imread(imname)).flatten() for imname in imlist], 'f')

    # PCA reduce dimension
    V, S, immean = pca.pca(immatrix)

    immatrix = np.array([np.array(cv2.imread(im)).flatten() for im in imlist])
    immatrix_src = np.array([np.array(cv2.imread(im)) for im in imlist])

    # project on the 40 first PCs
    immean = immean.flatten()
    projected = np.array([dot(V[:40], immatrix[i] - immean) for i in range(imnbr)])

    # k-means
    projected = whiten(projected)
    centroids, distortion = kmeans(projected, args.num_class)
    code, distance = vq(projected, centroids)
   
    output_path = os.path.join(args.output_dir, 'kmeans_result') 
    if not os.path.exists(output_path):
        os.mkdir(output_path)
    # plot clusters
    for k in range(args.num_class):
        ind = where(code == k)[0]
        print("class:", k, len(ind))
        os.mkdir(os.path.join(output_path, str(k)))
        i = rdm.randint(0,len(ind))
        cv2.imwrite(os.path.join(os.path.join(output_path, str(k)), str(i) + '.jpg'), immatrix_src[ind[i]])
예제 #2
0
# -*- coding: utf-8 -*-
from PCV.tools import imtools
import pickle
from scipy import *
import os
from pylab import *
from PIL import Image
from scipy.cluster.vq import *
from PCV.tools import pca

# Uses sparse pca codepath.
imlist = imtools.get_imlist('data1/selectedfontimages/a_selected_thumbs/')

# 获取图像列表和他们的尺寸
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print "The number of images is %d" % imnbr

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],
                 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)

# 保存均值和主成分
#f = open('./a_pca_modes.pkl', 'wb')
f = open('./a_pca_modes.pkl', 'wb')
pickle.dump(immean, f)
pickle.dump(V, f)
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
from sqlite3 import dbapi2 as sqlite
from PCV.tools.imtools import get_imlist

imlist = get_imlist('./first500/')
nbr_images = len(imlist)
featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]


f = open('./first500/vocabulary.pkl', 'rb')
voc = pickle.load(f)
f.close()

src = imagesearch.Searcher('web.db',voc)
locs,descr = sift.read_features_from_file(featlist[0])
iw = voc.project(descr)

print 'ask using a histogram...'
print src.candidates_from_histogram(iw)[:10]

src = imagesearch.Searcher('web.db',voc)
print 'try a query...'

nbr_results = 12
res = [w[1] for w in src.query(imlist[39])[:nbr_results]]
imagesearch.plot_results(src,res)
예제 #4
0
# -*- coding: utf-8 -*-
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
from sqlite3 import dbapi2 as sqlite
from PCV.tools.imtools import get_imlist

#获取图像列表
imlist = get_imlist('first1000/')
nbr_images = len(imlist)
#获取特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]

# load vocabulary
#载入词汇
with open('first1000/vocabulary.pkl', 'rb') as f:
    voc = pickle.load(f)
#创建索引
indx = imagesearch.Indexer('testImaAdd.db', voc)
indx.create_tables()
# go through all images, project features on vocabulary and insert
#遍历所有的图像,并将它们的特征投影到词汇上
for i in range(nbr_images)[:1000]:
    locs, descr = sift.read_features_from_file(featlist[i])
    indx.add_to_index(imlist[i], descr)
# commit to database
#提交到数据库
indx.db_commit()

con = sqlite.connect('testImaAdd.db')
print(con.execute('select count (filename) from imlist').fetchone())
예제 #5
0
from PCV.tools.imtools import get_imlist #导入原书的PCV模块
from PIL import Image
import os
import pickle

filelist = get_imlist('E:/python/Python Computer Vision/test jpg/') #获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
imlist = open('E:/python/Python Computer Vision/test jpg/imlist.txt','wb+')
#将获取的图片文件列表保存到imlist.txt中
pickle.dump(filelist,imlist) #序列化
imlist.close()

for infile in filelist:
    outfile = os.path.splitext(infile)[0] + ".png" #分离文件名与扩展名
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print ("cannot convert", infile)


예제 #6
0
from PIL import Image
from pylab import *
from numpy import *

from PCV.tools import imtools, pca

# Get list of images and their size
imlist = imtools.get_imlist(
    'fontimages/')  # fontimages.zip is part of the book data set
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],
                 'f')

# Perform PCA
V, S, immean = pca.pca(immatrix)

# Show the images (mean and 7 first modes)
# This gives figure 1-8 (p15) in the book.
figure()
gray()
subplot(2, 4, 1)
imshow(immean.reshape(m, n))
for i in range(7):
    subplot(2, 4, i + 2)
    imshow(V[i].reshape(m, n))
show()
# -*- coding: utf-8 -*-
import pickle
from PIL import Image
from numpy import *
from pylab import *
from PCV.tools import imtools, pca

# Uses sparse pca codepath.
# imlist = imtools.get_imlist('../data/selectedfontimages/a_selected_thumbs')

# 获取图像列表和他们的尺寸
imlist = imtools.get_imlist('../data/fontimages/a_thumbs')  # fontimages.zip is part of the book data set
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print("The number of images is %d" % imnbr)

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)

# 保存均值和主成分
f = open('../data/fontimages/font_pca_modes.pkl', 'wb')
pickle.dump(immean, f)
pickle.dump(V, f)
f.close()

# Show the images (mean and 7 first modes)
# This gives figure 1-8 (p15) in the book.
# -*- coding: utf-8 -*-
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
from sqlite3 import dbapi2 as sqlite
from PCV.tools.imtools import get_imlist

# 获取图像列表
imlist = get_imlist('./first500/')
nbr_images = len(imlist)
# 获取特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]

# 载入词汇
f = open('./first500/vocabulary.pkl', 'rb')
voc = pickle.load(f)
f.close()

src = imagesearch.Searcher('testImaAdd.db', voc)
locs, descr = sift.read_features_from_file(featlist[0])
iw = voc.project(descr)

print('ask using a histogram...')
print(src.candidates_from_histogram(iw)[:10])

src = imagesearch.Searcher('testImaAdd.db', voc)
print('try a query...')

nbr_results = 12
res = [w[1] for w in src.query(imlist[0])[:nbr_results]]
imagesearch.plot_results(src, res)
예제 #9
0
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from PCV.clustering import hcluster

imlist = imtools.get_imlist('../data/selectedfontimages/a_selected_thumbs')
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)
scaled = floor(
예제 #10
0
from PCV.tools import imtools
import pickle
from scipy.cluster.vq import *
from PIL import Image
from PCV.tools import pca
from pylab import *

imlist = imtools.get_imlist('../data/fontimages/a_thumbs/')

im = array(Image.open(imlist[0]))
n, m = im.shape[:2]

imlist = imlist[:200]
imnbr = len(imlist)
immatrix = np.array([np.array(Image.open(im)).flatten() for im in imlist], 'f')

V, S, immean = pca.pca(immatrix)

figure()
gray()
subplot(2, 4, 1)
imshow(immean.reshape(m, n))
for i in range(7):
    subplot(2, 4, i + 2)
    imshow(V[i].reshape(m, n))

show()

immean = immean.flatten()
projected = array([dot(V[:40], immatrix[i] - immean) for i in range(imnbr)])
projected = whiten(projected)
예제 #11
0
# -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from scipy.cluster.vq import *

imlist = imtools.get_imlist('data/line/watercolor/')
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右图

n = len(projected)
# compute distance matrix
S = array([[sqrt(sum((projected[i] - projected[j])**2)) for i in range(n)]
           for j in range(n)], 'f')
# create Laplacian matrix
rowsum = sum(S, axis=0)
D = diag(1 / sqrt(rowsum))
I = identity(n)
L = I - dot(D, dot(S, D))
# compute eigenvectors of L
U, sigma, V = linalg.svd(L)
k = 5
# create feature vector from k first eigenvectors
예제 #12
0
파일: 6.1.2.py 프로젝트: Seminhar/program
#
## Create matrix to store all flattened images
#immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')
#
## PCA降维
#V, S, immean = pca.pca(immatrix)
#
## 保存均值和主成分
##f = open('./a_pca_modes.pkl', 'wb')
#f = open('./a_pca_modes.pkl', 'wb')
#pickle.dump(immean,f)
#pickle.dump(V,f)
#f.close()

# get list of images
imlist = imtools.get_imlist('F:/python/pic/201710162100/')
imnbr = len(imlist)

# load model file
with open('../data/selectedfontimages/a_pca_modes.pkl', 'rb') as f:
    immean = pickle.load(f)
    V = pickle.load(f)
# create matrix to store all flattened images
immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f')

# project on the 40 first PCs
immean = immean.flatten()
projected = array([dot(V[:40], immatrix[i] - immean) for i in range(imnbr)])

# k-means
projected = whiten(projected)
接下来是将前面的预处理更改为同名,即不重命名操作,为此,在demo3_my_6077_test.py文件的基础上,新建spectral_clustering.py文件。

并对灰度化和压缩图像的代码文件进行重写,灰度化对应的代码文件是grayscale_processing.py,压缩图像的代码文件是picture_resize_2.py,
将这两个代码文件处理的时候不对图像进行重命名。
"""

# -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from scipy.cluster.vq import *
import os
import random

# imlist = imtools.get_imlist('./dir_my')
imlist = imtools.get_imlist('./dir_my_gray_rescale')
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右图

n = len(projected)
# compute distance matrix
S = array([[sqrt(sum((projected[i] - projected[j])**2)) for i in range(n)]
           for j in range(n)], 'f')
 # -*- coding: utf-8 -*-
import pickle
from PIL import Image
from numpy import *
from pylab import *
from PCV.tools import imtools, pca

# Uses sparse pca codepath.
#imlist = imtools.get_imlist('../data/selectedfontimages/a_selected_thumbs')

# 获取图像列表和他们的尺寸
imlist = imtools.get_imlist('../data/fontimages/a_thumbs')  # fontimages.zip is part of the book data set
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print "The number of images is %d" % imnbr

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)

# 保存均值和主成分
f = open('../data/fontimages/font_pca_modes.pkl', 'wb')
pickle.dump(immean,f)
pickle.dump(V,f)
f.close()

# Show the images (mean and 7 first modes)
# This gives figure 1-8 (p15) in the book.
 # -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *

imlist = imtools.get_imlist('../data/selectedfontimages/a_selected_thumbs')
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)
scaled = floor(array([(p/scale) * (w/2 - 20, h/2 - 20) + (w/2, h/2)
                      for p in projected])).astype(int)
예제 #16
0
# -*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist
from PIL import Image
import os
import pickle

filelist = get_imlist('../data/convert_images_format_test/'
                      )  # 获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
imlist = open('../data/convert_images_format_test/imlist.txt',
              'wb')  # 将获取的图片文件列表保存到imlist.txt中
pickle.dump(filelist, imlist)  # 序列化
imlist.close()

for infile in filelist:
    outfile = os.path.splitext(infile)[0] + ".png"  # 分离文件名与扩展名
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print("cannot convert", infile)

imlist1 = open('../data/convert_images_format_test/imlist.txt',
               'rb')  # 将获取的图片文件列表保存到imlist.txt中
ss = pickle.load(imlist1)
print(ss)
예제 #17
0
from PCV.tools import imtools

import pickle

from scipy import *

from pylab import *

from PIL import Image

from scipy.cluster.vq import *

from PCV.tools import pca

imlist = imtools.get_imlist(
    '/home/constantine/PycharmProjects/tf_playground/clustering/data/a_selected_thumbs/'
)

im = array(Image.open(imlist[0]))  # open one image to get the size

m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print("The number of images is %d" % imnbr)

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],
                 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)
예제 #18
0
import pickle
from PIL import Image
from numpy import *
from pylab import *
from PCV.tools import imtools,pca

# Uses sparse pca codepath

# 获取图像列表和尺寸
imlist=imtools.get_imlist('E:/python/Python Computer Vision/Image data/fontimages/a_thumbs')
# open ont image to get the size
im=array(Image.open(imlist[0]))
# get the size of the images
m,n=im.shape[:2]
# get the number of images
imnbr=len(imlist)
print("The number of images is %d" % imnbr)

# create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],'f')

# PCA降维
V,S,immean=pca.pca(immatrix)

# 保存均值和主成分
#f = open('../ch01/font_pca_modes.pkl', 'wb')
#pickle.dump(immean,f)
#pickle.dump(V,f)
#f.close()

# Show the images (mean and 7 first modes)
# -*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist
from PIL import Image
import os
import pickle

filelist = get_imlist('../data/convert_images_format_test/') #获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
imlist = file('../data/convert_images_format_test/imlist.txt','w') #将获取的图片文件列表保存到imlist.txt中
pickle.dump(filelist,imlist) #序列化
imlist.close()

for infile in filelist:
    outfile = os.path.splitext(infile)[0] + ".png" #分离文件名与扩展名
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print "cannot convert", infile
예제 #20
0
from PCV.tools import imtools
import numpy as np
import pickle
from scipy import *
from pylab import *
from PIL import Image
from scipy.cluster.vq import *
from PCV.tools import pca

img_dir = r'E:\DataSet\fiber\cluster_roi'
N = 15

# Uses sparse pca codepath.
imlist = imtools.get_imlist(img_dir)

# 获取图像列表和他们的尺寸
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print("The number of images is %d" % imnbr)

# Create matrix to store all flattened images

# ValueError: setting an array element with a sequence.
# immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

immatrix = np.array([
    array(Image.open(imname).resize((50, 50)).convert('L')).flatten()
    for imname in imlist
])
Modify Date: 2018-07-02
descirption: "some utils"
'''

import pickle

import numpy as np
from PCV.tools import imtools, pca
from PIL import Image
from pylab import *
from scipy import *
from scipy.cluster.vq import *

temp_image_path = "G:\python\pcv_data\data\selectedfontimages\\a_selected_thumbs"

imlist = imtools.get_imlist(temp_image_path)
print imlist[0]

# 获取图像列表和他们的尺寸
im = np.array(Image.open(imlist[0])) 
print type(im)
m, n = im.shape[:2]  
imnbr = len(imlist)  
print "The number of images is %d" % imnbr


immatrix = np.array([np.array(Image.open(imname)).flatten() for imname in imlist[:10]], 'f')
print immatrix.shape

V, S, immean = pca.pca(immatrix)
print V[0][:10]
예제 #22
0
from PCV.tools.imtools import get_imlist
from PIL import Image
from pylab import *
from PCV.tools import imtools

# 添加中文字体支持
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

filelist = get_imlist('E:/python/Python Computer Vision/test average/')
#获取convert_images_format_test文件夹下的图片文件名(包括后缀名)

avg = imtools.compute_average(filelist)

for impath in filelist:
    im1 = array(Image.open(impath))
    subplot(2, 2, filelist.index(impath) + 1)
    imshow(im1)
    imNum = str(filelist.index(impath) + 1)
    title(u'待平均图像' + imNum, fontproperties=font)
    axis('off')
subplot(2, 2, 4)
imshow(avg)
title(u'平均后的图像', fontproperties=font)
axis('off')

show()
예제 #23
0
from skimage import io
def falter_image(path):
    for i in os.listdir(path):
        file = os.path.join(path,i)
        image = io.imread(file)
        image = np.where(image > 1,0,1)
        new_image = np.ones((100,100))
        row,col = image.shape
        new_image[0:row,0:col] = image
        io.imsave(file,(new_image * 255).astype(np.int))
# Uses sparse pca codepath.
# imlist = imtools.get_imlist('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_selected_thumbs/')

# falter_image("D:/datebase/test11")

imlist = imtools.get_imlist('D:/datebase/test11/')
print(imlist)
# 获取图像列表和他们的尺寸
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images
imnbr = len(imlist)  # get the number of images
print("The number of images is %d" % imnbr)

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)

# 保存均值和主成分
#f = open('./a_pca_modes.pkl', 'wb')
예제 #24
0
def PCA_Kmeans(train_path,result_path):
    # Uses sparse pca codepath.
    # imlist = imtools.get_imlist('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_selected_thumbs/')

    # falter_image("D:/datebase/test")

    imlist = imtools.get_imlist(train_path)
    # print(imlist)
    # 获取图像列表和他们的尺寸
    im = array(Image.open(imlist[0]))  # open one image to get the size
    m, n = im.shape[:2]  # get the size of the images
    imnbr = len(imlist)  # get the number of images
    print("The number of images is %d" % imnbr)

    # Create matrix to store all flattened images
    immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

    # PCA降维
    V, S, immean = pca.pca(immatrix)

    # 保存均值和主成分
    # f = open('./a_pca_modes.pkl', 'wb')
    # f = open('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_pca_modes.pkl', 'wb')

    f = open(train_path + "\\a_pca_modes.pkl", 'wb')

    pickle.dump(immean, f)
    pickle.dump(V, f)
    f.close()

    # get list of images
    # imlist = imtools.get_imlist('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_selected_thumbs/')
    print("go")
    imlist = imtools.get_imlist(train_path)
    imnbr = len(imlist)
    # load model file
    # with open('C:/Users/Zqc/Desktop/PCV-book-data/data/selectedfontimages/a_pca_modes.pkl','rb') as f:
    with open(train_path + "a_pca_modes.pkl", 'rb') as f:
        immean = pickle.load(f)
        V = pickle.load(f)
    # create matrix to store all flattened images
    immatrix = array([array(Image.open(im)).flatten() for im in imlist], 'f')

    # project on the 40 first PCs
    immean = immean.flatten()

    projected = array([dot(V[:30], immatrix[i] - immean) for i in range(imnbr)])
    print("PCA")

    # k-means
    projected = whiten(projected)
    centroids, distortion = kmeans(projected, 370)
    code, distance = vq(projected, centroids)

    filepath = result_path
    # plot clusters
    for k in range(370):
        tempath = filepath + str(k)
        os.makedirs(tempath)
        ind = where(code == k)[0]
        for i in range(len(ind)):
            io.imsave(tempath + "\\" + str(i) + ".png", immatrix[ind[i]].reshape((100, 100)).astype(np.uint8),
                      cmap="gray")
예제 #25
0
# -*- coding: utf-8 -*-
from PCV.tools.imtools import get_imlist #导入PCV模块
from PIL import Image
import os
import pickle

filelist = get_imlist('results/1/') #获取convert_images_format_test文件夹下的图片文件名(包括后缀名)
os.mkdir('last')
imlist = file('last/imlist.txt','w') #将获取的图片文件列表保存到imlist.txt中
pickle.dump(filelist,imlist) #序列化
imlist.close()

for infile in filelist:
    outfile = os.path.splitext(infile)[0] + ".tif" #分离文件名与扩展名
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print ("cannot convert", infile)
예제 #26
0
파일: pca_graylevel.py 프로젝트: Adon-m/PCV
from PIL import Image
from pylab import *
from numpy import *

from PCV.tools import imtools, pca

# Get list of images and their size
imlist = imtools.get_imlist('fontimages/') # fontimages.zip is part of the book data set
im = array(Image.open(imlist[0])) # open one image to get the size 
m,n = im.shape[:2]

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],'f')

# Perform PCA
V,S,immean = pca.pca(immatrix)

# Show the images (mean and 7 first modes)
# This gives figure 1-8 (p15) in the book.
figure()
gray()
subplot(2,4,1)
imshow(immean.reshape(m,n))
for i in range(7):
    subplot(2,4,i+2)
    imshow(V[i].reshape(m,n))
show()
예제 #27
0
from PIL import Image
from scipy.cluster.vq import *
from PCV.tools import pca




img_w = 128
img_h = 128
img_deep = 3
pca_demension = 40 # 亦即取40张图去和特征做dot点乘
k_category = 8 # k-means聚类,设置为8类

#
imgs_path = "/Users/zac/5-Algrithm/algrithm-data/ImageCluster/images_resized"
imlist = imtools.get_imlist(imgs_path)

# 获取图像列表和其尺寸
im = array(Image.open(imlist[0])) # open one image to get the size
m, n = im.shape[:2] # get the size of the images
imnbr = len(imlist) # get number of images
print " the number of images is %d" % imnbr

# Create matrix to store all flattened images
immatrix = array([array(Image.open(imname)).flatten() for imname in imlist], 'f')

# PCA降维
V, S, immean = pca.pca(immatrix)
# 保存均值和主成分
pkl_path = "/Users/zac/5-Algrithm/algrithm-data/ImageCluster/pca_model.pkl"
#f = open('./a_pca_modes.pkl', 'wb')
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
from sqlite3 import dbapi2 as sqlite
from PCV.tools.imtools import get_imlist

imlist = get_imlist("./first500/")
nbr_images = len(imlist)
featlist = [imlist[i][:-3] + "sift" for i in range(nbr_images)]
# load vocabulary
with open("./vocabulary.pkl", "rb") as f:
    voc = pickle.load(f)
# create indexer
indx = imagesearch.Indexer("web.db", voc)
indx.create_tables()
# go through all images, project features on vocabulary and insert
for i in range(nbr_images)[:500]:
    locs, descr = sift.read_features_from_file(featlist[i])
    indx.add_to_index(imlist[i], descr)
# commit to database
indx.db_commit()

con = sqlite.connect("web.db")
print con.execute("select count (filename) from imlist").fetchone()
print con.execute("select * from imlist").fetchone()
예제 #29
0
 # -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from scipy.cluster.vq import *
import scipy.sparse as ss
import myTools as mt
import numpy as np
import pickle

np.seterr(divide='ignore',invalid='ignore')

save_A_path = './A.pkl'
save_feat_path = './result/feat.pkl'

imlist = imtools.get_imlist('./data/fer2013/cluTest/')
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右图
projected = array([dot(V[:40],immatrix[i]-immean) for i in range(imnbr)])    #463*40 = n*40
n = len(projected)

nn_opt = 'pdist'

if nn_opt == 'pdist':
예제 #30
0
# -*- coding: utf-8 -*-
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from pylab import *
from scipy.cluster.vq import *

imlist = imtools.get_imlist('tmp/test/')
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)

projected = array([dot(V[[0, 1]], immatrix[i] - immean) for i in range(imnbr)])

n = len(projected)
# compute distance matrix
S = array([[sqrt(sum((projected[i] - projected[j])**2)) for i in range(n)]
           for j in range(n)], 'f')
# create Laplacian matrix
rowsum = sum(S, axis=0)
D = diag(1 / sqrt(rowsum))
I = identity(n)
L = I - dot(D, dot(S, D))
# compute eigenvectors of L
U, sigma, V = linalg.svd(L)
k = 20
# create feature vector from k first eigenvectors
# by stacking eigenvectors as columns
features = array(V[:k]).T
# k-means
예제 #31
0
"""
Created on 2013-3-19

@author: liaoyuhua
"""
from PCV.localdescriptors import sift
from PCV.tools import imtools as itl
import pickle
from PCV.imagesearch import vocabulary
from PCV.imagesearch import imagesearch

if __name__ == "__main__":
    path = r"D:\Python_Computer_Vision\data\sunsets\flickr-sunsets-small"
    imlist = itl.get_imlist(path)

    nbr_images = len(imlist)
    featlist = [i[:-3] + "sft" for i in imlist]
    #    print '\n'.join(featlist)
    #    for i in range(nbr_images):
    #        sift.process_image(imlist[i], featlist[i])
    #    print "Sift Processing Ok!"
    #    voc=vocabulary.Vocabulary('ukbenchtest')
    #    voc.train(featlist,100,1)
    #    print "voc constructed!"
    #    with open('ukbentest.pkl','wb') as f:
    #        pickle.dump(voc,f)
    #    print 'vocabulary is,',voc.name,voc.nbr_words
    with open("ukbentest.pkl", "rb") as f:
        voc = pickle.load(f)

    indx = imagesearch.Indexer("test.db", voc)
예제 #32
0
from PIL import Image
from PCV.localdescriptors import sift
from PCV.tools import imtools
import pydot

""" This is the example graph illustration of matching images from Figure 2-10.
To download the images, see ch2_download_panoramio.py."""

#download_path = "panoimages"  # set this to the path where you downloaded the panoramio images
#path = "/FULLPATH/panoimages/"  # path to save thumbnails (pydot needs the full system path)

download_path = "F:\\dropbox\\Dropbox\\translation\\pcv-notebook\\data\\panoimages"  # set this to the path where you downloaded the panoramio images
path = "F:\\dropbox\\Dropbox\\translation\\pcv-notebook\\data\\panoimages\\"  # path to save thumbnails (pydot needs the full system path)

# list of downloaded filenames
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)

# extract features
featlist = [imname[:-3] + 'sift' for imname in imlist]
#for i, imname in enumerate(imlist):
#    sift.process_image(imname, featlist[i])

matchscores = zeros((nbr_images, nbr_images))

for i in range(nbr_images):
    for j in range(i, nbr_images):  # only compute upper triangle
        print 'comparing ', imlist[i], imlist[j]
        l1, d1 = sift.read_features_from_file(featlist[i])
        l2, d2 = sift.read_features_from_file(featlist[j])
        matches = sift.match_twosided(d1, d2)
예제 #33
0
from PCV.tools import imtools
from PCV.clustering import hcluster
import pickle
from scipy import *
from pylab import *
import matplotlib.pyplot as plt
from PIL import Image
from scipy.cluster.vq import *
from PCV.tools import pca

# Uses sparse pca codepath.

bank_hl = r'E:\CheckDS\bank_hualing'
bank_dn = r'E:\CheckDS\bank_logo0927'

imlist = imtools.get_imlist(bank_dn)
# imlist = imlist + imtools.get_imlist(bank_hl)

# 获取图像列表和他们的尺寸
im = array(Image.open(imlist[0]))  # open one image to get the size
m, n = im.shape[:2]  # get the size of the images

print("The number of images is %d" % len(imlist))

# Create matrix to store all flattened images

imss = [Image.open(imname) for imname in imlist]

immatrix = array([array(Image.open(imname)).flatten() for imname in imlist],
                 'f')
예제 #34
0
from PIL import Image

from PCV.localdescriptors import sift
from PCV.tools import imtools

import pydot
"""
This is the example graph illustration of matching images from Figure 2-10.
To download the images, see ch2_download_panoramio.py.
"""

download_path = "panoimages"  # set this to the path where you downloaded the panoramio images
path = "/FULLPATH/panoimages/"  # path to save thumbnails (pydot needs the full system path)

# list of downloaded filenames
imlist = imtools.get_imlist(download_path)
nbr_images = len(imlist)

# extract features
featlist = [imname[:-3] + 'sift' for imname in imlist]
for i, imname in enumerate(imlist):
    sift.process_image(imname, featlist[i])

matchscores = zeros((nbr_images, nbr_images))

for i in range(nbr_images):
    for j in range(i, nbr_images):  # only compute upper triangle
        print 'comparing ', imlist[i], imlist[j]
        l1, d1 = sift.read_features_from_file(featlist[i])
        l2, d2 = sift.read_features_from_file(featlist[j])
        matches = sift.match_twosided(d1, d2)
예제 #35
0
파일: test_pca.py 프로젝트: bestdpf/pycv
from PIL import Image
from numpy import *
from pylab import *
from PCV.tools import imtools, pca

path = '/home/duan/pycv/PCV-data/a_thumbs'
imlist = imtools.get_imlist(path)
im = array(Image.open(imlist[0]))
m, n = im.shape[0:2]
imnbr = len(imlist)

immatrix = array([ array(Image.open(im)).flatten() 
            for im in imlist ], 'f')

V, S, immean = pca.pca(immatrix)

figure()
gray()
subplot(2,4,1)
imshow(immean.reshape(m,n))
for i in range(7):
    subplot(2,4,i+2)
    imshow(V[i].reshape(m,n))
show()


예제 #36
0
# -*- coding: utf-8 -*-
import pickle
from PCV.imagesearch import imagesearch
from PCV.localdescriptors import sift
import sqlite3
from PCV.tools.imtools import get_imlist

#获取图像列表
imlist = get_imlist('training/')
nbr_images = len(imlist)

#获取特征列表
featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]

# load vocabulary
with open('training/vocabulary.pkl', 'rb') as f:
    voc = pickle.load(f)

# 创建索引
indx = imagesearch.Indexer('testImaAdd_training.db', voc)
indx.create_tables()

# go through all images, project features on vocabulary and insert
for i in range(nbr_images):
    locs, descr = sift.read_features_from_file(featlist[i])
    indx.add_to_index(imlist[i], descr)

# commit to database
indx.db_commit()
con = sqlite3.connect('testImaAdd_training.db')
print(con.execute('select count (filename) from imlist').fetchone())
예제 #37
0
from PCV.tools import imtools, pca
from PIL import Image, ImageDraw
from PCV.localdescriptors import sift
from pylab import *
import glob
from scipy.cluster.vq import *


#download_path = "panoimages"  # set this to the path where you downloaded the panoramio images
#path = "/FULLPATH/panoimages/"  # path to save thumbnails (pydot needs the full system path)

download_path = "F:/dropbox/Dropbox/translation/pcv-notebook/data/panoimages"  # set this to the path where you downloaded the panoramio images
path = "F:/dropbox/Dropbox/translation/pcv-notebook/data/panoimages/"  # path to save thumbnails (pydot needs the full system path)

# list of downloaded filenames
imlist = imtools.get_imlist('../data/panoimages/')
nbr_images = len(imlist)

# extract features
#featlist = [imname[:-3] + 'sift' for imname in imlist]
#for i, imname in enumerate(imlist):
#    sift.process_image(imname, featlist[i])

featlist = glob.glob('../data/panoimages/*.sift')

matchscores = zeros((nbr_images, nbr_images))

for i in range(nbr_images):
    for j in range(i, nbr_images):  # only compute upper triangle
        print 'comparing ', imlist[i], imlist[j]
        l1, d1 = sift.read_features_from_file(featlist[i])