当前位置:   article > 正文

基于C++实现的简单的图像搜索引擎_c++ python 以图搜图 图像识别

c++ python 以图搜图 图像识别

本工程主要根据:http://python.jobbole.com/80860/   移植的。具体原理不再赘述,直接上代码!


首先是 类:

  1. #ifndef IMAGE_SEARCH_ENGINE_H
  2. #define IMAGE_SEARCH_ENGINE_H
  3. #include "StdAfx.h"
  4. #include <vector>
  5. #include <opencv2/highgui/highgui.hpp>
  6. #include <opencv2/core/core.hpp>
  7. #include <opencv2/imgproc/imgproc.hpp>
  8. #include <string>
  9. #include <windows.h>
  10. #include <assert.h>
  11. #include <io.h>
  12. #include <sstream>
  13. #include <fstream>
  14. #include <string>
  15. #include <map>
  16. #include <algorithm>
  17. #include <math.h>
  18. using namespace cv;
  19. using namespace std;
  20. /*
  21. const string index_xml="D:\\xml\\index.yml";
  22. FileStorage fs(index_xml, FileStorage::WRITE);
  23. */
  24. const char DataBase[] = ".//PICTURE.txt";
  25. ofstream fout;
  26. ifstream fin;
  27. int cmp(const pair<string,double> &x,const pair<string,double> &y);
  28. typedef bool(*FindProc)(const char*chFinleName);
  29. class Image_Search_Engine{
  30. public:
  31. void setNumber(int number); // 设定最多需要显示的图片;
  32. void setSearchImage(Mat &img); //设定源图片;
  33. bool newIndex(const char* Directory); // 建立索引,true 表示建立索引成功
  34. bool updateIndex(const char* Directory); // 更新索引,true 表示更新索引成功
  35. vector<string> getImage(); // 得到相似图像的 路径和名称
  36. private:
  37. Mat SearchImage; //源图片;
  38. int Max_Display; // 最多需要显示的图片;
  39. vector<vector<Mat>> feature(Mat &src); // 一幅图的3D HSV 直方图,将5个区域放在了一个容器内
  40. vector<Mat> histogram(Mat &hsv_base,Mat &Mask); // 图片一部分的 HSV 直方图
  41. bool FindFolder(const char* chFolderPath, const char* chFilter, bool bFindFolder, FindProc pFindProc); // 搜索图片
  42. friend bool ProcFile(const char* chFileName); // 处理单幅图片
  43. bool setIndex(const char* Directory); // 建立索引,true 表示建立索引成功
  44. void inMat(Mat &M,stringstream &S); //将文本读取到的数据放入Mat
  45. double HistCompare(vector<vector<Mat>> &H,vector<vector<Mat>> &S); //比较两个图片的直方图的卡方相似度
  46. void sortMapbyValue(map<string,double> &t_map,vector< pair<string,double> > &t_vec);// 升序排列
  47. // 说明是升序还是降序
  48. double myCompare(Mat &serach,Mat &orignal);
  49. //static bool ProcFile(const char* chFileName);
  50. };
  51. // public ///
  52. // 得到相似图像的 路径和名称
  53. bool Image_Search_Engine::newIndex(const char* Directory){ // 建立索引,true 表示建立索引成功
  54. fout.open(DataBase); //打开文件
  55. setIndex(Directory);
  56. return true;
  57. }
  58. bool Image_Search_Engine::updateIndex(const char* Directory){ // 更新索引,true 表示更新索引成功
  59. fout.open(DataBase,ios::app); //打开文件,累加
  60. setIndex(Directory);
  61. return true;
  62. }
  63. vector<string> Image_Search_Engine::getImage(){
  64. Mat src=SearchImage.clone(); // do not affect orignal image
  65. vector<string> Imagefile; // 搜寻结果的图片
  66. ifstream fin(DataBase,ios::in);
  67. char line[1024]={0};
  68. vector<vector<Mat>> orignal_feature=feature(src); // 得到源图像的特征向量
  69. map<string,double> Image_CH_result; //图片和卡方值的map
  70. vector< pair<string,double> > v_result;
  71. while(!fin.eof()){
  72. // 遍历数据库,与源图像作对比
  73. // 读取文件名
  74. string filename;
  75. fin.getline(line, sizeof(line));
  76. std::stringstream word(line)
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
相关标签
  

闽ICP备14008679号