size_t get_min_diff(const image& img1, const image& img2) { if(img1.nc() < img2.nc()) return get_min_diff(img2, img1); size_t score = INFTY; for (int i = -H_TOL; i<= H_TOL; i+=STEP_SIZE) for(int j= -V_TOL; j<= V_TOL; j+= STEP_SIZE) { image copy; dlib::assign_image(copy, img1); dlib::rectangle r1 = dlib::get_rect(img1); dlib::rectangle r2 = dlib::get_rect(img2); dlib::rectangle crop1 = dlib::intersect(r1, dlib::translate_rect(r2, i, j)); dlib::rectangle crop2 = dlib::translate_rect(crop1, -i, -j); auto sub1 = dlib::sub_image(copy, crop1); auto sub2 = dlib::sub_image(img2, crop2); xor_copy(sub1, sub2); size_t res = cnt(copy); score = std::min(score, res); } return score; }
double distance_processor::calculate_one(const image& img1, const image& img2) { double dist = get_min_diff(img1, img2); int size = std::min(img1.nc(), img2.nc()); double s = std::max(dist/log2(size), 0.0); return pow(2, -s); }