def cbir_train(train_path, voc_name, db_name, n_subsample=2000, n_cluster=2000, subfeatsampling=10): voc_name = voc_name + '_' + str(n_subsample) + '_' + str(n_cluster) + '_' + str(subfeatsampling) db_name = db_name[:-3] + '_' + str(n_subsample) + '_' + str(n_cluster) + '_' + str(subfeatsampling) + db_name[-3:] imlist, featlist = cbir_utils.create_imglist_featlist(train_path) imlist = imlist[:n_subsample] featlist = featlist[:n_subsample] ### generate sift feature nbr_images = len(imlist) '''''' for i in range(nbr_images): sift.process_image(imlist[i], featlist[i], mask = True) ### generate visual word voc = visual_word.Vocabulary(voc_name) voc.train(featlist, n_cluster, subfeatsampling) with open(voc_name+'.pkl', 'wb') as f: cPickle.dump(voc, f) print 'vocabulary is', voc.name, voc.nbr_word ### generate image index with open(voc_name+'.pkl', 'rb') as f: voc = cPickle.load(f) indx = image_search.Indexer(db_name, voc) indx.create_tables() for i in range(nbr_images): locs, descr = sift.read_features_from_file(featlist[i]) indx.add_to_index(imlist[i], descr) indx.db_commit() print 'generate index finish!' print 'training over'
def sift_pan_desc_generator(path='/home/aurora/hdd/workspace/PycharmProjects/data/N20040103G/'): filelists = getFiles(path) feature = [] for index, file in enumerate(filelists): sift.process_image(file, 'pan'+str(index)+'.sift') feature.append('pan'+str(index)+'.sift') return feature
def sift_aurora_desc_generator(path, des): filelists = getFiles(path) feature = [] for index, file in enumerate(filelists): sift.process_image(file, des+str(index)+'.sift') feature.append(des+str(index)+'.sift') return feature
def extract_sift_feature(fname): fn, fext = os.path.splitext(os.path.basename(fname)) if not os.path.exists(TMP_DIR + fn + '.key'): im = Image.open(fname) im_l = im.convert('L') im_l.save(TMP_DIR + fn + '.pgm', 'PPM') sift.process_image(TMP_DIR + fn + '.pgm', TMP_DIR + fn + '.key') os.remove(TMP_DIR + fn + '.pgm')
def plot_sift_feature(im): #imname = ’empire.jpg’ #im1 = array(Image.open(imname).convert(’L’)) tmpFile = 'tmp.sift' sift.process_image(im,tmpFile) l1,d1 = sift.read_features_from_file(tmpFile) figure() gray() sift.plot_features(im,l1,circle=True) show()
def cal(queryname, judge): features = {} i = 0 model = zeros(128) for dirs, root, files in os.walk(datapath): for filename in files: #sift.process_image(datapath + filename , './sifts/' + filename[:-4] + '.sift') l, d = sift.read_features_from_file('./sifts/' + filename[:-4] + '.sift') features[filename] = d i += 1 for i in range(len(d)): model = vstack((model, d[i])) model = delete(model, 0, 0) query = zeros(128) sift.process_image(datapath + queryname, 'temp.sift') l, d = sift.read_features_from_file('temp.sift') for i in range(len(d)): query = vstack((query, d[i])) query = delete(query, 0, 0) whitened = whiten(model) k, num = kmeans(whitened, 10) queryarr, disnum = vq(query, k) temp = [0] * 10 for i in queryarr: temp[i] += 1 countdict = {} for k, v in features.iteritems(): countdict[k] = [0] * 10 featurearr, desnum = vq(features[k], k) for i in featurearr: countdict[k][i] += 1 result = {} for k, v in countdict.iteritems(): numsum = 0 for i in range(10): val = countdict[k][i] numsum += (val - temp[i]) * (val - temp[i]) result[k] = numsum result = sorted([(v, k) for (k, v) in result.items()], reverse=False) if judge: for i in result[:10]: print i else: return result[:10]
def get_krt(im1, im2): ims = [im1, im2] sifts = [] for x in range(2): sifts.append(ims[x][:-3]+"sift") # compute features #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift') sift.process_image(ims[0],sifts[0]) l0,d0 = sift.read_features_from_file(sifts[0]) #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift') sift.process_image(ims[1],sifts[1]) l1,d1 = sift.read_features_from_file(sifts[1]) # match features and estimate homography matches = sift.match_twosided(d0,d1) ndx = matches.nonzero()[0] fp = homography.make_homog(l0[ndx,:2].T) ndx2 = [int(matches[i]) for i in ndx] print len(ndx2) tp = homography.make_homog(l1[ndx2,:2].T) model = homography.RansacModel() H,ransac_data = homography.H_from_ransac(fp,tp,model) # camera calibration #K = camera.my_calibration((747,1000)) K = camera.my_calibration((Image.open(im2).size)) # 3D points at plane z=0 with sides of length 0.2 box = cube.cube_points([0,0,0.1],0.1) # project bottom square in first image cam1 = camera.Camera( hstack((K,dot(K,array([[0],[0],[-1]])) )) ) # first points are the bottom square box_cam1 = cam1.project(homography.make_homog(box[:,:5])) # use H to transfer points to the second image print dot(H,box_cam1) box_trans = homography.normalize(dot(H,box_cam1)) # compute second camera matrix from cam1 and H cam2 = camera.Camera(dot(H,cam1.P)) A = dot(linalg.inv(K),cam2.P[:,:3]) A = array([A[:,0],A[:,1],cross(A[:,0],A[:,1])]).T cam2.P[:,:3] = dot(K,A) # project with the second camera box_cam2 = cam2.project(homography.make_homog(box)) # test: projecting point on z=0 should give the same point = array([1,1,0,1]).T print homography.normalize(dot(dot(H,cam1.P),point)) print cam2.project(point) import pickle with open('%s.pkl' % ims[1][:-4],'w') as f: pickle.dump(K,f) pickle.dump(dot(linalg.inv(K),cam2.P),f) sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
def get_sift_lowe(img): features_fname = img + '.sift' if os.path.isfile(features_fname) == False: is_size_zero = sift.process_image(img, features_fname) if is_size_zero: os.remove(features_fname) sift.process_image(img, features_fname) if os.path.isfile(features_fname) and os.path.getsize(features_fname) == 0: os.remove(features_fname) sift.process_image(img, features_fname) locs, desc = sift.read_features_from_file(features_fname) return desc
def get_krt(im1, im2): ims = [im1, im2] sifts = [] for x in range(2): sifts.append(ims[x][:-3] + "sift") # compute features #sift.process_image('../../data/book_frontal.JPG','../../data/im0.sift') sift.process_image(ims[0], sifts[0]) l0, d0 = sift.read_features_from_file(sifts[0]) #sift.process_image('../../data/book_perspective.JPG','../../data/im1.sift') sift.process_image(ims[1], sifts[1]) l1, d1 = sift.read_features_from_file(sifts[1]) # match features and estimate homography matches = sift.match_twosided(d0, d1) ndx = matches.nonzero()[0] fp = homography.make_homog(l0[ndx, :2].T) ndx2 = [int(matches[i]) for i in ndx] print len(ndx2) tp = homography.make_homog(l1[ndx2, :2].T) model = homography.RansacModel() H, ransac_data = homography.H_from_ransac(fp, tp, model) # camera calibration #K = camera.my_calibration((747,1000)) K = camera.my_calibration((Image.open(im2).size)) # 3D points at plane z=0 with sides of length 0.2 box = cube.cube_points([0, 0, 0.1], 0.1) # project bottom square in first image cam1 = camera.Camera(hstack((K, dot(K, array([[0], [0], [-1]]))))) # first points are the bottom square box_cam1 = cam1.project(homography.make_homog(box[:, :5])) # use H to transfer points to the second image print dot(H, box_cam1) box_trans = homography.normalize(dot(H, box_cam1)) # compute second camera matrix from cam1 and H cam2 = camera.Camera(dot(H, cam1.P)) A = dot(linalg.inv(K), cam2.P[:, :3]) A = array([A[:, 0], A[:, 1], cross(A[:, 0], A[:, 1])]).T cam2.P[:, :3] = dot(K, A) # project with the second camera box_cam2 = cam2.project(homography.make_homog(box)) # test: projecting point on z=0 should give the same point = array([1, 1, 0, 1]).T print homography.normalize(dot(dot(H, cam1.P), point)) print cam2.project(point) import pickle with open('%s.pkl' % ims[1][:-4], 'w') as f: pickle.dump(K, f) pickle.dump(dot(linalg.inv(K), cam2.P), f) sys.stderr.write("K and Rt dumped to %s.pkl\n" % ims[1][:-4])
def extractSift(input_files): all_features_dict = {} for i, fname in enumerate(input_files): features_fname = fname + '.sift' if exists(features_fname) == False: print "calculating sift features for", fname sift.process_image(fname, features_fname) print "gathering sift features for", fname, locs, descriptors = sift.read_features_from_file(features_fname) print descriptors.shape all_features_dict[fname] = descriptors return all_features_dict
def generate_sift_image(imname): im = array(Image.open(imname)) removed_ext = imname[0:imname.index(".")] sift.process_image(imname, removed_ext + '.sift') l1, d1 = sift.read_features_from_file(removed_ext + '.sift') print d1.shape figure() gray() sift.plot_features(im, l1, circle=True) savefig(removed_ext + "_sift.png", bbox_inches="tight") show()
def extractSift(input_files,target_folder): all_features_dict = {} count=0 for i,fname in enumerate(input_files): features_fname = target_folder+'/'+fname.split('/')[2].split('.')[0]+'.sift' if exists(features_fname) == False: print("Calculating sift features for ",fname) sift.process_image(fname, features_fname,count) count+=1 locs, descriptors = sift.read_features_from_file(features_fname) all_features_dict[fname] = (locs,descriptors) os.chdir('..') return all_features_dict
def plot_circs_match(imname1, imname2, circle=False): def draw_circle(c, r): t = arange(0, 1.01, .01) * 2 * pi x = r * cos(t) + c[0] y = r * sin(t) + c[1] plot(x, y, "b", linewidth=2) aim1 = array(Image.open(imname1).convert('L')) aim2 = array(Image.open(imname2).convert('L')) offset = aim2.shape[0] - aim1.shape[0] aim1 = concatenate((aim1, zeros((offset, aim1.shape[1]))), axis=0) sift.process_image(imname1, 'tmp1.sift') sift.process_image(imname2, 'tmp2.sift') l1, d1 = sift.read_features_from_file('tmp1.sift') l2, d2 = sift.read_features_from_file('tmp2.sift') # Now we have the two features, we can build the distance matrix D = pairwise_dist(d1, d2) # for each feature in d1, find the minimum distance in d2 features that matches lines = empty((50, 2, 2)) # [ [[x1,y1], [x2,y2]], ...] li = 0 min_list = empty((D.shape[0])) for i in range(0, D.shape[0]): min_list[i] = min(D[i]) min_list = sort(min_list) for i in range(0, D.shape[0]): for j in range(0, D.shape[1]): if (D[i, j] in min_list[:50]): lines[li][0] = l1[i, :2] lines[li][1] = l2[j, :2] lines[li][1][0] += aim1.shape[1] li += 1 fig, ax = subplots() ax.add_collection(mc.LineCollection(lines)) imshow(concatenate((aim1, aim2), axis=1)) for p in l1: draw_circle(p[:2], p[2]) for p in l2: draw_circle(p[:2] + [aim1.shape[1], 0], p[2]) axis("off") savefig("matching_with_circles.png", bbox_inches="tight") show()
def extractSift(input_files, target_folder): all_features_dict = {} count = 0 for i, fname in enumerate(input_files): features_fname = target_folder + '/' + fname.split('/')[2].split( '.')[0] + '.sift' if exists(features_fname) == False: print("Calculating sift features for ", fname) sift.process_image(fname, features_fname, count) count += 1 locs, descriptors = sift.read_features_from_file(features_fname) all_features_dict[fname] = (locs, descriptors) os.chdir('..') return all_features_dict
def find_matches(image_names, root): l = {} d = {} n = len(image_names) for i, im in enumerate(image_names): resultname = os.path.join(root, '{}.sift'.format(im)) if not os.path.isfile(resultname): sift.process_image(os.path.join(root, '{}.png'.format(im)), resultname) l[i], d[i] = sift.read_features_from_file(resultname) matches = {} for i in range(n - 1): matches[i] = sift.match(d[i + 1], d[i]) return matches, l, d
def extractSift(input_files): all_features_dict = {} count = 0 for i,fname in enumerate(input_files): # path to store resulting sift files features_fname = 'sift_output/'+fname.split('/')[2].split('.')[0]+'.sift' if count == 0: os.chdir('siftDemoV4') print("Calculating sift features for ",fname) sift.process_image(fname,features_fname,count) count+=1 locs, descriptors = sift.read_features_from_file(features_fname) all_features_dict[fname] = descriptors os.chdir('..') return all_features_dict
def query_img(img): query_img_list = [] sift.process_image(img, './ukbench/tmp.sift') with open('vocabulary.pkl', 'rb') as f: voc = pickle.load(f) index = imagesearch.Indexer('test.db', voc) locs, descr = sift.read_features_from_file('./ukbench/tmp.sift') index.add_to_index(img, descr) index.db_commit() src = imagesearch.Searcher('test.db', voc) res = src.query(img)[:3] for dist, ndx in res: imname = src.get_filename(ndx) query_img_list.append(imname) return query_img_list
def extractSift(input_files): print "extracting Sift features" all_features_dict = {} for i, fname in enumerate(input_files): features_fname = fname + '.sift' if exists(features_fname) == False: print "calculating sift features for", fname sift.process_image(fname, features_fname) #print "gathering sift features for", fname, locs, descriptors = sift.read_features_from_file(features_fname) #print descriptors.shape all_features_dict[fname] = descriptors if (i % 100 == 0): print "finish ", str(i), " images extracting features" return all_features_dict
def extractSift(input_files): all_features_dict = {} count = 0 for i, fname in enumerate(input_files): # path to store resulting sift files features_fname = 'sift_output/' + fname.split('/')[2].split( '.')[0] + '.sift' if count == 0: os.chdir('siftDemoV4') print("Calculating sift features for ", fname) sift.process_image(fname, features_fname, count) count += 1 locs, descriptors = sift.read_features_from_file(features_fname) all_features_dict[fname] = descriptors os.chdir('..') return all_features_dict
def extractSift(input_files): print "extracting Sift features" all_features_dict = {} for i, fname in enumerate(input_files): rest_of_path = fname[:-(len(os.path.basename(fname)))] rest_of_path = os.path.join(rest_of_path, "sift") rest_of_path = os.path.join(rest_of_path, os.path.basename(fname)) features_fname = rest_of_path + '.sift' if os.path.exists(features_fname) == False: # print "calculating sift features for", fname sift.process_image(fname, features_fname) # print "gathering sift features for", fname, locs, descriptors = sift.read_features_from_file(features_fname) # print descriptors.shape all_features_dict[fname] = descriptors return all_features_dict
def extractMF(filename): features_fname = filename + '.sift' sift.process_image(filename, features_fname) locs, descriptors = sift.read_features_from_file(features_fname) sh = min(locs.shape[0], 1000) res = np.zeros((sh,SIZE_LOCAL_FEATURE)).astype(np.float32) extra = [20,False,True,False,0,0,0] WIN = 5 for i in range(sh): x = np.int32(round(locs[i][0])) y = np.int32(round(locs[i][1])) I = Image.open(filename) Nx,Ny = I.size a = sg.spec(I.crop((max(x-WIN,0),max(y-WIN,0),min(x+WIN,Nx-1),min(y+WIN,Ny-1))),extra) res[i] = a print res.shape return res
def add(file): targetImgSiftPath = file[:-3] + 'sift' sift.process_image(file, targetImgSiftPath) voc = vocabulary.Vocabulary('target') voc.train([targetImgSiftPath], 100, 10) print('vocabulary is:', voc.name, voc.nbr_words) locs, descr = sift.read_features_from_file(targetImgSiftPath) indx = imagesearch.Indexer('testImaAdd.db', voc) indx.add_to_index(file, descr) indx.db_commit() return voc
def __main__: nbr_images = len(imlist) featlist = [ imlist[i][:-3] + 'sif' for i in range(nbr_images)) for i in range(nbr_images): sift.process_image(imlist[i],featlist[i]) voc = vocabularly.Vocabulary('ukbenchtest') voc.train(featlist,1000,10) with open('vocabulary.pkl', 'wb') as f: pickle.dump(voc,f) print 'vocabulary is:', voc.name, voc.nbr_wods nbr_images = len(imlist) with open('vocabulary.pkl', 'rb') as f: voc = pickle.load(f) indx = imagesearch.Indexer('test.db',voc) indx.create_tables() for i in range(nbr_images)[:100]: locs,descr = sift.read_features_from_file(featlist[i]) indx.add_to_index(imlist[i],descr) indx.db_commit() con = sqlite.connect('test.db') print con.execute('select count (filename) from imlist').fetchone() print con.execute('select * from imlist').fetchone() src = imagesearch.Searcher('test.db') 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] print 'try a query...' print src.query(imlist[0])[:10]
def extractSift(input_files): print "extracting Sift features" all_features_dict = {} for i, fname in enumerate(input_files): siftFolderPath = dirname(fname) + "/sift" if not exists(siftFolderPath): makedirs(siftFolderPath) features_fname = siftFolderPath + "/" + basename(fname) + ".sift" if exists(features_fname) == False: print "calculating sift features for", fname sift.process_image(fname, features_fname) print "gathering sift features for", fname, locs, descriptors = sift.read_features_from_file(features_fname) print descriptors.shape all_features_dict[fname] = descriptors return all_features_dict
def extractSift(input_files): print "extracting Sift features" all_features_dict = {} #all_features = zeros([1,128]) for i, fname in enumerate(input_files): features_fname = fname + '.sift' if exists(features_fname) == False: print "calculating sift features for", fname sift.process_image(fname, features_fname) locs, descriptors = sift.read_features_from_file(features_fname) # print descriptors.shape all_features_dict[fname] = descriptors # if all_features.shape[0] == 1: # all_features = descriptors # else: # all_features = concatenate((all_features, descriptors), axis = 0) return all_features_dict
def plot_local_feats(imname1, imname2): aim1 = array(Image.open(imname1).convert('L')) aim2 = array(Image.open(imname2).convert('L')) offset = aim2.shape[0] - aim1.shape[0] aim1 = concatenate((aim1, zeros((offset, aim1.shape[1]))), axis=0) # We have to add a black padding/border to make the two images # the same size so they can be plotted together sift.process_image(imname1, 'tmp1.sift') sift.process_image(imname2, 'tmp2.sift') l1, d1 = sift.read_features_from_file('tmp1.sift') l2, d2 = sift.read_features_from_file('tmp2.sift') print l1.shape print d1.shape print d1 # Now we have the two features, we can build the distance matrix D = pairwise_dist(d1, d2) # for each feature in d1, find the minimum distance in d2 features that matches lines = empty((50, 2, 2)) # [ [[x1,y1], [x2,y2]], ...] li = 0 min_list = empty((D.shape[0])) for i in range(0, D.shape[0]): min_list[i] = min(D[i]) min_list = sort(min_list) for i in range(0, D.shape[0]): for j in range(0, D.shape[1]): if (D[i, j] in min_list[:50]): lines[li][0] = l1[i, :2] lines[li][1] = l2[j, :2] lines[li][1][0] += aim1.shape[1] li += 1 fig, ax = subplots() ax.add_collection(mc.LineCollection(lines)) plot_img = concatenate((aim1, aim2), axis=1) imshow(plot_img) axis("off") savefig("matching.png", bbox_inches=None, dpi=fig.dpi) show()
def detect_features(self, faces, image_filename): """ Detects Features in an list of faces and returns the images """ logging.debug('start detect %s features for file %s (train.py)' %(parameter.description_method, image_filename)) keypoints = [] descriptors = [] # detect features and save for i in range(len(faces)): if not parameter.face_enlargement == None: faces[i] = tools.enlarge_image(faces[i], parameter.face_enlargement) logging.debug('Cropped face from file %s has been enlarged with factor %.3f (surf-detector.py)' % (image_filename, parameter.face_enlargement)) face_numpy = np.asarray(faces[i]) # convert image for further processing # compute surf calculation if parameter.description_method == 'surf': surf = cv2.SURF(parameter.hessian_threshold, parameter.nOctaves, parameter.nOctaveLayers) # threshold, number of octaves, number of octave layers within each octave (http://opencv.itseez.com/modules/features2d/doc/feature_detection_and_description.html, http://www.mathworks.de/help/toolbox/vision/ref/detectsurffeatures.html) tmpkeypoints, tmpdescriptors = surf.detect(face_numpy, None, False) # extracting the SURF keys if len(tmpdescriptors) == 0: logging.warn('No descriptors found for a face in file %s (surf-detector.py)' % (image_filename)) else: tmpdescriptors.shape = (-1, surf.descriptorSize()) # change the shape of the descriptor from 1-dim to 2-dim (notwendig, damit die Funktionen - match_bruteforce - bei der Suche funktionieren) logging.info('%d Features found in file %s: face number %d (surf-detector.py)' % (len(tmpdescriptors), image_filename, (i+1))) # compute sift calculation if parameter.description_method == 'sift': cv.SaveImage('tmp-sift.jpg', faces[i]) sift.process_image('tmp-sift.jpg',"tmp.sift") l1,tmpdescriptors = sift.read_features_from_file("tmp.sift") tmpkeypoints = [] if tmpdescriptors == None: logging.warn('No descriptors found for a face in file %s (surf-detector.py)' % (image_filename)) else: for j in range(len(l1)): keypoint = cv2.KeyPoint(l1[j][0], l1[j][1], l1[j][2], l1[j][3]) tmpkeypoints.append(keypoint) logging.info('%d Features found in file %s: face number %d (surf-detector.py)' % (len(tmpdescriptors), image_filename, (i+1))) keypoints.append(tmpkeypoints) # add keypoints do list even when none are found descriptors.append(tmpdescriptors) # add descriptors do list even when none are found return(keypoints, descriptors)
def extractSift(input_files): print "extracting Sift features" all_features_dict = {} for i, fname in enumerate(input_files): features_fname = fname + '.sift' if exists(features_fname) == False: #print "calculating sift features for", fname sift.process_image(fname, features_fname) #print "gathering sift features for", fname, locs, descriptors = sift.read_features_from_file(features_fname) # check if there is description for the image if len(descriptors) > 0: print descriptors.shape all_features_dict[fname] = descriptors return all_features_dict
def loadImage(self, file): if file in self.dictFileImage: return self.dictFileImage[file]; else: id = self.totalImages; pixels = pylab.flipud(pylab.imread(file)); # SIFT features partName, partDot, partExt = file.rpartition('.'); keyFile = ''.join(partName + partDot + "key"); pgmFile = ''.join(partName + partDot + "pgm"); if os.path.exists(pgmFile) == False: #pylab.imsave(pgmFile, pixels); if len(pixels.shape) == 2: pilImage = Image.fromarray(pixels, 'L'); else: h = pixels.shape[0]; w = pixels.shape[1]; pixelsGray = np.matrix(np.zeros((h, w)), dtype=np.uint8); for i in range(h): for j in range(w): pixelsGray[i, j] = (np.mean(pixels[i, j])).astype(np.uint8); pilImage = Image.fromarray(pixelsGray, 'L'); pilImage.save(pgmFile); if os.path.exists(keyFile) == False: sift.process_image(pgmFile, keyFile); loc, des = sift.read_features_from_file(keyFile); #im.features = [Feature(im.id, des[i], loc[i]) for i in range(len(des))]; #print "Total features: ", len(im.features) im = ImageObject(id, pixels, loc, des); # add to dictionary self.dictFileImage[file] = im; self.dictIdImage[im.id] = im; # increase total images self.totalImages += 1; #print "Total images: ", self.totalImages; return im;
def get_H(im0_path, im1_path): """ Get the Homography matrix. """ # compute features sift.process_image(im0_path, 'im0.sift') l0, d0 = sift.read_features_from_file('im0.sift') sift.process_image(im1_path, 'im1.sift') l1, d1 = sift.read_features_from_file('im1.sift') # match features and estimate homography matches = sift.match_twosided(d0, d1) ndx = matches.nonzero()[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)[0] return H
def make_keypoints(img): ''' makes, saves the key file ''' if img.filename_resized: name = img.filename_resized else: name = img.filename kp_name = name.split('.')[-2] + '.key' if sift.process_image(img.filename_pgm, kp_name): img.filename_keypoints = kp_name return img else: print 'Error while making keypoints. Exit.' exit(-1)
def process(self): """ Run sift in a batch for each image in the given path """ for image in self.images: sift.process_image(image, SiftRunner.__get_sift_id__(image))
wid = 5 harrisim = harris.compute_harris_response(im1,5) filtered_coords1 = harris.get_harris_points(harrisim,wid+1) d1 = harris.get_descriptors(im1,filtered_coords1,wid) harrisim = harris.compute_harris_response(im2,5) filtered_coords2 = harris.get_harris_points(harrisim,wid+1) d2 = harris.get_descriptors(im2,filtered_coords2,wid) print 'starting matching' matches = harris.match_twosided(d1,d2) plt.figure() plt.gray() harris.plot_matches(im1,im2,filtered_coords1,filtered_coords2,matches) plt.show() ### 2.2.3 特徴点を検出する ### #sift.pyを作成する imname = 'empire.jpg' im1 = np.array(Image.open(imname).convert('L')) sift.process_image(imname,'empire.sift') l1,d1 = sift.read_features_from_file('empire.sift') plt.figure() plt.gray() sift.plot_features(im1,l1,circle=True) plt.show()
from PIL import Image from numpy import * from pylab import * import sift imname = 'pic/test.jpg' im1 = array(Image.open(imname).convert('L')) sift.process_image(imname, 'holmes.sift') l1, d1 = sift.read_features_from_file('holmes.sift') figure() gray() sift.plot_features(im1, l1, circle=True) show()
#!/usr/bin/env python import PIL.Image as Image import homography import sfm import sift from numpy import * from matplotlib.pylab import * # calibration K = array([[2394,0,932],[0,2398,628],[0,0,1]]) print "load images and compute features" im1 = array(Image.open('../data/alcatraz1.jpg')) sift.process_image('../data/alcatraz1.jpg','im1.sift') l1,d1 = sift.read_features_from_file('im1.sift') im2 = array(Image.open('../data/alcatraz2.jpg')) sift.process_image('../data/alcatraz2.jpg','im2.sift') l2,d2 = sift.read_features_from_file('im2.sift') print "match features" matches = sift.match_twosided(d1,d2) ndx = matches.nonzero()[0] # make homogeneous and normalize with inv(K) x1 = homography.make_homog(l1[ndx,:2].T) ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l2[ndx2,:2].T) x1n = dot(inv(K),x1) x2n = dot(inv(K),x2) print "estimate E with RANSAC" model = sfm.RansacModel() E,inliers = sfm.F_from_ransac(x1n,x2n,model) print "compute camera matrices (P2 will be list of four solutions)" P1 = array([[1,0,0,0],[0,1,0,0],[0,0,1,0]]) P2 = sfm.compute_P_from_essential(E)
import sift from glob import glob EXTENSIONS = [".jpg", ".bmp", ".png", ".pgm", ".tif", ".tiff"] def get_categories(datasetpath): cat_paths = [files for files in glob(datasetpath + "/*") if isdir(files)] cat_paths.sort() cats = [basename(cat_path) for cat_path in cat_paths] return cats def get_imgfiles(path): all_files = [] all_files.extend([join(path, basename(fname)) for fname in glob(path + "/*") if splitext(fname)[-1].lower() in EXTENSIONS]) return all_files dataset_path = '../flower_rec1/square_images128_sift/test' categories = get_categories(dataset_path) for category in categories: category_path = join(dataset_path, category) image_file_list = get_imgfiles(category_path) num_images = len(image_file_list) feature_file_list = [image_file_list[i][:-3]+'sift' for i in range(num_images)] for i in range(num_images): sift.process_image(image_file_list[i], feature_file_list[i])
import sift if len(sys.argv) >= 3: im1f, im2f = sys.argv[1], sys.argv[2] else: # im1f = '../data/sf_view1.jpg' # im2f = '../data/sf_view2.jpg' im1f = '../data/crans_1_small.jpg' im2f = '../data/crans_2_small.jpg' # im1f = '../data/climbing_1_small.jpg' # im2f = '../data/climbing_2_small.jpg' im1 = array(Image.open(im1f)) im2 = array(Image.open(im2f)) sift.process_image(im1f, 'out_sift_1.txt') l1, d1 = sift.read_features_from_file('out_sift_1.txt') figure() gray() subplot(121) sift.plot_features(im1, l1, circle=False) sift.process_image(im2f, 'out_sift_2.txt') l2, d2 = sift.read_features_from_file('out_sift_2.txt') subplot(122) sift.plot_features(im2, l2, circle=False) #matches = sift.match(d1, d2) matches = sift.match_twosided(d1, d2) #print '{} matches'.format(len(matches.nonzero()[0]))
def Q4(qname): QFnumber = qname[18:21] QFnumber = int(QFnumber) d1_list = [] d1_length_list = [] statistics_list = [] dst_list_Q4 = [] fnum = 0 for fname in glob.glob('dataset1/*.jpg'): #print fname fnum += 1 if os.path.isfile(fname + '.sift') == False : im = array(Image.open(fname).convert('L')) sift.process_image(fname, fname+'.sift') l1, d1 = sift.read_features_from_file(fname+'.sift') d1_length_list.append(len(d1)) for x in d1: d1_list.append(x) #print "d1_length_list" #print d1_length_list #print "---clusters---" a = KMeans(n_clusters=100).fit(d1_list) #print "a" #print len(a.labels_) #print a.labels_ #print "---" # find max total_0, total_1, total_2 = 0, 0, 0 for i in range(len(a.labels_)): if a.labels_[i] == 0: total_0 += 1 elif a.labels_[i] == 1: total_1 += 1 elif a.labels_[i] == 2: total_2 += 1 #print "total_0: %d, total_1: %d, total_2: %d" % (total_0, total_1, total_2) #print "max *= 0.9" ignore = 0 if( total_0 > total_1 and total_0 > total_2): total_0 *= 0.9 ignore = 0 elif( total_1 > total_0 and total_1 > total_2): total_1 *= 0.9 ignore = 1 elif( total_2 > total_0 and total_2 > total_1): total_2 *= 0.9 ignore = 2 #print "total_0: %d, total_1: %d, total_2: %d" % (total_0, total_1, total_2) # Statistic the number of 0, 1, 2 in each file counter_0, counter_1, counter_2, start, end = 0, 0, 0, 0, 0 for f in range(fnum): #print "f = %d" % f end += d1_length_list[f] #print ">>>> %d ~ %d" % (start, end) counter_0, counter_1, counter_2 = 0, 0, 0 for i in range(start, end): if a.labels_[i] == 0: counter_0 += 1 elif a.labels_[i] == 1: counter_1 += 1 elif a.labels_[i] == 2: counter_2 += 1 start += d1_length_list[f] if ignore == 0: counter_0 *= 0.9 elif ignore == 1: counter_1 *= 0.9 elif ignore == 2: counter_2 *= 0.9 statistics_list.append((counter_0, counter_1, counter_2)) #print "0: %d, 1:%d, 2:%d ---- total:%d" % (counter_0, counter_1, counter_2, counter_0+counter_1+counter_2) #print statistics_list # Querying for f in range(fnum): #print "f = %d" % f dst = np.sqrt((statistics_list[QFnumber][0]-statistics_list[f][0])**2 + (statistics_list[QFnumber][1]-statistics_list[f][1])**2 + (statistics_list[QFnumber][2]-statistics_list[f][2])**2) if (0 <= f and f < 10): fname = "dataset1/ukbench0000"+ str(f) +".jpg" elif (10 <= f and f < 100): fname = "dataset1/ukbench000"+ str(f) +".jpg" else: fname = "dataset1/ukbench00"+ str(f) +".jpg" #print fname dst_list_Q4.append((fname,dst)) global dst_list_Q4_unsorted dst_list_Q4_unsorted = dst_list_Q4 # sort by distance dst_list_Q4.sort(key=itemgetter(1)) print "Q4. Using SIFT Visual Words with Stop Words---" print_similarity(dst_list_Q4)
fileSize = 0 folderCount = 0 rootdir = './dataset' for root, subFolders, files in os.walk(rootdir): folderCount += len(subFolders) for file in files: f = os.path.join(root, file) fileSize = fileSize + os.path.getsize(f) #print(f) fileList.append(f) print("Total Size is {0} bytes".format(fileSize)) print("Total Files ", len(fileList)) print("Total Folders ", folderCount) images_quantities = len(fileList) for i in range(0, images_quantities): temp_str = str(i) temp_str_fillZero = temp_str.zfill(5) i_th_fileName = "./dataset/ukbench" + temp_str_fillZero + ".jpg" #im1 = array(Image.open(i_th_fileName).convert('L')) outputFilename = './siftset/' + i_th_fileName[-16:-4] + '.sift' sift.process_image(i_th_fileName, outputFilename) print temp_str + " complete"
Created on Sat Aug 20 20:05:22 2016 @author: user """ from PIL import Image from numpy import * import sys sys.path.append('../ch2/') import sift import homography featname = ['../pcv_data/data/Univ' + str(i + 1) + '.sift' for i in range(5)] imname = ['../pcv_data/data/Univ' + str(i + 1) + '.jpg' for i in range(5)] l = {} d = {} for i in range(5): sift.process_image(imname[i], featname[i]) l[i], d[i] = sift.read_features_from_file(featname[i]) matches = {} for i in range(4): matches[i] = sift.match(d[i + 1], d[i]) # visualize the matches (Figure 3-11 in the book) for i in range(4): im1 = array(Image.open(imname[i])) im2 = array(Image.open(imname[i + 1])) figure() sift.plot_matches(im2, im1, l[i + 1], l[i], matches[i], show_below=True) '''def convert_points(j): ndx = matches[j].nonzero()[0] fp = homography.make_homog(l[j+1][ndx,:2].T) ndx2 = [int(matches[j][i]) for i in ndx] tp = homography.make_homog(l[j][ndx2,:2].T)
def extractSift(filename): features_fname = filename + '.sift' sift.process_image(filename, features_fname) locs, descriptors = sift.read_features_from_file(features_fname) return descriptors
# -*- coding: utf-8 -*- from pylab import * from PIL import Image from mpl_toolkits.mplot3d import axes3d import homography import sfm import sift import camera # 标定矩阵 K = array([[2394, 0, 932], [0, 2398, 628], [0, 0, 1]]) # 载入图像,并计算特征 im1 = array(Image.open("../data/alcatraz1.jpg")) sift.process_image("../data/alcatraz1.jpg", "im1.sift") l1, d1 = sift.read_features_from_file("im1.sift") im2 = array(Image.open("../data/alcatraz2.jpg")) sift.process_image("../data/alcatraz2.jpg", "im2.sift") l2, d2 = sift.read_features_from_file("im2.sift") # 匹配特征 matches = sift.match_twosided(d1, d2) ndx = matches.nonzero()[0] # 使用齐次坐标表示,并使用inv(K)归一化 x1 = homography.make_homog(l1[ndx, :2].T) ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l2[ndx2, :2].T)
Created on 2013/04/10 @author: n_shimada ''' import os from os import path import sys sys.path.append('C:/workPyth/Mypython/cv') import sift from PIL import Image import numpy as np from pylab import * #c_path = str(os.getcwd()) c_path = "C:/workPyth/MyPython/cv/" imname = os.path.join(c_path, "4l0le8.jpg") print imname im1 = np.array(Image.open(imname).convert('L')) try: sift.process_image(imname,"4l0le8.sift") except Exception, e: import sys import traceback sys.stderr.write(traceback.format_exc()) l1, d1 = sift.read_features_from_file("4l0le8.sift") figure() gray() sift.plot_features(im1, l1, circle = True) show()
import cv2 import os import numpy as np import matplotlib.pyplot as plt import sift from PIL import Image imname = 'sift1.jpg' im1 = np.array(Image.open(imname).convert('L')) sift.process_image(imname,'sift1.sift') l1, d1 = sift.read_features_from_file('sift1.sift') plt.figure() plt.gray() sift.plot_features(im1, l1, circle=True) plt.show()
def my_calibration(sz): """ Calibration function for the camera (iPhone4) used in this example. """ row, col = sz fx = 2555*col/2592 fy = 2586*row/1936 K = diag([fx, fy, 1]) K[0, 2] = 0.5*col K[1, 2] = 0.5*row return K # compute features sift.process_image('../data/book_frontal.JPG', 'im0.sift') l0, d0 = sift.read_features_from_file('im0.sift') sift.process_image('../data/book_perspective.JPG', 'im1.sift') l1, d1 = sift.read_features_from_file('im1.sift') # match features and estimate homography matches = sift.match_twosided(d0, d1) ndx = matches.nonzero()[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, inliers = homography.H_from_ransac(fp, tp, model)
""" import os import cPickle as pickle import tic import sift import vocabulary from imtools import get_imageList imlist = get_imageList("../local/data/JianDa1") imcount = len(imlist) print imlist print imcount featlist = [imlist[i][:-3] + 'sift' for i in range(imcount)] tic.k('Start') for i in range(imcount): if not os.path.exists(featlist[i]): sift.process_image(imlist[i], featlist[i]) tic.k('sift loaded') voc = vocabulary.Vocabulary('JianDa1') # ukbenchtest voc.train(featlist, k=imcount, subsampling=10) tic.k('train loaded') # 保存词汇 #imagepkl = r"pickle\vocabulary.pkl" imagepkl = r"../static\pickle\jianda1.pkl" with open(imagepkl, 'wb') as f: pickle.dump(voc, f) print imagepkl, 'is:', voc.name, voc.word_count
@author: user """ from PIL import Image from numpy import * sys.path.append('../ch2/') sys.path.append('../ch3/') sys.path.append('../ch4/') import homography import sfm import sift # calibration K = array([[2394, 0, 932], [0, 2398, 628], [0, 0, 1]]) # load images and compute features im1 = array(Image.open('../pcv_data/data/alcatraz1.jpg')) sift.process_image('../pcv_data/data/alcatraz1.jpg', 'im1.sift') l1, d1 = sift.read_features_from_file('im1.sift') im2 = array(Image.open('../pcv_data/data/alcatraz2.jpg')) sift.process_image('../pcv_data/data/alcatraz2.jpg', 'im2.sift') l2, d2 = sift.read_features_from_file('im2.sift') # match features matches = sift.match_twosided(d1, d2) ndx = matches.nonzero()[0] # make homogeneous and normalize with inv(K) x1 = homography.make_homog(l1[ndx, :2].T) ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l2[ndx2, :2].T) x1n = dot(inv(K), x1) x2n = dot(inv(K), x2) # estimate E with RANSAC model = sfm.RansacModel()
os.mkdir(imgfolder) for url in imurls: image = urllib.URLopener() file_name = imgfolder+'/'+os.path.basename(urlparse.urlparse(url).path) image.retrieve(url,file_name) imlist.append(file_name) print 'download:', url ####################################################### import sift nbr_images = len(imlist) featlist = [] for i in range(nbr_images): featfile = os.path.splitext(imlist[i])[0]+'.sift' print 'generating features for: ',imlist[i] sift.process_image(imlist[i],featfile) featlist.append(featfile) ###################################################### 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])
p.append([c[0] - wid, c[1] - wid, c[2] + wid]) # 竖直边 p.append([c[0] - wid, c[1] - wid, c[2] + wid]) p.append([c[0] - wid, c[1] + wid, c[2] + wid]) p.append([c[0] - wid, c[1] + wid, c[2] - wid]) p.append([c[0] + wid, c[1] + wid, c[2] - wid]) p.append([c[0] + wid, c[1] + wid, c[2] + wid]) p.append([c[0] + wid, c[1] - wid, c[2] + wid]) p.append([c[0] + wid, c[1] - wid, c[2] - wid]) return array(p).T # 计算特征 sift.process_image('book_main.jpg', 'im0.sift') l0, d0 = sift.read_features_from_file('im0.sift') imname = 'book_main.jpg' im1 = array(Image.open(imname).convert('L')) l1, d1 = sift.read_features_from_file('im0.sift') figure() gray() sift.plot_features(im1, l1, circle=True) show()
#!/usr/bin/env python # -*- coding: utf-8 -*- from PIL import Image from numpy import array import sift from pylab import * import os datapath = "./../dataset" imname = 'Penguins.jpg' for dirs, root, files in os.walk(datapath): for filename in files: #im1 = array(Image.open(datapath + filename).convert('L')) name = filename.split(',') sift.process_image(filename, name[0] + '.sift') #l1,d1 = sift.read_features_from_file('Penguins.sift') ''' figure() gray() sift.plot_features(im1,l1,circle=True) show() '''
import numpy as np from PIL import Image import matplotlib.pyplot as plt # calibration #K = np.array([[2394, 0, 932], [0, 2398, 628], [0, 0, 1]]) K = np.array([[1253, 0, 866], [0, 2137, 1155], [0, 0, 1]]) #K = np.array([[1670, 0, 1155], [0, 1604, 866], [0, 0, 1]]) # load images and compute features #im1_path = 'C:/Users/User/Desktop/course/3D/project/pcv_data/alcatraz1.jpg' #im2_path = 'C:/Users/User/Desktop/course/3D/project/pcv_data/alcatraz2.jpg' im1_path = './e1.jpg' im2_path = './e2.jpg' im1 = np.array(Image.open(im1_path)) sift.process_image(im1_path, 'im1.sift') l1, d1 = sift.read_features_from_file('im1.sift') im2 = np.array(Image.open(im2_path)) sift.process_image(im2_path, 'im2.sift') l2, d2 = sift.read_features_from_file('im2.sift') # match features matches = sift.match_twosided(d1, d2) ndx = matches.nonzero()[0] # make homogeneous and normalize with inv(K) x1 = homography.make_homog(l1[ndx, :2].T) ndx2 = [int(matches[i]) for i in ndx] x2 = homography.make_homog(l2[ndx2, :2].T)
from PIL import Image from numpy import * from pylab import * import sift import imtools import os from os.path import basename, splitext im_path = os.getcwd() imlist = imtools.get_imlist('nail_book') featlist = [] for imname in imlist: name, ext = splitext(basename(imname)) sname = name + '.sift' sift.process_image(imname, sname) featlist.append(sname) nbr_images = len(imlist) matchscores = zeros((nbr_images, nbr_images)) for i in xrange(nbr_images): for j in xrange(i, nbr_images): # 上三角成分だけを計算する 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)