I compared all the images in pairs - it turns out a matrix of numbers (for convenience, compressed to 0-254) N x N, how to arrange now so that the similar ones are near
int r[][] = { { 255, 100, 93 }, { 100, 255, 113 }, { 93, 113, 255 }}; in this example, the order of = 1,0,2 seems to be correct. n-number of images will be less than 100,000, the numbers in the array are how different are the images, i.e. 1 line 0/0 (such a special case = 255), 0 and 1, 0 and 2 ..
Option 1, the search function looks for the most similar insertion point; working is not bad at all, better than complete bust. In the long run, the result was generally interesting, like preview or cutting. It would be better, of course, if it was continuous with 1 piece, and so the resulting scenes should be connected at the borders (a bug or comparison is not exact)
of.push_back(0); for (int i = 1; i < n; i++) { auto find_bm = [&](int j) { std::pair<int, int> m = { 0, 255 }; for (int i = 0; i < of.size(); i++) { int _1 = of[i], x = r[j][_1], _2, y = x; if (i < of.size() - 1) _2 = of[i + 1], y = r[j][_2]; if (m.second > x + y) m = { i, x + y }; } return of.begin() + m.first + 1; }; auto it = find_bm(i); of.emplace(it, i); } matrix creation, the code is too integrated, I hope it is clear like this:
srcc = avs_vfbuf_t(new avs_vfbuf(_srcc, env)); cv::Ptr<cv::img_hash::ImgHashBase> func = cv::img_hash::BlockMeanHash::create(); std::vector<cv::Mat> img_hash(n); for (int i = 0; i < n; i++) { avs_vf_t srcf = srcc->get(i); s_vf_plane srcp = srcf->plane(); cv::Mat img(srcp.h, srcp.w, CV_8U, srcp.p(), srcp.pitch); func->compute(img, img_hash[i]); } for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) r[i][j] = r[j][i] = std::min(int(func->compare(img_hash[i], img_hash[j])), 254); } this option does not work with more than ~ 15 size, and I’m not sure what is right:
int of[] = { 0, 1, 2 }; std::pair<int, std::vector<int>> bm = { INT32_MAX, of }; do { int _ = 0; for (int i = 1; i < n; i++) _ += r[of[i - 1]][of[i]]; if (_ < bm.first) bm = { _, of }; } while (std::next_permutation(of.begin(), of.end())); of = bm.second;