Advertisement
OscarAHB

Untitled

Sep 18th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. /*
  2. *        EMD.cpp
  3. *
  4. *  Created on: Sep 16, 2016
  5. *      Author: Oscar Antonio Hernández Blasí
  6. */
  7.  
  8. #include <vector>
  9. #include "opencv2/imgcodecs.hpp"
  10. #include "opencv2/highgui/highgui.hpp"
  11. #include "opencv2/imgproc/imgproc.hpp"
  12. #include <iostream>
  13. #include <stdio.h>
  14.  
  15. using namespace cv;
  16. using namespace std;
  17. // input argument is path to the first image in the directory
  18. void SortImages(vector<float> &num, vector<String> &name); 
  19. float CompareEMD(Mat base, Mat test1);
  20.  
  21. int main(int argc, char** argv)
  22. {
  23.     Mat Match;
  24.     Mat imagenBase = imread("../DataBase_1/IMG_0398.tif", 1);
  25.     vector < cv::String> nombres; //Coloco strings en el vector
  26.     glob("../DataBase_1/*.tif", nombres, false);
  27.     vector<Mat>imagenes; //Coloco matrices en cada posicion del vector
  28.     size_t cuentaN = nombres.size(); //Almacena el maximo teorico de un objeto de cualquier tipo, incluyendo arreglos
  29.     for (size_t i = 0; i<cuentaN; i++)
  30.     {
  31.         imagenes.push_back(imread(nombres[i])); //ingresa los nombres de las imagenes al vector imagenes
  32.     }
  33.     vector<float> scores; //Coloca doubles en el vector
  34.     size_t cuentaI = imagenes.size();
  35.     for (size_t i = 0; i < cuentaI; i++)
  36.     {
  37.         scores.push_back(CompareEMD(imagenBase, imagenes[i])); //Ingresa las imagenes a la funcion definida como CompareEMD
  38.         //printf("%s: %f \n", nombres[i].c_str(), scores[i]);
  39.     }
  40.     // sort scores using bubble sorting
  41.     SortImages(scores, nombres);   
  42.     size_t cuentaS = imagenes.size();
  43.     for (std::size_t i = 0; i < cuentaS; i++)
  44.     {
  45.         printf("Similaridad %s: %f \n", nombres[cuentaS-1-i].c_str(), scores[cuentaS-1-i]);
  46.         if (scores[i] == 0.00184)
  47.         {
  48.             Match = imread(nombres[i]);
  49.         }
  50.     }
  51.     namedWindow("Base", WINDOW_AUTOSIZE);
  52.     imshow("Base", imagenBase);
  53.     namedWindow("Prueba1", WINDOW_AUTOSIZE);
  54.     imshow("Prueba1", Match);
  55.     waitKey();
  56.     return 0;
  57. }
  58. void SortImages(vector<float> &num, vector<String> &name)
  59. {
  60.     int i, j, flag = 1;                 // set flag to 1 to start first pass
  61.     double temp;
  62.     String tempName;
  63.     int numLength = num.size();
  64.     for (i = 1; (i <= numLength) && flag; i++)
  65.     {
  66.         flag = 0;
  67.         for (j = 0; j < (numLength - 1); j++)
  68.         {
  69.             if (num[j + 1] > num[j])
  70.             {
  71.                 temp = num[j];          // swap elements
  72.                 num[j] = num[j + 1];
  73.                 num[j + 1] = temp;
  74.  
  75.                 tempName = name[j];     // swap elements
  76.                 name[j] = name[j + 1];
  77.                 name[j + 1] = tempName;
  78.  
  79.                 flag = 1;               // indicates that a swap occurred.
  80.             }
  81.         }
  82.     }
  83.     return;
  84. }
  85. float CompareEMD(Mat base, Mat test1) {
  86.     //read 2 images for histogram comparing
  87.     Mat imgA, imgB;
  88.     imgA = base;
  89.     imgB = test1;
  90.  
  91.     //variables preparing
  92.  
  93.     int hbins = 30, sbins = 32;
  94.     int channels[] = { 0,  1 };
  95.     int histSize[] = { hbins, sbins };
  96.     float hranges[] = { 0, 180 };
  97.     float sranges[] = { 0, 255 };
  98.     const float* ranges[] = { hranges, sranges };
  99.  
  100.     Mat patch_HSV;
  101.     MatND HistA, HistB;
  102.  
  103.     //cal histogram & normalization
  104.     cvtColor(imgA, patch_HSV, CV_BGR2HSV);
  105.     calcHist(&patch_HSV, 1, channels, Mat(), // do not use mask
  106.         HistA, 2, histSize, ranges,
  107.         true, // the histogram is uniform
  108.         false);
  109.     normalize(HistA, HistA, 0, 1, CV_MINMAX);
  110.  
  111.  
  112.     cvtColor(imgB, patch_HSV, CV_BGR2HSV);
  113.     calcHist(&patch_HSV, 1, channels, Mat(),// do not use mask
  114.         HistB, 2, histSize, ranges,
  115.         true, // the histogram is uniform
  116.         false);
  117.     normalize(HistB, HistB, 0, 1, CV_MINMAX);
  118.  
  119.     //compare histograms
  120.     int numrows = hbins * sbins;
  121.  
  122.     //make signature
  123.     Mat sig1(numrows, 3, CV_32FC1);
  124.     Mat sig2(numrows, 3, CV_32FC1);
  125.  
  126.     //fill value into signature
  127.     for (int h = 0; h< hbins; h++)
  128.     {
  129.         for (int s = 0; s< sbins; ++s)
  130.         {
  131.             float binval = HistA.at< float>(h, s);
  132.             sig1.at< float>(h*sbins + s, 0) = binval;
  133.             sig1.at< float>(h*sbins + s, 1) = h;
  134.             sig1.at< float>(h*sbins + s, 2) = s;
  135.  
  136.             binval = HistB.at< float>(h, s);
  137.             sig2.at< float>(h*sbins + s, 0) = binval;
  138.             sig2.at< float>(h*sbins + s, 1) = h;
  139.             sig2.at< float>(h*sbins + s, 2) = s;
  140.         }
  141.     }
  142.  
  143.     //compare similarity of 2images using emd
  144.     return cv::EMD(sig1, sig2, CV_DIST_L2); //emd 0 is best matching.
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement