SHARE
TWEET

Untitled

a guest Apr 22nd, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. #include<iostream>
  3. #include<stdio.h>
  4. #include<opencv2/imgproc.hpp>
  5. #include<opencv2/highgui.hpp>
  6. using namespace cv;
  7. using namespace std;
  8. /// Global variables
  9. Mat inverseBinarize(Mat colored);
  10. vector<Point> getContours(Mat source) {
  11.     vector<vector<Point>> contours;
  12.     vector<Vec4i> hierarchy;
  13.     findContours(source, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
  14.     return contours[0];
  15. }
  16. double distance(vector<Point> a, vector<Point> b) {
  17.     double distance = 0;
  18.     int minLength = a.size() < b.size() ? a.size() : b.size();
  19.     for (int i = 0;i < minLength;i++) {
  20.         distance += sqrt(pow(a[i].x - b[i].x,2) + pow(a[i].y - b[i].y,2));
  21.     }
  22.     return distance;
  23. }
  24. double CEdistance(vector<double> a, vector<double> b) {
  25.     double distance = 0;
  26.     int minLength = a.size() < b.size() ? a.size() : b.size();
  27.     for (int i = 0;i < minLength;i++) {
  28.         distance += abs(a[i] - b[i]);
  29.     }
  30.     return distance;
  31. }
  32. vector<double> getDescriptors(vector<Point> contour) {
  33.     int m = contour.size();
  34.     int n = round(m / 2);
  35.     double *ax = new double[m],
  36.         *bx = new double[m],
  37.         *ay = new double[m],
  38.         *by = new double[m];
  39.     vector<double> CE;
  40.     double t = 2 * 3.14159 / m;
  41.     for (int k = 0;k < m;k++) {
  42.         ax[k] = bx[k] = ay[k] = by[k] = 0;
  43.         for (int i = 0;i < m;i++) {
  44.             ax[k] = ax[k] + contour[i].x * cos(k*t*i);
  45.             bx[k] = bx[k] + contour[i].x * sin(k*t*i);
  46.             ay[k] = ay[k] + contour[i].y * cos(k*t*i);
  47.             by[k] = by[k] + contour[i].y * sin(k*t*i);
  48.         }
  49.         ax[k] = ax[k] * 2 / m;
  50.         bx[k] = bx[k] * 2 / m;
  51.         ay[k] = ay[k] * 2 / m;
  52.         by[k] = by[k] * 2 / m;
  53.     }
  54.     for (int k = 0;k < n;k++) {
  55.         CE.push_back(sqrt((pow(ax[k], 2) + pow(ay[k], 2)) / (pow(ax[0], 2) + pow(ay[0], 2)))
  56.             + sqrt((pow(bx[k], 2) + pow(by[k], 2)) / (pow(bx[0], 2) + pow(by[0], 2))));
  57.     }
  58.     return CE;
  59. }
  60.  
  61. int main() {
  62.     //imshow("Source", inv_src);
  63.  
  64.     String path("db_images/*.jpg"); //select only jpg
  65.     String testpath("test_images/*.jpg"); //select only jpg
  66.     vector<String> fn,fn2;
  67.     vector<Mat> dbImages;
  68.     vector<vector<Point>> allContours;
  69.     vector<double> distances;
  70.     glob(path, fn, true); // recurse
  71.     glob(testpath, fn2, true); // recurse
  72.     for (size_t k = 0; k<fn.size(); ++k)
  73.     {
  74.         Mat im = imread(fn[k]);
  75.         if (im.empty()) continue; //only proceed if successful
  76.         Mat inv = inverseBinarize(im);
  77.         dbImages.push_back(inv);
  78.    
  79.         allContours.push_back(getContours(inv));
  80.     }
  81.  
  82.     for (size_t k = 0; k < fn2.size(); ++k) {
  83.         Mat im = imread(fn2[k]);
  84.         if (im.empty()) continue; //only proceed if successful
  85.         Mat inv = inverseBinarize(im);
  86.         vector<Point> imgContour = getContours(inv);
  87.         for (size_t j = 0; j < fn.size(); ++j) {
  88.             distances.push_back(CEdistance(getDescriptors(allContours[j]), getDescriptors( imgContour)));
  89.         }
  90.         int minElementIndex = std::min_element(distances.begin(), distances.end()) - distances.begin();
  91.         String matchPath = fn[minElementIndex];
  92.         imshow("Original", imread(fn2[k]));
  93.         imshow("Match", imread(matchPath));
  94.         distances.clear();
  95.  
  96.         waitKey(0);
  97.     }
  98.  
  99.    
  100.  
  101.     Mat dst;
  102.  
  103.     waitKey(0);
  104.  
  105.     return 0;
  106.  
  107. }
  108.  
  109. Mat inverseBinarize(Mat colored) {
  110.     Mat src_gray,dst;
  111.     /// Convert the image to grayscale
  112.     cvtColor(colored, src_gray, CV_BGR2GRAY);
  113.  
  114.     threshold(src_gray, dst, 150, 255, THRESH_BINARY_INV);
  115.  
  116.     return dst;
  117. }
  118.  
  119. //images are 24bit per px -> grayscale -> treshhold
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top