for ndx in res_reg[1:]: locs,descr = sift.read_features_from_file(featlist[ndx]) # get matches matches = sift.match(q_descr,descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:,:2].T) try: H,inliers = homography.H_from_ransac(fp[:,ind],tp[:,ind2],model,match_theshold=4) except: inliers = [] # store inlier count rank[ndx] = len(inliers) sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res_reg[0]]+[s[0] for s in sorted_rank] print ('top matches (homography):', res_geom)
时间: 2024-04-21 18:29:15 浏览: 18
这段代码是一个重要的部分,它是用来进行单应性矩阵和RANSAC算法的匹配和筛选的。具体来说,代码中的for循环遍历了常规查询得到的候选图像列表,从中载入了特征描述符和关键点,并使用SIFT算法的match函数来获取查询图像和候选图像的匹配点对。然后使用homography.make_homog函数将这些匹配点对转换为齐次坐标形式,并使用homography.H_from_ransac函数来估计单应性矩阵,并使用match_theshold参数来指定匹配点对的阈值。最后根据匹配点对的数量来计算得分,并将得分按降序排序,得到重排后的查询结果(res_geom)。
相关问题
# -*- coding: utf-8 -*- import pickle from PCV.localdescriptors import sift from PCV.imagesearch import imagesearch from PCV.geometry import homography from PCV.tools.imtools import get_imlist #载入图像列表 imlist = get_imlist('oxbuild/') nbr_images = len(imlist) #载入特征列表 featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)] #载入词汇 with open('oxbuild/vocabulary.pkl', 'rb') as f: voc = pickle.load(f) src = imagesearch.Searcher('testImaAdd.db',voc) #查询图像索引和查询返回的图像数 q_ind = 892 nbr_results = 20 # 常规查询(按欧式距离对结果排序) res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]] print ('top matches (regular):', res_reg) #载入查询图像特征 q_locs,q_descr = sift.read_features_from_file(featlist[q_ind]) fp = homography.make_homog(q_locs[:,:2].T) #用单应性进行拟合建立RANSAC模型 model = homography.RansacModel() rank = {} #载入候选图像的特征 for ndx in res_reg[1:]: locs,descr = sift.read_features_from_file(featlist[ndx]) # get matches matches = sift.match(q_descr,descr) ind = matches.nonzero()[0] ind2 = matches[ind] tp = homography.make_homog(locs[:,:2].T) try: H,inliers = homography.H_from_ransac(fp[:,ind],tp[:,ind2],model,match_theshold=4) except: inliers = [] # store inlier count rank[ndx] = len(inliers) sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True) res_geom = [res_reg[0]]+[s[0] for s in sorted_rank] print ('top matches (homography):', res_geom) # 显示查询结果 imagesearch.plot_results(src,res_reg[:8]) #常规查询 imagesearch.plot_results(src,res_geom[:8]) #重排后的结果
这段代码是一个图像检索的例子,其中使用了SIFT算法提取图像的关键点和特征描述符,使用了单应性矩阵和RANSAC算法进行匹配和筛选,使用了词汇表和倒排索引技术来加速查询。具体来说,代码中首先载入了图像列表和特征列表,然后载入了之前训练好的词汇表,接着创建了一个图像搜索器Searcher。在进行查询时,首先使用常规查询(按欧式距离对结果排序)来获取最初的查询结果,然后使用单应性矩阵和RANSAC算法对查询图像和候选图像进行匹配和筛选,得到重排后的查询结果。最后使用plot_results函数来展示查询结果。
def block_SVD(M, blk_size): row_block = M.shape[0] // blk_size[0] col_block = M.shape[1] // blk_size[1] U = np.empty((row_block, col_block), dtype=object) S = np.empty((row_block, col_block), dtype=object) V = np.empty((row_block, col_block), dtype=object) M = M[:M.shape[0] - M.shape[0] % blk_size[0], :M.shape[1] - M.shape[1] % blk_size[1]] rows = [] for i in range(0, M.shape[0], blk_size[0]): cols = [] for j in range(0, M.shape[1], blk_size[1]): max_ndx = (min(i + blk_size[0], M.shape[0]), min(j + blk_size[1], M.shape[1])) u, s, v = np.linalg.svd(M[i:max_ndx[0], j:max_ndx[1]]) U[i // blk_size[0], j // blk_size[1]] = u S[i // blk_size[0], j // blk_size[1]] = s V[i // blk_size[0], j // blk_size[1]] = v return U, S, V def block_ISVD(U, S, V, blk_size): row_U, col_U = U.shape U = U.flatten() S = S.flatten() V = V.flatten() im_tmp = np.empty_like(U) im_wm = np.empty((row_U*blk_size[0],col_U*blk_size[1])) for i in range(U.size): tmp = np.matmul(U[i],np.diag(S[i])) im_tmp[i] = np.matmul(tmp,V[i]) im_tmp = im_tmp.reshape(row_U, col_U) for i in range(row_U): for j in range(col_U): im_wm[i*blk_size[0]:(i+1)*blk_size[0], j*blk_size[1]:(j+1)*blk_size[1]] = im_tmp[i,j] return im_wm 这段代码什么意思
这段代码实现了基于块的 SVD(奇异值分解)和逆 SVD 操作。具体地,它分为两个函数:
1. block_SVD(M, blk_size):输入一个矩阵 M 和一个块大小 blk_size,输出 M 的基于块的 SVD 分解结果,包括 U、S、V 三个矩阵。其中 U 和 V 是正交矩阵,S 是对角线上的奇异值向量。
2. block_ISVD(U, S, V, blk_size):输入基于块的 SVD 分解结果 U、S、V 和块大小 blk_size,输出原始矩阵 M。这个函数实际上是对 U、S、V 分别进行逆 SVD 操作,然后将结果拼接成原始矩阵 M。
整个过程中,矩阵 M 被分成多个大小为 blk_size 的块,每个块分别进行 SVD 分解和逆 SVD 操作,最终得到整个矩阵的 SVD 分解结果和原始矩阵。这种基于块的 SVD 分解和逆 SVD 操作可以在处理大型矩阵时提高计算效率,并且适用于分布式计算环境。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)