def image_fisher_featurize_sift(im_keys, out_matrix, bidx, gmm_sift, pca_sift_mat, pca_sift_mu): features = [] t = time.time() for im_key in im_keys: s3 = boto3.resource('s3') client = boto3.client('s3') bio = io.BytesIO( client.get_object(Bucket="pictureweb", Key=im_key)["Body"].read()) im = scipy.misc.imread(bio, flatten=True) im = im.astype('float32') im /= 255.0 descs = sift.sift(im) descs = (descs).dot(pca_sift_mat) sift_features = fisher.fisher_vector_features(descs.astype('float32'), *gmm_sift) features.append(sift_features) #sqrt normalization signs = np.sign(features) features = signs * np.sqrt(np.abs(features)) feature_norms = np.linalg.norm(features, axis=1)[:, np.newaxis] features /= feature_norms out_matrix.put_block(features, bidx, 0) e = time.time() return e - t
def init_photos_reconstruction(dirpath, str1, str2): # 读入图像 image1 = cv2.imread(dirpath + str1) image2 = cv2.imread(dirpath + str2) # image1 = cv2.resize(image1, (1000, 750)) # image2 = cv2.resize(image2, (1000, 750)) # sift、surf + KDTree sift_pts = sift.sift(image1, image2) surf_pts = surf.surf(image1, image2) pts = sift_pts + surf_pts # pts使用RANSAC, 返回RANSAC后的匹配点、本征矩阵、R、t矩阵、匹配点以及相机内参 pts, essentialMat, R, t, src_pts, dst_pts, K = RANSAC.compute(pts) # 透视变换并保存 # image3 = cv2.warpPerspective(image1, M, (1000, 720)) # cv2.imwrite("warpPerspective.tif", image3) global projMatr, RList, tList, points, colors points4D, color4D, Mat = sfm.triangulate_compute(projMatr, R, t, src_pts, dst_pts, K, image1) projMatr = Mat RList.append(projMatr[:, 0:3]) tList.append(projMatr[:, 3:4]) points.append(points4D) colors.append(color4D) # 水平拼接 # image = np.hstack((image1, image2)) # 存储点云信息 # save.save_data(image1, src_pts, R, t, points4D) # 显示匹配线段 # sift.show_line(image, pts) # cv2.waitKey() # 显示点云 # os.system('show') return dst_pts, points4D
def main(): print "opening image" im = Image.open("test.pgm") l = list(im.getdata()) l.insert(0,im.size) #the image data must be a list of pixels in row order #(that is, the first row, then the second row etc) #with a (width,height) tuple as the first element #the image must be grayscale (one channel) print "running sift" #this will calculate the keypoints and then the sift descriptors for each desc = sift.sift(l,verbose=True) print "done" print len(desc), "descriptors found" #print desc output = open('results.txt','w') for a in desc: x = a[0] y = a[1] im.putpixel((x,y),int(255)) printDescriptor(a,output) output.close() im.show()
def generateSift(self, imgOrFilename): #if isinstance(imgOrFilename, str): #grayImg = cv2.imread(imgOrFilename, cv2.CV_LOAD_IMAGE_GRAYSCALE) return sift.sift().calculateSiftDescriptorFromFile(imgOrFilename, self.gridSpacing, self.patchSize, self.maxImSize, self.nrml_threshold) #grayImg = grayImg = cv2.cvtColor(imgOrFilename, cv2.COLOR_BGR2GRAY) #else: #grayImg = cv2.cvtColor(imgOrFilename, cv2.COLOR_BGR2GRAY) #return sift.sift().calculateSiftDescriptor(imgOrFilename, self.gridSpacing, self.patchSize, self.maxImSize, self.nrml_threshold)
def extract_features(image_paths): Features = [] # 所有图像的所有sift descriptor的列表 for i, image_path in enumerate(image_paths): img = cv.imread(image_path) gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY) kp, img_des = sift.sift(image_path) Features.append(img_des) return Features
def image_fisher_featurize(im_key, gmm_sift, gmm_lcs, pca_sift, pca_lcs): t = time.time() s3 = boto3.resource('s3') s3.Bucket("pictureweb").download_file(im_key, "/tmp/img.jpg") im = scipy.misc.imread("/tmp/img.jpg", flatten=True) descs = sift(im).dot(pca_sift.T) sift_features = fisher.fisher_vector_features(descs.astype('float32'), *gmm_sift) im = scipy.misc.imread("/tmp/img.jpg") descs = lcs(im).reshape(-1, 96).dot(pca_lcs.T) lcs_features = fisher.fisher_vector_features(descs.astype('float32'), *gmm_lcs) out_features = np.hstack((sift_features, lcs_features)).T e = time.time() return out_features, e - t
def demo(imgfile0,imgfile1,rth0,rth1,mth): # read image, convert to gray #img = io.imread(sys.argv[1]); img=io.imread("data/plane.bmp") img0 = io.imread(imgfile0) img1 = io.imread(imgfile1) print("orig0 shape: "+str(img0.shape)) print("orig1 shape: "+str(img1.shape)) gry0 = rgb2gray(img0) gry1 = rgb2gray(img1) print("gray0 shape: "+str(gry0.shape)) print("gray1 shape: "+str(gry1.shape)) # get list of locations of interest points feat0 = harris(gry0,gk(3,3,1),rth0) feat1 = harris(gry1,gk(3,3,1),rth1) print("number of features0: "+str(len(feat0))) print("number of features1: "+str(len(feat1))) # make sift descriptors des0 = sift(gry0,feat0) des1 = sift(gry1,feat1) showFeatures(img0,feat0,des0) showFeatures(img1,feat1,des1) M = matchSIFT(des0,des1,40,mth) img2 = drawMatches(img0,feat0,img1,feat1,M) io.imshow( img2,vmin=0,vmax=255,cmap="gray") io.show()
def main(): print "opening image" im = Image.open("test.pgm") l = list(im.getdata()) l.insert(0,im.size) #the image data must be a list of pixels in row order #(that is, the first row, then the second row etc) #with a (width,height) tuple as the first element #the image must be grayscale (one channel) print "running sift" #we do this to load in a file of keypoints so that sift #doesnt have to find them (or we can guide it) #takes the first 4 floats per line and puts in a tuple input = open('keypoints.txt','r') keys = [] for a in input: keys.append(tuple(map(float,a.split()[0:4]))) input.close() #keys must be a list of 4-tuples for x,y,sigma and theta of each keypoint desc = sift.sift(l,keypoints=keys,verbose=True) print "done" print len(desc), "descriptors found" #print desc output = open('results.txt','w') for a in desc: x = a[0] y = a[1] im.putpixel((x,y),int(255)) printDescriptor(a,output) output.close() im.show()
def calculate_sifts(img_keys, out_matrix, block_idx, descs_per_img=16): sifts = [] s3 = boto3.resource('s3') import time t = time.time() np.random.seed(block_idx) for im_key in img_keys: s3.Bucket("pictureweb").download_file(im_key, "/tmp/img.jpg") im = scipy.misc.imread("/tmp/img.jpg", flatten=True) im = im.astype('float32') im /= 255.0 descs = sift.sift(im) assert (np.all(descs >= 0)) idxs = np.random.choice(descs.shape[0], descs_per_img) descs = np.sqrt(descs) sifts.append(descs[idxs, :]) out_matrix.put_block(np.vstack(sifts), block_idx, 0) e = time.time() return e - t
def starter(): image_id, model = input("Enter image ID and model name (LBP or SIFT): ").split() constants_dict = constants.read_json() read_path = constants_dict["READ_PATH"] write_path = constants_dict["WRITE_PATH"] files = os.listdir(read_path) file = files[files.index("{}.jpg".format(image_id))] img = cv2.imread(read_path + file) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) if model == "LBP": print(lbp.lbp(gray)) else: print(sift.sift(gray)) print("Done... ")
def photos_reconstruction(dirpath, str1, str2, pre_dst_pts, pre_points4D): # 读入图像 image1 = cv2.imread(dirpath + str1) image2 = cv2.imread(dirpath + str2) # image1 = cv2.resize(image1, (1000, 750)) # image2 = cv2.resize(image2, (1000, 750)) # sift、surf + KDTree sift_pts = sift.sift(image1, image2) surf_pts = surf.surf(image1, image2) pts = sift_pts + surf_pts # pts使用RANSAC, 返回RANSAC后的匹配点、本征矩阵、R、t矩阵、匹配点以及相机内参 pts, essentialMat, R, t, src_pts, dst_pts, K = RANSAC.compute(pts) src_pts_PnP, src_pts_PnP4D = kdtree.KDTree_match_pre(pre_dst_pts, pre_points4D, src_pts, dst_pts) # print(src_pts_PnP, src_pts_PnP4D) retval, rvec, t, inliers = cv2.solvePnPRansac(src_pts_PnP4D, src_pts_PnP, K, np.zeros(4), flags=cv2.SOLVEPNP_EPNP, reprojectionError=8.0, confidence=0.99) R = cv2.Rodrigues(rvec)[0] # 透视变换并保存 # image3 = cv2.warpPerspective(image1, M, (1000, 720)) # cv2.imwrite("warpPerspective.tif", image3) global projMatr, RList, tList, points, colors print(t) # print(projMatr[:, 3:4]) points4D, color4D, Mat = sfm.triangulate_compute_pre(projMatr, R, t, src_pts, dst_pts, K, image1) projMatr = Mat RList.append(projMatr[:, 0:3]) tList.append(projMatr[:, 3:4]) points.append(points4D) colors.append(color4D) # 水平拼接 # image = np.hstack((image1, image2)) # 存储点云信息 # save.save_data(image1, src_pts, R, t, points4D) # 显示匹配线段 # sift.show_line(image, pts) # cv2.waitKey() # 显示点云 # os.system('show') return dst_pts, points4D
def store_feature_vectors(collection, read_path, write_path): files = os.listdir(read_path) for file in files: print("Reading file: {}".format(file)) img = cv2.imread(read_path + file) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) lbp_results = lbp.lbp(gray) sift_results = sift.sift(gray) feature_vector = { "name": file.replace(".jpg", ""), "lbp": lbp_results.tolist(), "sift": sift_results.tolist() } with open("{}{}_fd.json".format(write_path, file.replace(".jpg", "")), "w") as fp: json.dump(feature_vector, fp, indent=4, sort_keys=True) feature_vector["lbp"] = Binary(pickle.dumps(lbp_results)) feature_vector["sift"] = Binary(pickle.dumps(sift_results)) collection.insert_one(feature_vector)
means = convert_keystone_csv_to_numpy("./gmmMeans_lcs.csv").T covars = convert_keystone_csv_to_numpy("./gmmVars_lcs.csv").T descs = pca_mat.dot(descs) pca_keystone = convert_keystone_csv_to_numpy("./pca_keystone_lcs.txt") gmm = (means, covars, weights) fv_keystone = convert_keystone_csv_to_numpy("./fisher_keystone_lcs.txt") fv_features = fisher.fisher_vector_features( descs.astype('float32').T, *gmm) print("AVG LCS DIFF ", np.mean(np.abs(fv_features - fv_keystone))) print("MAX LCS DIFF ", np.max(np.abs(fv_features - fv_keystone))) im = scipy.misc.imread("./ILSVRC2012_val_00000293.JPEG", flatten=True) im /= 255.0 descs = sift.sift(im).T descs_keystone = C_ARRAY( convert_keystone_csv_to_numpy("./sift_imagenet.txt")) pca_mat = convert_keystone_csv_to_numpy("./pcaMat_sift.csv") weights = convert_keystone_csv_to_numpy("./gmmCoefs_sift.csv") means = convert_keystone_csv_to_numpy("./gmmMeans_sift.csv").T covars = convert_keystone_csv_to_numpy("./gmmVars_sift.csv").T descs = pca_mat.dot(descs) pca_keystone = convert_keystone_csv_to_numpy("./pca_keystone_sift.txt") gmm = (means, covars, weights) fv_keystone = convert_keystone_csv_to_numpy("./fisher_keystone_sift.txt") fv_features = fisher.fisher_vector_features( descs.astype('float32').T, *gmm)
import sift if __name__ == '__main__': s = sift.sift() finre, scal = s.match("./img/1.png", "./img/2.png") for i in finre: print(i[0]) print(i[1]) print(scal)
def image_fisher_featurize_sift_lcs(im_keys, out_matrix, bidx, gmm_sift, pca_sift_mat, gmm_lcs, pca_lcs_mat): all_sift_features = [] all_lcs_features = [] t = time.time() for im_key in im_keys: s3 = boto3.resource('s3') client = boto3.client('s3') bio = io.BytesIO( client.get_object(Bucket="pictureweb", Key=im_key)["Body"].read()) im = scipy.misc.imread(bio, flatten=True) im = im.astype('float32') im /= 255.0 sift_descs = sift.sift(im) assert (np.all(sift_descs >= 0)) sift_descs = np.sqrt(sift_descs) sift_descs = (sift_descs).dot(pca_sift_mat) sift_features = fisher.fisher_vector_features( sift_descs.astype('float32'), *gmm_sift) sift_features /= np.linalg.norm(sift_features) bio = io.BytesIO( client.get_object(Bucket="pictureweb", Key=im_key)["Body"].read()) im = scipy.misc.imread(bio) lcs_descs = lcs.lcs(im).reshape(-1, LCS_DESC_LENGTH) try: assert (np.any(np.isnan(lcs_descs)) == False) except: raise Exception("RAISING LCS Error pre pca in {0}".format(im_key)) lcs_descs = (lcs_descs).dot(pca_lcs_mat) try: assert (np.any(np.isnan(lcs_descs)) == False) except: raise Exception("RAISING LCS Error post pca in {0}".format(im_key)) lcs_features = fisher.fisher_vector_features( lcs_descs.astype('float32'), *gmm_lcs) lcs_features /= np.linalg.norm(lcs_features) try: assert (np.any(np.isnan(lcs_features)) == False) except: raise Exception( "RAISING LCS Fisher Vector Error in {0}".format(im_key)) all_sift_features.append(sift_features) all_lcs_features.append(lcs_features) all_sift_features = np.array(all_sift_features) all_lcs_features = np.array(all_lcs_features) assert (np.any(np.isnan(all_sift_features)) == False) #sqrt normalization signs = np.sign(all_sift_features) all_sift_features = signs * np.sqrt(np.abs(all_sift_features)) feature_norms = np.linalg.norm(all_sift_features, axis=1)[:, np.newaxis] assert (np.any(np.isnan(feature_norms)) == False) all_sift_features /= feature_norms assert (np.any(np.isnan(all_sift_features)) == False) assert (np.any(np.isnan(all_lcs_features)) == False) signs = np.sign(all_lcs_features) all_lcs_features = signs * np.sqrt(np.abs(all_lcs_features)) feature_norms = np.linalg.norm(all_lcs_features, axis=1)[:, np.newaxis] assert (np.any(np.isnan(feature_norms)) == False) all_lcs_features /= feature_norms assert (np.any(np.isnan(all_lcs_features)) == False) features = np.hstack((all_sift_features, all_lcs_features)) out_matrix.put_block(features, bidx, 0) e = time.time() return e - t, t, e
def printDescriptor(desc,file): map(lambda a: file.write(str(a)+' '),desc) file.write('\n') if 0: a = np.zeros((128,128),dtype=np.uint8) a[40:70,20:50] = 255 im = Image.fromarray(a,mode='L') else: im = Image.open(input_image) a = np.asarray(im) a = np.flipud(a) l = list(im.getdata()) l.insert(0,im.size) desc = sift.sift(l,verbose=True) print len(desc), "descriptors found" output = open(output_descriptors,'w') pl.close() pl.imshow(np.asarray(im),cmap="gray", origin='upper') pl.axis('off') r = np.linspace(0,2*math.pi,100) for a in desc: x,y,scale,ang = a[:4] im.putpixel((x,y),int(255)) pl.plot(x+scale/2*np.cos(r),y+scale/2*np.sin(r),'r') pl.plot((x,x+scale/2*math.cos(ang)),(y,y+scale/2*math.sin(ang)),'g')