Image retrieval: one-dimensional histogram + Euclidean distance +FLANN+KNN

Source: Internet
Author: User
Tags ranges

A text file with a file name of "folder" is generated in the F-drive.


First step: Batch extracts the one-dimensional color histogram of the image and saves it to the featurehists in the. xml

First parameter: The path to the image

Second parameter: the saved. xml

#include <iostream> #include <fstream> #include <string>using namespace std; #include <opencv2\ imgproc\imgproc.hpp> #include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp>using Namespace cv;//calculates one-dimensional histogram feature Mat hist1d (const mat& SRC); int main (int argc,char* argv[]) {    if (argc!=3) & nbsp;   {        cerr << "wrong Argument!" <<endl;   & nbsp;    return-1;    }    //definition file stream, only can read     ifstream Inputfile (argv[1],ios::in);    if (inputfile)     {        cerr << "File Open erro!" <<endl;        return-1;    }     //reads each row in the file stream and assigns a value to filename. Reads each image and displays     string fileName;    mat image;    mat featurehist; & nbsp  mat FEATUREHISTS;&NBSP;&NBsp  while (Getline (inputfile,filename))     {                image = Imread (filename,1);        //compute one-dimensional histogram features         featurehist = hist1d (image);        //One-dimensional histogram feature for each image by row         featurehists.push_back (featurehist);    }   / /Note Be sure to remember to close the file stream     inputfile.close ();    /* Fifth step. Save image features to an. xml file */    filestorage FS (argv[2],filestorage::write);    fs<< " Featurehists "<<featureHists;    fs.release ();        return 0;} Mat hist1d (const mat& src) {    mat hsv;    //color space conversion bgr2hsv     Cvtcolor (SRC,HSV,CV_BGR2HSV);    //divides h channel into 60 bin    int hbins = 60;     int histsize[] = {Hbins};    //h Value range 0-179    float hranges[]= {0,180};    const float* Ranges [] ={hranges};    mat hist1d,histrow,histrowdst;    //We are based on the first channel of the image. Calculates a one-dimensional histogram and outputs a hist1d of 32f    int channels [] ={0};    calchist (&hsv,1,channels,mat ( ), Hist1d,1,histsize,ranges,true,false);    //to store the histogram feature on one line     histrow= Hist1d.reshape (;  )  //histogram normalization     normalize (HISTROW,HISTROWDST,1,0,NORM_L1);     return HISTROWDST;}

After compiling, go to the command line

Then, a features. xml file appears on the F-disk. The one-dimensional histogram feature of the above image is stored in the inside.

—————————————————————————————————————————————————————————————————————————————
Step two: Extracting one-dimensional color histogram of color cards

 #include < Iostream> #include <string>using namespace std; #include <opencv2\core\core.hpp> #include <opencv2\ Highgui\highgui.hpp> #include <opencv2\imgproc\imgproc.hpp>using namespace cv;int main (int argc,char* argv[] {Mat src = imread (argv[1],1), if (! Src.data) {cout << "No Image" << endl;return-1;} Mat hsv;//Color space Conversion bgr2hsvcvtcolor (SRC,HSV,CV_BGR2HSV);//divide H channel into 60 binint hbins = 60;int histsize[] = {hbins};//h value range 0-179float hranges[]= {0,180};const float* ranges [] ={hranges}; Mat hist1d,histrow,histrowdst;//We calculate a one-dimensional histogram based on the first channel of the image, and the output HIST1D is 32Fint channels [] ={0};calchist (&hsv,1, Channels,mat (), hist1d,1,histsize,ranges,true,false);//The histogram feature is stored in one row for Histrow=hist1d.reshape (n/a); The histogram normalization normalize (HISTROW,HISTROWDST,1,0,NORM_L1); Filestorage FS (argv[2],filestorage::write);//Save HISTROWDST to. xml file FS << argv[3] << histrowdst; Fs.release (); return 0;} 


Individually extracting their one-dimensional color histogram



Then, there were four. xml files in the F-disk, each storing their color histogram information

_______________________________________________________________________________________________________________ _____________________

The third step: retrieving the color histogram of the image using color histogram of colour card


Image retrieval: one-dimensional histogram + Euclidean distance +FLANN+KNN

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.