Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * EMD.cpp
- *
- * Created on: Sep 16, 2016
- * Author: Oscar Antonio Hernández Blasí
- */
- #include <vector>
- #include "opencv2/imgcodecs.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include <iostream>
- #include <stdio.h>
- using namespace cv;
- using namespace std;
- // input argument is path to the first image in the directory
- void SortImages(vector<float> &num, vector<String> &name);
- float CompareEMD(Mat base, Mat test1);
- int main(int argc, char** argv)
- {
- Mat Match;
- Mat imagenBase = imread("../DataBase_1/IMG_0398.tif", 1);
- vector < cv::String> nombres; //Coloco strings en el vector
- glob("../DataBase_1/*.tif", nombres, false);
- vector<Mat>imagenes; //Coloco matrices en cada posicion del vector
- size_t cuentaN = nombres.size(); //Almacena el maximo teorico de un objeto de cualquier tipo, incluyendo arreglos
- for (size_t i = 0; i<cuentaN; i++)
- {
- imagenes.push_back(imread(nombres[i])); //ingresa los nombres de las imagenes al vector imagenes
- }
- vector<float> scores; //Coloca doubles en el vector
- size_t cuentaI = imagenes.size();
- for (size_t i = 0; i < cuentaI; i++)
- {
- scores.push_back(CompareEMD(imagenBase, imagenes[i])); //Ingresa las imagenes a la funcion definida como CompareEMD
- //printf("%s: %f \n", nombres[i].c_str(), scores[i]);
- }
- // sort scores using bubble sorting
- SortImages(scores, nombres);
- size_t cuentaS = imagenes.size();
- for (std::size_t i = 0; i < cuentaS; i++)
- {
- printf("Similaridad %s: %f \n", nombres[cuentaS-1-i].c_str(), scores[cuentaS-1-i]);
- if (scores[i] == 0.00184)
- {
- Match = imread(nombres[i]);
- }
- }
- namedWindow("Base", WINDOW_AUTOSIZE);
- imshow("Base", imagenBase);
- namedWindow("Prueba1", WINDOW_AUTOSIZE);
- imshow("Prueba1", Match);
- waitKey();
- return 0;
- }
- void SortImages(vector<float> &num, vector<String> &name)
- {
- int i, j, flag = 1; // set flag to 1 to start first pass
- double temp;
- String tempName;
- int numLength = num.size();
- for (i = 1; (i <= numLength) && flag; i++)
- {
- flag = 0;
- for (j = 0; j < (numLength - 1); j++)
- {
- if (num[j + 1] > num[j])
- {
- temp = num[j]; // swap elements
- num[j] = num[j + 1];
- num[j + 1] = temp;
- tempName = name[j]; // swap elements
- name[j] = name[j + 1];
- name[j + 1] = tempName;
- flag = 1; // indicates that a swap occurred.
- }
- }
- }
- return;
- }
- float CompareEMD(Mat base, Mat test1) {
- //read 2 images for histogram comparing
- Mat imgA, imgB;
- imgA = base;
- imgB = test1;
- //variables preparing
- int hbins = 30, sbins = 32;
- int channels[] = { 0, 1 };
- int histSize[] = { hbins, sbins };
- float hranges[] = { 0, 180 };
- float sranges[] = { 0, 255 };
- const float* ranges[] = { hranges, sranges };
- Mat patch_HSV;
- MatND HistA, HistB;
- //cal histogram & normalization
- cvtColor(imgA, patch_HSV, CV_BGR2HSV);
- calcHist(&patch_HSV, 1, channels, Mat(), // do not use mask
- HistA, 2, histSize, ranges,
- true, // the histogram is uniform
- false);
- normalize(HistA, HistA, 0, 1, CV_MINMAX);
- cvtColor(imgB, patch_HSV, CV_BGR2HSV);
- calcHist(&patch_HSV, 1, channels, Mat(),// do not use mask
- HistB, 2, histSize, ranges,
- true, // the histogram is uniform
- false);
- normalize(HistB, HistB, 0, 1, CV_MINMAX);
- //compare histograms
- int numrows = hbins * sbins;
- //make signature
- Mat sig1(numrows, 3, CV_32FC1);
- Mat sig2(numrows, 3, CV_32FC1);
- //fill value into signature
- for (int h = 0; h< hbins; h++)
- {
- for (int s = 0; s< sbins; ++s)
- {
- float binval = HistA.at< float>(h, s);
- sig1.at< float>(h*sbins + s, 0) = binval;
- sig1.at< float>(h*sbins + s, 1) = h;
- sig1.at< float>(h*sbins + s, 2) = s;
- binval = HistB.at< float>(h, s);
- sig2.at< float>(h*sbins + s, 0) = binval;
- sig2.at< float>(h*sbins + s, 1) = h;
- sig2.at< float>(h*sbins + s, 2) = s;
- }
- }
- //compare similarity of 2images using emd
- return cv::EMD(sig1, sig2, CV_DIST_L2); //emd 0 is best matching.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement