def train(self, featurefiles, k=100, subsampling=10): """用含有k个单词的K-means列出在featurefiles中的特征文件训练处一个词汇。对训练数据下采样可以加快训练速度""" nbr_images = len(featurefiles) # 从文件中读取特征 descr = [] descr.append(sift.read_feature_from_file(featurefiles[0])[1]) # 将所有的特征并在一起,以便后面进行K-means聚类 descriptors = descr[0] count = 0 for i in np.arange(1, nbr_images): d1 = sift.read_feature_from_file(featurefiles[i])[1] if d1.shape[1] == 0: count += 1 continue descr.append(d1) descriptors = np.vstack((descriptors, descr[i - count])) self.voc, distortion = kmeans(descriptors[::subsampling, :], k, 1) self.nbr_words = self.voc.shape[0] nbr_images = nbr_images - count # 遍历所有的训练图像,并投影到词汇上 imwords = np.zeros((nbr_images, self.nbr_words)) for i in range(nbr_images): imwords[i] = self.project(descr[i]) nbr_occurences = np.sum((imwords > 0) * 1, axis=0) self.idf = np.log((1.0 * nbr_images) / (1.0 * nbr_occurences + 1)) self.trainingdata = featurefiles
def train(self, featurefiles, k=100, subsampling=10): """用含有k个单词的K-means列出在featurefiles中的特征文件训练处一个词汇。对训练数据下采样可以加快训练速度""" nbr_images = len(featurefiles) # 从文件中读取特征 descr = [] descr.append(sift.read_feature_from_file(featurefiles[0])[1]) # 将所有的特征并在一起,以便后面进行K-means聚类 descriptors = descr[0] count=0 for i in np.arange(1, nbr_images): d1 = sift.read_feature_from_file(featurefiles[i])[1] if d1.shape[1] == 0: count += 1 continue descr.append(d1) descriptors = np.vstack((descriptors, descr[i-count])) self.voc, distortion = kmeans(descriptors[::subsampling, :], k, 1) self.nbr_words = self.voc.shape[0] nbr_images = nbr_images - count # 遍历所有的训练图像,并投影到词汇上 imwords = np.zeros((nbr_images, self.nbr_words)) for i in range(nbr_images): imwords[i] = self.project(descr[i]) nbr_occurences = np.sum((imwords>0)*1, axis=0) self.idf = np.log((1.0*nbr_images)/(1.0*nbr_occurences+1)) self.trainingdata = featurefiles
def sift_matrix(): featurelist = sift_pan_desc_generator('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/') imlist = getFiles('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/panoimages/') nbr_images = len(imlist) matchscores = np.zeros((nbr_images, nbr_images)) for i in range(nbr_images): for j in range(i, nbr_images): print 'comparing ', imlist[i], imlist[j] l1, d1 = sift.read_feature_from_file(featurelist[i]) l2, d2 = sift.read_feature_from_file(featurelist[j]) matches = sift.match_twosided(d1, d2) nbr_matches = sum(matches > 0) print 'number of matches = ', nbr_matches matchscores[i, j] = nbr_matches for i in range(nbr_images): for j in range(i + 1, nbr_images): matchscores[j, i] = matchscores[i, j] np.save('pan_img_matchscore', matchscores)
def read_gesture_features_labels(path): featlists = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.dsift')] features = [] for featfile in featlists: l, d = sift.read_feature_from_file(featfile) features.append(d.flatten()) features = np.array(features, dtype=np.float64) labels = [f.split('/')[-1][0] for f in featlists] return features, np.array(labels)
def read_gesture_features_labels(path): featlists = [ os.path.join(path, f) for f in os.listdir(path) if f.endswith('.dsift') ] features = [] for featfile in featlists: l, d = sift.read_feature_from_file(featfile) features.append(d.flatten()) features = np.array(features, dtype=np.float64) labels = [f.split('/')[-1][0] for f in featlists] return features, np.array(labels)
else: cmmd = str('sift ' + imagename + ' --output=' + resultname + " --read-frames=tmp.frame") os.system(cmmd) print 'processed', imagename, 'to', resultname if __name__ == '__main__': """可以使用欧几里得距离来计算dsift之间的距离""" process_image_dsift( '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg', 'climbing_1.sift', 90, 40, True) process_image_dsift( '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_2_small.jpg', 'climbing_2.sift', 90, 40, True) l1, d1 = sift.read_feature_from_file('climbing_1.sift') l2, d2 = sift.read_feature_from_file('climbing_2.sift') img1 = np.array( Image.open( '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg' ).convert('L')) print 'starting matching' matches = sift.match_twosided(d1, d2) plt.figure() plt.gray() sift.plot_features(img1, l1, True) # img1 = np.array(Image.open('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg').convert('L')) # img2 = np.array(Image.open('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_2_small.jpg').convert('L')) # sift.plot_matches(img1, img2, l1, l2, matches)
print xx.shape frame = np.array([xx, yy, scale*np.ones(xx.shape[0]), np.zeros(xx.shape[0])]) print frame.shape np.savetxt('tmp.frame', frame.T, fmt='%03.3f') if force_orientation: cmmd = str('sift '+imagename+' --output='+resultname+" --read-frames=tmp.frame --orientations") else: cmmd = str('sift '+imagename+' --output='+resultname+" --read-frames=tmp.frame") os.system(cmmd) print 'processed', imagename, 'to', resultname if __name__=='__main__': """可以使用欧几里得距离来计算dsift之间的距离""" process_image_dsift('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg', 'climbing_1.sift', 90, 40, True) process_image_dsift('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_2_small.jpg', 'climbing_2.sift', 90, 40, True) l1, d1 = sift.read_feature_from_file('climbing_1.sift') l2, d2 = sift.read_feature_from_file('climbing_2.sift') img1 = np.array(Image.open('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg').convert('L')) print 'starting matching' matches = sift.match_twosided(d1, d2) plt.figure() plt.gray() sift.plot_features(img1, l1, True) # img1 = np.array(Image.open('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_1_small.jpg').convert('L')) # img2 = np.array(Image.open('/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/climbing_2_small.jpg').convert('L')) # sift.plot_matches(img1, img2, l1, l2, matches) plt.show()
url = '/home/aurora/hdd/workspace/PycharmProjects/data/pcv_img/first1000/' imlists = vocabulary.get_img_list(url) feature = vocabulary.get_feature_list(url) nbr_images = len(imlists) with open('vocabulary-new.pkl', 'rb') as f: voc = pickle.load(f) src = imagesearch.Searcher('test.db', voc) q_ind = 50 nbr_results = 20 res_reg = [w[1] for w in src.query(imlists[q_ind])[:nbr_images]] print 'top matches (regular):', res_reg q_locs, q_descr = sift.read_feature_from_file(feature[q_ind]) fp = homography.make_homog(q_locs[:, :2].T) model = homography.RansacModel() rank = {} # load image features for result #载入候选图像的特征 for ndx in res_reg[1:]: locs,descr = sift.read_feature_from_file(feature[ndx]) # because 'ndx' is a rowid of the DB that starts at 1 # get matches matches = sift.match(q_descr,descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:,:2].T) # compute homography, count inliers. if not enough matches return empty list try:
import homography import cam import sift #compute the feature sift.process_image('/home/wangkai/Pictures/book_frontal.jpg','im0.sift') l0,d0 = sift.read_feature_from_file('im0.sift') sift.process_image('/home/wangkai/Pictures/book_prespective.jpg','im1.sift') l1,d1 = sift.read_feature_from_file('im1.sift') #match the feature, compute the homography matches = sift.match_twoside(d0,d1) ndx = matches.nozeros()[0] fp = homography.make_homog(l0[ndx,:2].T) ndx2 = [int(matches[i]) for i in ndx] tp = homography.make_homog(l1[ndx2,:2].T) model = homography.RansacModel() H = homography.H_from_ransac(fp,tp,model) def cube_points(c,wid): """create a list of points for draw cube""" p = [] #bottom p.addpen([c[0]-wid,c[1]-wid,c[2]-wid]) p.addpen([c[0]-wid,c[1]+wid,c[2]-wid]) p.addpen([c[0]+wid,c[1]+wid,c[2]-wid]) p.addpen([c[0]+wid,c[1]-wid,c[2]-wid]) p.addpen([c[0]-wid,c[1]-wid,c[2]-wid])#to draw close image,as the first #top