Browse Source

Adds a mozaic datatype. Adds slack to mozaics. Gets rid of libav logging.

master
Joshua Moerman 10 years ago
parent
commit
e206b3f4a8
  1. 2
      lib/fingerprint_traits.hpp
  2. 37
      lib/image_database.hpp
  3. 51
      lib/mozaic.hpp
  4. 59
      src/main.cpp
  5. 47
      src/needle.cpp

2
lib/fingerprint_traits.hpp

@ -13,4 +13,6 @@ struct fingerprint_traits {
static pre_fingerprint pre_calculate(av::frame const & frame) { return fingerprint::pre_calculate(frame); } static pre_fingerprint pre_calculate(av::frame const & frame) { return fingerprint::pre_calculate(frame); }
static fingerprint calculate(av::frame const & frame) { return fingerprint::calculate(frame); } static fingerprint calculate(av::frame const & frame) { return fingerprint::calculate(frame); }
static auto distance(fingerprint const & x, pre_fingerprint const & y) { return x.distance_to(y); } static auto distance(fingerprint const & x, pre_fingerprint const & y) { return x.distance_to(y); }
using distance_type = decltype(distance(std::declval<fingerprint const &>(), std::declval<pre_fingerprint const &>()));
}; };

37
lib/image_database.hpp

@ -13,6 +13,10 @@ template <typename Fingerprint, typename Traits = fingerprint_traits<Fingerprint
struct image_database { struct image_database {
using index = size_t; using index = size_t;
auto filename(index i) const { return filenames[i]; }
auto fingerprint(index i) const { return fingerprints[i]; }
auto size() const { return fingerprints.size(); }
void add(std::string const & filename){ void add(std::string const & filename){
auto && image = open_image(filename); auto && image = open_image(filename);
auto && fingerprint = Traits::calculate(image); auto && fingerprint = Traits::calculate(image);
@ -21,33 +25,16 @@ struct image_database {
fingerprints.push_back(fingerprint); fingerprints.push_back(fingerprint);
} }
std::string filename(index i) const { //! returns a list of distances along with their index
return filenames.at(i); auto distances_for_image(av::frame const & image) const {
} std::vector<std::pair<typename Traits::distance_type, index>> ret;
ret.reserve(size());
Fingerprint fingerprint(index i) const {
return fingerprints.at(i);
}
auto size() const {
return fingerprints.size();
}
index nearest_image(av::frame const & image) const {
const auto && pre_fingerprint = Traits::pre_calculate(image);
index best_index = 0;
auto && best_distance = Traits::distance(fingerprints[0], pre_fingerprint);
for(index i = 1; i < fingerprints.size(); ++i){ auto const pre_fingerprint = Traits::pre_calculate(image);
const auto && distance = Traits::distance(fingerprints[i], pre_fingerprint); for(auto&& fingerprint : fingerprints){
if(distance < best_distance) { ret.emplace_back(Traits::distance(fingerprint, pre_fingerprint), ret.size());
best_distance = distance;
best_index = i;
} }
} return ret;
return best_index;
} }
private: private:

51
lib/mozaic.hpp

@ -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;
});
}

59
src/main.cpp

@ -2,6 +2,7 @@
#include <wavelet.hpp> #include <wavelet.hpp>
#include <image_database.hpp> #include <image_database.hpp>
#include <fingerprints.hpp> #include <fingerprints.hpp>
#include <mozaic.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <boost/archive/binary_oarchive.hpp> #include <boost/archive/binary_oarchive.hpp>
@ -29,7 +30,7 @@ namespace fs = boost::filesystem;
namespace ar = boost::archive; namespace ar = boost::archive;
using Database = image_database<downscale>; using Database = image_database<downscale>;
using Mozaic = map<pair<int, int>, string>; using Mozaiq = Mozaic<string>;
static Database read_database(string const & database_directory){ static Database read_database(string const & database_directory){
Database db; Database db;
@ -56,36 +57,36 @@ static Database read_database(string const & database_directory){
archive >> db; archive >> db;
} }
cout << colors::green("read database: ") << db.size() << endl;
return db; return db;
} }
static Mozaic create_mozaic(Database const & db, string const & filename, int h_tiles, int v_tiles){ static auto calculate_all_distances(Database const & db, string const & filename, int h_tiles, int v_tiles){
Mozaic mozaic; using return_type = decltype(db.distances_for_image(std::declval<av::frame const &>()));
Mozaic<return_type> mozaic(h_tiles, v_tiles);
apply_to_tiles(filename, h_tiles, v_tiles, [&](int c, int r, av::frame const & frame){ apply_to_tiles(filename, h_tiles, v_tiles, [&](int c, int r, av::frame const & frame){
auto const index = db.nearest_image(frame); mozaic[r][c] = db.distances_for_image(frame);
cout << colors::red("tile ") << c << ", " << r << ": " << db.filename(index) << endl;
mozaic[make_pair(c, r)] = db.filename(index);
}); });
return mozaic; return mozaic;
} }
static void save_mozaic(Mozaic const & mozaic, string filename, int h_tiles, int v_tiles){ static void save_mozaic(Mozaiq const & mozaic, string filename){
auto const pix_fmt = AV_PIX_FMT_YUVJ444P; auto const pix_fmt = AV_PIX_FMT_YUVJ444P;
auto const codec_id= AV_CODEC_ID_MJPEG; auto const codec_id= AV_CODEC_ID_MJPEG;
// Open all files we need // Open all files we need
map<string, av::frame> frames; map<string, av::frame> frames;
for(auto&& x : mozaic){ for(auto&& y : mozaic.tiles){
for(auto&& x : y){
// only open a file once // only open a file once
if(frames.count(x.second) > 0) continue; if(frames.count(x) > 0) continue;
frames.emplace(x.second, crop_to_square(open_image(x.second))); frames.emplace(x, crop_to_square(open_image(x)));
}
} }
auto const total_width = h_tiles * tile_width; auto const total_width = mozaic.h_tiles * tile_width;
auto const total_height = v_tiles * tile_height; auto const total_height = mozaic.v_tiles * tile_height;
// Create output frame // Create output frame
std::vector<uint8_t, av::allocator<uint8_t>> data(make_u(avpicture_get_size(pix_fmt, total_width, total_height)), 0); std::vector<uint8_t, av::allocator<uint8_t>> data(make_u(avpicture_get_size(pix_fmt, total_width, total_height)), 0);
@ -97,13 +98,13 @@ static void save_mozaic(Mozaic const & mozaic, string filename, int h_tiles, int
// For each tile: get the part, copy input to it // For each tile: get the part, copy input to it
av::frame frame_part = av::frame_clone(frame); av::frame frame_part = av::frame_clone(frame);
for(int r = 0; r < v_tiles; ++r) { for(int r = 0; r < mozaic.v_tiles; ++r) {
for(int c = 0; c < h_tiles; ++c){ for(int c = 0; c < mozaic.h_tiles; ++c){
av_picture_crop(reinterpret_cast<AVPicture*>(frame_part.get()), reinterpret_cast<AVPicture*>(frame.get()), av::get_format(frame), r * tile_height, c * tile_width); av_picture_crop(reinterpret_cast<AVPicture*>(frame_part.get()), reinterpret_cast<AVPicture*>(frame.get()), av::get_format(frame), r * tile_height, c * tile_width);
frame_part->width = tile_width; frame_part->width = tile_width;
frame_part->height = tile_height; frame_part->height = tile_height;
auto&& input = frames.at(mozaic.at(make_pair(c, r))); auto&& input = frames.at(mozaic[r][c]);
auto sws_context = sws_getContext(input->width, input->height, av::get_format(input), frame_part->width, frame_part->height, av::get_format(frame_part), 0, nullptr, nullptr, nullptr); auto sws_context = sws_getContext(input->width, input->height, av::get_format(input), frame_part->width, frame_part->height, av::get_format(frame_part), 0, nullptr, nullptr, nullptr);
if(!sws_context) throw std::runtime_error("boem sws context"); if(!sws_context) throw std::runtime_error("boem sws context");
sws_scale (sws_context, {input->data}, {input->linesize}, 0, input->height, {frame_part->data}, {frame_part->linesize}); sws_scale (sws_context, {input->data}, {input->linesize}, 0, input->height, {frame_part->data}, {frame_part->linesize});
@ -131,7 +132,6 @@ static void save_mozaic(Mozaic const & mozaic, string filename, int h_tiles, int
std::vector<uint8_t> buffer(make_u(buffer_size), 0); std::vector<uint8_t> buffer(make_u(buffer_size), 0);
auto const output_size = avcodec_encode_video(codec_ctx.get(), buffer.data(), buffer_size, frame.get()); auto const output_size = avcodec_encode_video(codec_ctx.get(), buffer.data(), buffer_size, frame.get());
assert(output_size <= buffer_size); assert(output_size <= buffer_size);
cout << "output size" << output_size << endl;
auto file = fopen(filename.c_str(), "wb"); auto file = fopen(filename.c_str(), "wb");
fwrite(buffer.data(), 1, make_u(output_size), file); fwrite(buffer.data(), 1, make_u(output_size), file);
@ -140,22 +140,29 @@ static void save_mozaic(Mozaic const & mozaic, string filename, int h_tiles, int
int main(){ int main(){
av_register_all(); av_register_all();
av_log_set_level(AV_LOG_QUIET);
// TODO: use boost::program_options
string const database_directory = "database"; string const database_directory = "database";
string const filename = "image.jpg"; string const filename = "image.jpg";
string const output = "output.jpg"; string const output = "output.jpg";
int const h_tiles = 4 * 7; int const h_tiles = 4 * 7;
int const v_tiles = 3 * 7; int const v_tiles = 3 * 7;
// NOTE: we could almost do the rest on one line ;D
// TODO: make every step parallel (this should be easy, except for the IO steps)
auto const db = read_database(database_directory); auto const db = read_database(database_directory);
auto const mozaic = create_mozaic(db, filename, h_tiles, v_tiles); cout << colors::green("Read database: ") << db.size() << endl;
save_mozaic(mozaic, output, h_tiles, v_tiles);
// debugging auto const distances = calculate_all_distances(db, filename, h_tiles, v_tiles);
for(int r = 0; r < v_tiles; ++r){ cout << colors::green("Calculated distances for ") << filename << endl;
for(int c = 0; c < h_tiles; ++c){
cout << mozaic.at(make_pair(c, r)) << "\t"; auto const best_indices = optimize(distances, 1);
} cout << colors::green("Picked best distances") << endl;
cout << endl;
} auto const mozaic = mozaic_fmap(best_indices, [&](auto i){ return db.filename(i); });
cout << colors::green("Picked filenames") << endl;
save_mozaic(mozaic, output);
cout << colors::green("Saved Mozaic to ") << output << endl;
} }

47
src/needle.cpp

@ -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…
Cancel
Save