Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include<iostream>
- #include<stdio.h>
- #include<opencv2/imgproc.hpp>
- #include<opencv2/highgui.hpp>
- using namespace cv;
- using namespace std;
- /// Global variables
- Mat inverseBinarize(Mat colored);
- vector<Point> getContours(Mat source) {
- vector<vector<Point>> contours;
- vector<Vec4i> hierarchy;
- findContours(source, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
- return contours[0];
- }
- double distance(vector<Point> a, vector<Point> b) {
- double distance = 0;
- int minLength = a.size() < b.size() ? a.size() : b.size();
- for (int i = 0;i < minLength;i++) {
- distance += sqrt(pow(a[i].x - b[i].x,2) + pow(a[i].y - b[i].y,2));
- }
- return distance;
- }
- double CEdistance(vector<double> a, vector<double> b) {
- double distance = 0;
- int minLength = a.size() < b.size() ? a.size() : b.size();
- for (int i = 0;i < minLength;i++) {
- distance += abs(a[i] - b[i]);
- }
- return distance;
- }
- vector<double> getDescriptors(vector<Point> contour) {
- int m = contour.size();
- int n = round(m / 2);
- double *ax = new double[m],
- *bx = new double[m],
- *ay = new double[m],
- *by = new double[m];
- vector<double> CE;
- double t = 2 * 3.14159 / m;
- for (int k = 0;k < m;k++) {
- ax[k] = bx[k] = ay[k] = by[k] = 0;
- for (int i = 0;i < m;i++) {
- ax[k] = ax[k] + contour[i].x * cos(k*t*i);
- bx[k] = bx[k] + contour[i].x * sin(k*t*i);
- ay[k] = ay[k] + contour[i].y * cos(k*t*i);
- by[k] = by[k] + contour[i].y * sin(k*t*i);
- }
- ax[k] = ax[k] * 2 / m;
- bx[k] = bx[k] * 2 / m;
- ay[k] = ay[k] * 2 / m;
- by[k] = by[k] * 2 / m;
- }
- for (int k = 0;k < n;k++) {
- CE.push_back(sqrt((pow(ax[k], 2) + pow(ay[k], 2)) / (pow(ax[0], 2) + pow(ay[0], 2)))
- + sqrt((pow(bx[k], 2) + pow(by[k], 2)) / (pow(bx[0], 2) + pow(by[0], 2))));
- }
- return CE;
- }
- int main() {
- //imshow("Source", inv_src);
- String path("db_images/*.jpg"); //select only jpg
- String testpath("test_images/*.jpg"); //select only jpg
- vector<String> fn,fn2;
- vector<Mat> dbImages;
- vector<vector<Point>> allContours;
- vector<double> distances;
- glob(path, fn, true); // recurse
- glob(testpath, fn2, true); // recurse
- for (size_t k = 0; k<fn.size(); ++k)
- {
- Mat im = imread(fn[k]);
- if (im.empty()) continue; //only proceed if successful
- Mat inv = inverseBinarize(im);
- dbImages.push_back(inv);
- allContours.push_back(getContours(inv));
- }
- for (size_t k = 0; k < fn2.size(); ++k) {
- Mat im = imread(fn2[k]);
- if (im.empty()) continue; //only proceed if successful
- Mat inv = inverseBinarize(im);
- vector<Point> imgContour = getContours(inv);
- for (size_t j = 0; j < fn.size(); ++j) {
- distances.push_back(CEdistance(getDescriptors(allContours[j]), getDescriptors( imgContour)));
- }
- int minElementIndex = std::min_element(distances.begin(), distances.end()) - distances.begin();
- String matchPath = fn[minElementIndex];
- imshow("Original", imread(fn2[k]));
- imshow("Match", imread(matchPath));
- distances.clear();
- waitKey(0);
- }
- Mat dst;
- waitKey(0);
- return 0;
- }
- Mat inverseBinarize(Mat colored) {
- Mat src_gray,dst;
- /// Convert the image to grayscale
- cvtColor(colored, src_gray, CV_BGR2GRAY);
- threshold(src_gray, dst, 150, 255, THRESH_BINARY_INV);
- return dst;
- }
- //images are 24bit per px -> grayscale -> treshhold
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement