当前位置:   article > 正文

计算机视觉(五)--- 图像检索_面向数据的方法来捕捉图像和文本之间的物-词共现性,即图像中的物体在文本中具有相

面向数据的方法来捕捉图像和文本之间的物-词共现性,即图像中的物体在文本中具有相

原理

1.1 Bag-of-words原理简介


Bag Of Words(词袋)模型,是现在一种用于图像检索的一种方法。它最早用于对于文章内容的检索,原理是将文本看作是单词的集合,不考虑文档内的词的顺序关系和语法等。通过建立词典,对每个单词出现次数进行统计,以便得到文本内容的分类。


1.2 Bag-of-features原理简介


计算机视觉的专家将Bag-of-words方法应用于图像的检索中就有了Bag-of-features。
和Bag-of-words原理相似,若将文档对应一幅图像的话,那么文档内的词就是一个图像块的特征向量。一篇文档有若干个词构成,同样的,一幅图像由若干个图像块构成,而特征向量是图像块的一种表达方式。我们求得N幅图像中的若干个图像块的特征向量,然后用k-means算法把它们聚成k类,相当于我们的词袋里就有k个词,然后来了一幅图像,看它包含哪些词,若包含单词A,就把单词A的频数加1。

1.3 Bag-of-features算法


特征提取
学习 “视觉词典(visual vocabulary)”
针对输入图片对应的特征集,根据视觉词典进行量化
把输入图像,根据TF-IDF转化成视觉单词(visual words)的频率直方图
构造特征到图像的倒排表,通过倒排表快速索引相关图像
根据索引结果进行直方图匹配

TF-IDF理解

 

代码实践

 数据集准备

特征提取,生成特征词典

  1. import pickle
  2. from pcv.imagesearch import vocabulary
  3. from pcv.tools.imtools import get_imlist
  4. from pcv.localdescriptors import sift
  5. #获取图像列表
  6. imlist = get_imlist('WYS/')
  7. nbr_images = len(imlist)
  8. #获取特征列表
  9. featlist = [imlist[i][:-3]+'sift' for i in range(nbr_images)]
  10. #提取文件夹下图像的sift特征
  11. for i in range(nbr_images):
  12. sift.process_image(imlist[i], featlist[i])
  13. #生成词汇
  14. voc = vocabulary.Vocabulary('ukbenchtest')
  15. voc.train(featlist, 1000, 10)
  16. #保存词汇
  17. with open('WYS/vocabulary.pkl', 'wb') as f:
  18. pickle.dump(voc, f)
  19. print ('vocabulary is:', voc.name, voc.nbr_words)

对输入特征集量化

  1. import pickle
  2. from pcv.imagesearch import imagesearch
  3. from pcv.localdescriptors import sift
  4. import sqlite3
  5. from pcv.tools.imtools import get_imlist
  6. # 获取图像列表
  7. imlist = get_imlist(r'WYS')
  8. nbr_images = len(imlist)
  9. # 获取特征列表
  10. featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]
  11. # 载入词汇
  12. with open(r'WYS\vocabulary.pkl', 'rb') as f:
  13. voc = pickle.load(f)
  14. # 创建索引
  15. indx = imagesearch.Indexer('testImaAdd.db', voc)
  16. indx.create_tables()
  17. # 遍历所有的图像,并将它们的特征投影到词汇上
  18. for i in range(nbr_images)[:120]:
  19. locs, descr = sift.read_features_from_file(featlist[i])
  20. indx.add_to_index(imlist[i], descr)
  21. # 提交到数据库
  22. indx.db_commit()
  23. con = sqlite3.connect('testImaAdd.db')
  24. print(con.execute('select count (filename) from imlist').fetchone())
  25. print(con.execute('select * from imlist').fetchone())

建立图像索引进行图像检索

  1. import pickle
  2. from pcv.imagesearch import imagesearch
  3. from pcv.geometry import homography
  4. from pcv.tools.imtools import get_imlist
  5. from pcv.localdescriptors import sift
  6. import warnings
  7. warnings.filterwarnings("ignore")
  8. # load image list and vocabulary
  9. # 载入图像列表
  10. imlist = get_imlist(r'WYS')
  11. print(imlist)
  12. nbr_images = len(imlist)
  13. # 载入特征列表
  14. print(imlist[1][:-3] + 'sift')
  15. featlist = [imlist[i][:-3] + 'sift' for i in range(nbr_images)]
  16. # 载入词汇
  17. with open(r'WYS\vocabulary.pkl', 'rb') as f:
  18. voc = pickle.load(f, encoding='iso-8859-1')
  19. src = imagesearch.Searcher('testImaAdd.db', voc) # Searcher类读入图像的单词直方图执行查询
  20. # index of query image and number of results to return
  21. # 查询图像索引和查询返回的图像数
  22. q_ind = 3
  23. nbr_results = 10
  24. # regular query
  25. # 常规查询(按欧式距离对结果排序)
  26. res_reg = [w[1] for w in src.query(imlist[q_ind])[:nbr_results]] # 查询的结果
  27. print('top matches (regular):', res_reg)
  28. # load image features for query image
  29. # 载入查询图像特征进行匹配
  30. q_locs, q_descr = sift.read_features_from_file(featlist[q_ind])
  31. fp = homography.make_homog(q_locs[:, :2].T)
  32. # RANSAC model for homography fitting
  33. # 用单应性进行拟合建立RANSAC模型
  34. model = homography.RansacModel()
  35. rank = {}
  36. # load image features for result
  37. # 载入候选图像的特征
  38. for ndx in res_reg[1:]:
  39. locs, descr = sift.read_features_from_file(featlist[ndx]) # because 'ndx' is a rowid of the DB that starts at 1
  40. # get matches
  41. matches = sift.match(q_descr, descr)
  42. ind = matches.nonzero()[0]
  43. ind2 = matches[ind]
  44. tp = homography.make_homog(locs[:, :2].T)
  45. # compute homography, count inliers. if not enough matches return empty list
  46. # 计算单应性矩阵
  47. try:
  48. H, inliers = homography.H_from_ransac(fp[:, ind], tp[:, ind2], model, match_theshold=4)
  49. except:
  50. inliers = []
  51. # store inlier count
  52. rank[ndx] = len(inliers)
  53. # sort dictionary to get the most inliers first
  54. # 对字典进行排序,可以得到重排之后的查询结果
  55. sorted_rank = sorted(rank.items(), key=lambda t: t[1], reverse=True)
  56. res_geom = [res_reg[0]] + [s[0] for s in sorted_rank]
  57. print('top matches (homography):', res_geom)
  58. # 显示查询结果
  59. imagesearch.plot_results(src, res_reg[:6]) # 常规查询
  60. imagesearch.plot_results(src, res_geom[:6]) # 重排后的结果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/435543
推荐阅读
相关标签
  

闽ICP备14008679号