Joshua Moerman
10 years ago
5 changed files with 99 additions and 99 deletions
@ -0,0 +1,51 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <vector> |
||||
|
#include <string> |
||||
|
#include <algorithm> |
||||
|
#include <random> |
||||
|
|
||||
|
template <typename T> |
||||
|
struct Mozaic { |
||||
|
Mozaic(int h_tiles_, int v_tiles_) |
||||
|
: h_tiles(h_tiles_) |
||||
|
, v_tiles(v_tiles_) |
||||
|
, tiles(v_tiles, std::vector<T>(h_tiles)) |
||||
|
{} |
||||
|
|
||||
|
decltype(auto) operator[](size_t c) { return tiles[c]; } |
||||
|
decltype(auto) operator[](size_t c) const { return tiles[c]; } |
||||
|
|
||||
|
// aka width & height
|
||||
|
int h_tiles = 0, v_tiles = 0; |
||||
|
|
||||
|
// tiles[row][column]
|
||||
|
std::vector<std::vector<T>> tiles; |
||||
|
}; |
||||
|
|
||||
|
template <typename F, typename T> |
||||
|
auto mozaic_fmap(Mozaic<T> const & mozaic, F&& f){ |
||||
|
using return_type = decltype(f(mozaic[0][0])); |
||||
|
Mozaic<return_type> ret(mozaic.h_tiles, mozaic.v_tiles); |
||||
|
|
||||
|
for(auto r = 0; r < mozaic.v_tiles; ++r){ |
||||
|
for(auto c = 0; c < mozaic.h_tiles; ++c){ |
||||
|
ret[r][c] = f(mozaic[r][c]); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return ret; |
||||
|
} |
||||
|
|
||||
|
//! Currently takes one of the (slack+1) best fitting images
|
||||
|
template <typename Distance> |
||||
|
auto optimize(Mozaic<std::vector<Distance>> distances, int slack){ |
||||
|
std::random_device rd; |
||||
|
std::uniform_int_distribution<int> dist(0, slack); |
||||
|
auto rand = [&]{ return dist(rd); }; |
||||
|
|
||||
|
return mozaic_fmap(distances, [&](auto d){ |
||||
|
std::nth_element(begin(d), begin(d) + slack, end(d)); |
||||
|
return (begin(d) + rand())->second; |
||||
|
}); |
||||
|
} |
@ -1,47 +0,0 @@ |
|||||
#include <image_io.hpp> |
|
||||
#include <wavelet.hpp> |
|
||||
#include <image_database.hpp> |
|
||||
#include <fingerprints.hpp> |
|
||||
|
|
||||
#include <boost/filesystem.hpp> |
|
||||
|
|
||||
extern "C" { |
|
||||
#include <libavformat/avformat.h> |
|
||||
} |
|
||||
|
|
||||
#include <iostream> |
|
||||
#include <algorithm> |
|
||||
#include <cmath> |
|
||||
|
|
||||
using namespace std; |
|
||||
namespace fs = boost::filesystem; |
|
||||
|
|
||||
int main(){ |
|
||||
av_register_all(); |
|
||||
|
|
||||
string const database_directory = "database"; |
|
||||
string const filename = "needle.jpg"; |
|
||||
|
|
||||
image_database<rgb_wavelet_coefficients> db; |
|
||||
|
|
||||
fs::path const directory(database_directory); |
|
||||
fs::directory_iterator eod; |
|
||||
for(fs::directory_iterator it(directory); it != eod; ++it){ |
|
||||
auto const path = it->path(); |
|
||||
auto const ext = path.extension(); |
|
||||
if(ext != ".png" && ext != ".jpg") continue; |
|
||||
|
|
||||
cout << colors::green("adding: ") << path.string() << endl; |
|
||||
db.add(path.string()); |
|
||||
} |
|
||||
|
|
||||
while(true){ |
|
||||
cout << "****************" << endl; |
|
||||
cout << colors::green("database: ") << db.size() << endl; |
|
||||
cout << colors::green("press enter to search match for: ") << filename << endl; |
|
||||
cin.ignore(); |
|
||||
auto const index = db.nearest_image(open_image(filename)); |
|
||||
cout << colors::green("match: ") << db.filename(index) << endl; |
|
||||
} |
|
||||
} |
|
||||
|
|
Loading…
Reference in new issue