329 lines
12 KiB
C++
329 lines
12 KiB
C++
#include "DescriptorManager.h"
|
|
|
|
//Default constructor, don't use this
|
|
DescriptorManager::DescriptorManager() {
|
|
img_path = "data"; //default value
|
|
}
|
|
|
|
//Sets image path and defines the image descriptors.
|
|
DescriptorManager::DescriptorManager(const fs::path & image_path) {
|
|
img_path = image_path;
|
|
|
|
//Manual definitions.. SAD!
|
|
/*descriptor_types[7] = new DCTMedian();
|
|
descriptor_types[6] = new DCTPearson();
|
|
descriptor_types[5] = new threshold();
|
|
descriptor_types[4] = new pearson();
|
|
descriptor_types[3] = new sobel();
|
|
descriptor_types[2] = new median();
|
|
descriptor_types[1] = new numColors();*/
|
|
descriptor_types[0] = new averageColor();
|
|
|
|
mutators[0] = new Grayscale();
|
|
mutators[1] = new Crop();
|
|
mutators[2] = new Flop();
|
|
mutators[3] = new JPEG();
|
|
mutators[4] = new LowQ();
|
|
mutators[5] = new Hue();
|
|
mutators[6] = new Brighten();
|
|
mutators[7] = new Watermark();
|
|
}
|
|
|
|
//No need to call destructor.. yet?
|
|
DescriptorManager::~DescriptorManager() {
|
|
//Do some clean-up?
|
|
//Realistically, when this gets destroyed the application is done anyway.
|
|
//Let the OS take care of garbage collection
|
|
}
|
|
|
|
//Loads all base images, calculates their descriptors,
|
|
//calculates all image mutations and stores their descriptors
|
|
//on disk.
|
|
void DescriptorManager::Init() {
|
|
//First load all images and store descriptors in cache and disk
|
|
if (!fs::exists(img_path)) {
|
|
cout << "Error. Data folder not found." << endl;
|
|
return;
|
|
}
|
|
fs::ifstream ifs;
|
|
fs::ofstream ofs;
|
|
//LoadFeatures();
|
|
|
|
numImages = std::count_if(
|
|
fs::directory_iterator(img_path),
|
|
fs::directory_iterator(),
|
|
static_cast<bool(*)(const fs::path&)>(fs::is_regular_file));
|
|
//foreach image in directory (non-recursive)
|
|
int filecount = 0;
|
|
for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) {
|
|
if (!itr->path().has_extension()) continue; //skip directories and such
|
|
filecount++;
|
|
string image_name = itr->path().filename().generic_string();
|
|
cout << setw(60) << setfill('*') << "\r" << string(60, ' ') << flush;
|
|
cout << setfill('*') << setw(60) << right<< "\rProgress: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << "): " << image_name << flush;
|
|
Image* image = new Image;
|
|
image->read(itr->path().string());
|
|
//foreach descriptor
|
|
for (int i = 0; i < numDescriptors; i++) {
|
|
//First check if we already have this information from the loaded cache
|
|
if (features[i].count(image_name) > 0) {
|
|
//cout << "We already have feature for " << image_name << endl;
|
|
continue;
|
|
}
|
|
//folder to store descriptors; create if not exists
|
|
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
|
|
if (!fs::is_directory(descriptorBase))
|
|
fs::create_directory(descriptorBase);
|
|
//cout << "Starting with descriptor " << i << " on location " << descriptorBase.generic_string() << endl;
|
|
|
|
fs::path featurePath(descriptorBase / itr->path().stem());
|
|
uint64_t b1;
|
|
if (fs::exists(featurePath)) { //feature on disk, load it in
|
|
ifs.open(featurePath, fs::fstream::binary);
|
|
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
|
|
ifs.close();
|
|
}
|
|
else { //not on disk, calculate it
|
|
b1 = descriptor_types[i]->feature(image);
|
|
ofs.open(featurePath, fs::fstream::binary);
|
|
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
|
|
ofs.close();
|
|
}
|
|
features[i][image_name] = b1;
|
|
//SaveFeatures();
|
|
}
|
|
for (int i = 0; i < numMutations; i++) {
|
|
//Check if we have somewhere to store the mutation
|
|
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
|
if (!fs::is_directory(mutationBase))
|
|
fs::create_directory(mutationBase);
|
|
bool missing = false;
|
|
for (int j = 0; j < numDescriptors; j++) {
|
|
if (mutatedFeatures[j][i].count(image_name) <= 0) {
|
|
missing = true;
|
|
}
|
|
}
|
|
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
|
Image* mutated;
|
|
if (!fs::exists(mutPath)) { //only mutate if nothing on disk
|
|
//Measured: about 10ms
|
|
auto start = high_resolution_clock::now();
|
|
mutated = mutators[i]->getMutated(image);
|
|
mutated->write(mutPath.generic_string());
|
|
auto duration = duration_cast<microseconds>(high_resolution_clock::now() - start);
|
|
//cout << "Calculating mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
|
|
|
|
}
|
|
else {
|
|
//Measured: about 4ms
|
|
auto start = high_resolution_clock::now();
|
|
mutated = new Image;
|
|
mutated->read(mutPath.generic_string());
|
|
auto duration = duration_cast<microseconds>(high_resolution_clock::now() - start);
|
|
//cout << "Loading mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl;
|
|
}
|
|
|
|
//now calculate all the features for the mutated images as well
|
|
//foreach descriptor
|
|
for (int j = 0; j < numDescriptors; j++) {
|
|
//folder to store descriptors; create if not exists
|
|
fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath());
|
|
if (!fs::is_directory(descriptorBase))
|
|
if (fs::create_directory(descriptorBase)) {}
|
|
fs::path featurePath(descriptorBase / itr->path().stem());
|
|
//cout << featurePath << endl;
|
|
string image_name = itr->path().filename().generic_string();
|
|
uint64_t b1;
|
|
if (!fs::exists(featurePath)) { //feature not yet calculated
|
|
//descriptor_types[i]->distance(b1, b1);
|
|
//descriptors[i][image_name] = b1;
|
|
b1 = descriptor_types[j]->feature(mutated);
|
|
//cout << "Writing " << *b1 << "(" << n << ")" << " to " << featurePath << endl;
|
|
ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done
|
|
ofs.write(reinterpret_cast<const char*>(&b1), sizeof(b1));
|
|
ofs.close();
|
|
}
|
|
else {
|
|
ifs.open(featurePath, fs::fstream::binary);
|
|
ifs.read(reinterpret_cast<char*>(&b1), sizeof(b1));
|
|
ifs.close();
|
|
}
|
|
|
|
mutatedFeatures[j][i][image_name] = b1;
|
|
}
|
|
delete mutated;
|
|
}
|
|
delete image;
|
|
}
|
|
initialized = true;
|
|
}
|
|
|
|
|
|
//Saves feature map to disk.
|
|
//Makes it so that it can reload easily
|
|
void DescriptorManager::SaveFeatures() {
|
|
{ //Boost serialization
|
|
ofstream f("features.map", ios::binary);
|
|
//ofstream feature_cache_backup("features.map.bak", ios::binary);
|
|
//feature_cache_backup << feature_cache.rdbuf();
|
|
ar::binary_oarchive oa(f);
|
|
oa << features;
|
|
|
|
ofstream fm("mutated_features.map", ios::binary);
|
|
ar::binary_oarchive oam(fm);
|
|
oam << mutatedFeatures;
|
|
}
|
|
/*{ //YAS serialization
|
|
yas::file_ostream os("features.yas", yas::file_mode::file_trunc);
|
|
//const std::size_t flg = yas::binary|yas::file;
|
|
yas::binary_oarchive<yas::file_ostream, yas::file|yas::binary> oa(os);
|
|
oa & features;
|
|
//yas::save<yas::file|yas::binary>(image_name, features);
|
|
}*/
|
|
}
|
|
|
|
//Loads saved feature maps from disk.
|
|
void DescriptorManager::LoadFeatures() {
|
|
{ //Boost
|
|
ifstream f("features.map", ios::binary);
|
|
if (f.is_open()) {
|
|
ar::binary_iarchive bi(f);
|
|
bi >> features;
|
|
}
|
|
ifstream fm("mutated_features.map", ios::binary);
|
|
if (fm.is_open()) {
|
|
ar::binary_iarchive bim(fm);
|
|
bim >> mutatedFeatures;
|
|
}
|
|
}
|
|
{ //YAS
|
|
|
|
}
|
|
}
|
|
|
|
//Sorts all base (unmutated) images in order of distance from the given image
|
|
//After this function the distances array will be filled
|
|
void DescriptorManager::rankImages(string image_name, int method) {
|
|
uint64_t target;
|
|
if (features[method].count(image_name) && initialized) { //if we have image
|
|
target = features[method][image_name];
|
|
}
|
|
else {
|
|
return;
|
|
}
|
|
distances.clear(); //start fresh
|
|
for (map<string, uint64_t>::iterator it = features[method].begin(); it != features[method].end(); ++it) {
|
|
unsigned long long d = descriptor_types[method]->distance(target, it->second);
|
|
pair<unsigned long long, string> p(d, it->first);
|
|
distances.insert(p); //pairs are compared by their first element..
|
|
}
|
|
}
|
|
|
|
//Adds mutated descriptors of a given image to a copy of the distances array.
|
|
//It inserts these mutated descriptors in a sorted way
|
|
//Assumes the distances array is filled with the normal distances
|
|
multiset<pair<unsigned long long, string>> DescriptorManager::rankMutations(string image_name, int method) {
|
|
uint64_t target;
|
|
if (features[method].count(image_name) && initialized) { //this should always be the case
|
|
target = features[method][image_name];
|
|
}
|
|
|
|
//cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
|
|
multiset<pair<unsigned long long, string>> local_distance = distances; //get our local copy of the distances.
|
|
for (int i = 0; i < numMutations; i++) {
|
|
uint64_t b1 = mutatedFeatures[method][i][image_name];
|
|
unsigned long long d = descriptor_types[method]->distance(target, b1);
|
|
pair<unsigned long long, string> p(d, mutators[i]->ToString());
|
|
local_distance.insert(p);
|
|
//upper_bound
|
|
}
|
|
return local_distance;
|
|
/*
|
|
//cout << "..." << endl;
|
|
for (int i = 0; i < numMutations; i++) {
|
|
int j = 0;
|
|
for (std::multiset<pair<unsigned long long, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) {
|
|
//cout << "hey " << endl;
|
|
if (it->second == mutators[i]->ToString()) {
|
|
cout << j << "th position: ";
|
|
cout << "(" << it->first << ", " << it->second << ")" << endl;
|
|
}
|
|
j++;
|
|
}
|
|
}*/
|
|
}
|
|
|
|
//Needs to be called after Init().
|
|
//Takes all the calculated features and rankes
|
|
void DescriptorManager::runExperiments() {
|
|
fs::ofstream ofs;
|
|
for (int i = 0; i < numDescriptors; i++) {
|
|
for (map<string, uint64_t>::iterator it = features[i].begin(); it != features[i].end(); ++it) {
|
|
//cout << "=== " << descriptor_types[i]->ToString() << ": " << it->first << " ===" << endl;
|
|
rankImages(it->first, i);
|
|
multiset<pair<unsigned long long, string>> local_distance = rankMutations(it->first, i);
|
|
|
|
//Now we can draw some conclusions from the received data:
|
|
//Put all results in some useful files in the results directory:
|
|
fs::path resultFolder("results" / descriptor_types[i]->ToPath());
|
|
if (!fs::is_directory(resultFolder)) //TODO: Create all folders in Init()
|
|
fs::create_directory(resultFolder);
|
|
for (int i = 0; i < numMutations; i++) {
|
|
int j = 0;
|
|
fs::path resultPath(resultFolder / mutators[i]->ToPath());
|
|
ofs.open(resultPath, ofstream::out | ofstream::app);
|
|
for (std::multiset<pair<unsigned long long, string>>::iterator it = local_distance.begin(); it != local_distance.end(); ++it) {
|
|
if (it->second == mutators[i]->ToString()) {
|
|
double percentile = (j + 1) / (double)(numImages+numMutations);
|
|
|
|
ofs << (int)(percentile*100) << endl;
|
|
//cout << "(" << it->first << ", " << it->second << ")" << endl;
|
|
}
|
|
j++;
|
|
}
|
|
ofs.close();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
//outputs the gathered data to iostream
|
|
//can be called directly or is called by its overload
|
|
//assumes a < numDescriptors
|
|
void DescriptorManager::outputMap(int a) {
|
|
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
|
|
map<string, uint64_t> a_map = features[a];
|
|
if (a_map.begin() == a_map.end()) {
|
|
cout << "------------------------" << endl << "No data found" << endl << endl;
|
|
}
|
|
|
|
for (map<string, uint64_t>::iterator it = a_map.begin(); it != a_map.end(); ++it) {
|
|
cout << it->first << " => " << it->second << " (" << bitset<64>(it->second) << ")" << endl;
|
|
}
|
|
}
|
|
|
|
//calls its buddy function to output all data to iostream
|
|
void DescriptorManager::outputMap() {
|
|
for (int i = 0; i < numDescriptors; i++) {
|
|
outputMap(i);
|
|
}
|
|
}
|
|
|
|
//Outputs all generated features ordered on distance from the query image.
|
|
void DescriptorManager::outputRank(string imagename, int method) {
|
|
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end(); ++it) {
|
|
cout << "(" << it->first << ", " << it->second << ")" << endl;
|
|
}
|
|
}
|
|
|
|
//Just runs the preview functions of all the descriptors on given image.
|
|
//No need to init before this.
|
|
void DescriptorManager::generatePreviews(string imagename) {
|
|
fs::path imagePath(img_path / imagename);
|
|
Image* img = new Image(imagePath.generic_string());
|
|
for (int i = 0; i < numDescriptors; i++) {
|
|
descriptor_types[i]->preview(img);
|
|
}
|
|
}
|