赞
踩
本工程主要根据:http://python.jobbole.com/80860/ 移植的。具体原理不再赘述,直接上代码!
首先是 类:
- #ifndef IMAGE_SEARCH_ENGINE_H
- #define IMAGE_SEARCH_ENGINE_H
-
- #include "StdAfx.h"
- #include <vector>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/core/core.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- #include <string>
- #include <windows.h>
- #include <assert.h>
- #include <io.h>
- #include <sstream>
- #include <fstream>
- #include <string>
- #include <map>
- #include <algorithm>
- #include <math.h>
-
- using namespace cv;
- using namespace std;
- /*
- const string index_xml="D:\\xml\\index.yml";
- FileStorage fs(index_xml, FileStorage::WRITE);
- */
-
- const char DataBase[] = ".//PICTURE.txt";
- ofstream fout;
- ifstream fin;
-
- int cmp(const pair<string,double> &x,const pair<string,double> &y);
-
- typedef bool(*FindProc)(const char*chFinleName);
-
- class Image_Search_Engine{
- public:
- void setNumber(int number); // 设定最多需要显示的图片;
- void setSearchImage(Mat &img); //设定源图片;
- bool newIndex(const char* Directory); // 建立索引,true 表示建立索引成功
- bool updateIndex(const char* Directory); // 更新索引,true 表示更新索引成功
- vector<string> getImage(); // 得到相似图像的 路径和名称
-
- private:
- Mat SearchImage; //源图片;
- int Max_Display; // 最多需要显示的图片;
-
- vector<vector<Mat>> feature(Mat &src); // 一幅图的3D HSV 直方图,将5个区域放在了一个容器内
- vector<Mat> histogram(Mat &hsv_base,Mat &Mask); // 图片一部分的 HSV 直方图
- bool FindFolder(const char* chFolderPath, const char* chFilter, bool bFindFolder, FindProc pFindProc); // 搜索图片
- friend bool ProcFile(const char* chFileName); // 处理单幅图片
- bool setIndex(const char* Directory); // 建立索引,true 表示建立索引成功
-
- void inMat(Mat &M,stringstream &S); //将文本读取到的数据放入Mat
- double HistCompare(vector<vector<Mat>> &H,vector<vector<Mat>> &S); //比较两个图片的直方图的卡方相似度
-
- void sortMapbyValue(map<string,double> &t_map,vector< pair<string,double> > &t_vec);// 升序排列
- // 说明是升序还是降序
- double myCompare(Mat &serach,Mat &orignal);
- //static bool ProcFile(const char* chFileName);
- };
- // public ///
-
- // 得到相似图像的 路径和名称
- bool Image_Search_Engine::newIndex(const char* Directory){ // 建立索引,true 表示建立索引成功
-
- fout.open(DataBase); //打开文件
- setIndex(Directory);
- return true;
- }
- bool Image_Search_Engine::updateIndex(const char* Directory){ // 更新索引,true 表示更新索引成功
- fout.open(DataBase,ios::app); //打开文件,累加
- setIndex(Directory);
- return true;
-
- }
-
- vector<string> Image_Search_Engine::getImage(){
- Mat src=SearchImage.clone(); // do not affect orignal image
-
- vector<string> Imagefile; // 搜寻结果的图片
-
-
- ifstream fin(DataBase,ios::in);
- char line[1024]={0};
-
- vector<vector<Mat>> orignal_feature=feature(src); // 得到源图像的特征向量
-
- map<string,double> Image_CH_result; //图片和卡方值的map
-
- vector< pair<string,double> > v_result;
-
-
- while(!fin.eof()){
- // 遍历数据库,与源图像作对比
- // 读取文件名
- string filename;
-
- fin.getline(line, sizeof(line));
- std::stringstream word(line)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。