From 845a3309b0626a02473c919b9727d25165974d9a Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Sat, 29 Jun 2019 13:52:38 +0200 Subject: [PATCH] Code cleanups --- DescriptorManager.cpp | 47 ++++++------------------------------------- DescriptorManager.h | 13 ++++++------ main.cpp | 8 -------- 3 files changed, 12 insertions(+), 56 deletions(-) diff --git a/DescriptorManager.cpp b/DescriptorManager.cpp index dd07ed3..4577b27 100644 --- a/DescriptorManager.cpp +++ b/DescriptorManager.cpp @@ -1,10 +1,6 @@ #include "DescriptorManager.h" -//Default constructor, don't use this -DescriptorManager::DescriptorManager() { - img_path = "data"; //default value -} - +DescriptorManager::DescriptorManager() {} //don't use this DescriptorManager::DescriptorManager(const fs::path & image_path) { img_path = image_path; if (!fs::exists(img_path)) { @@ -75,9 +71,7 @@ Image* DescriptorManager::GetMutated(Image* image, string image_name, int method return mutated; } -//Takes the unmodified image and loads its features into memory. -//Either by reading them from disk, or calculating them directly. -//For each of the defined descriptors. +//Fills the features array with all descriptors for the given image. void DescriptorManager::LoadFeatures(string image_path, string image_name) { for (int i = 0; i < numDescriptors; i++) { //First check if we already have this information from the loaded cache @@ -104,6 +98,7 @@ void DescriptorManager::LoadFeatures(string image_path, string image_name) { } } +//Fills the mutatedFeatures array with all descriptors for the given mutation and image. void DescriptorManager::LoadMutationFeatures(string image_path, string image_name, int mutation) { for (int i = 0; i < numDescriptors; i++) { //First check if we already have this information from the loaded cache @@ -132,14 +127,13 @@ void DescriptorManager::LoadMutationFeatures(string image_path, string image_nam } //Calculates all features, mutations and features of mutations. -//This is generally the first step required +//Do this before anything else. void DescriptorManager::Init() { LoadFromDisk(); 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 string image_name = itr->path().filename().generic_string(); - //Image* image = new Image; string image_path = itr->path().string(); { //Progress bar filecount++; @@ -154,20 +148,15 @@ void DescriptorManager::Init() { } if (current_image) delete current_image; current_image = NULL; - //delete image; SaveToDisk(); } - initialized = true; } //Saves feature map to disk. -//Makes it so that it can reload easily void DescriptorManager::SaveToDisk() { { //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; @@ -175,13 +164,6 @@ void DescriptorManager::SaveToDisk() { 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 oa(os); - oa & features; - //yas::save(image_name, features); - }*/ } //Loads saved feature maps from disk. @@ -198,16 +180,13 @@ void DescriptorManager::LoadFromDisk() { 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 + if (features[method].count(image_name)) { //Check if this image was properly initialized target = features[method][image_name]; } else { @@ -226,7 +205,7 @@ void DescriptorManager::rankImages(string image_name, int method) { //Assumes the distances array is filled with the normal distances multiset> DescriptorManager::rankMutations(string image_name, int method) { uint64_t target; - if (features[method].count(image_name) && initialized) { //this should always be the case + if (features[method].count(image_name)) { //Check if this image was properly initialized target = features[method][image_name]; } @@ -240,22 +219,8 @@ multiset> DescriptorManager::rankMutations(string image_n //upper_bound } return local_distance; - /* - //cout << "..." << endl; - for (int i = 0; i < numMutations; i++) { - int j = 0; - for (std::multiset>::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; diff --git a/DescriptorManager.h b/DescriptorManager.h index c89a859..560db92 100644 --- a/DescriptorManager.h +++ b/DescriptorManager.h @@ -56,18 +56,17 @@ public: void outputMap(); void outputRank(string imagename, int method); private: + fs::path img_path; + int numImages; + static const int numDescriptors = 8; static const int numMutations = 8; - //TODO: automagically update this? - - bool initialized = false; - int numImages; + Descriptor* descriptor_types[numDescriptors]; + Mutation* mutators[numMutations]; + Image* current_image = NULL; //for caching purposes map features[numDescriptors]; //array of maps that will hold the descriptors map mutatedFeatures[numDescriptors][numMutations]; multiset > distances; //multiset that stores the distances - fs::path img_path; //where the images are - Descriptor* descriptor_types[numDescriptors]; - Mutation* mutators[numMutations]; }; diff --git a/main.cpp b/main.cpp index 2322c43..e5d644e 100644 --- a/main.cpp +++ b/main.cpp @@ -40,14 +40,6 @@ int main(int argc, char **argv) { } InitializeMagick(*argv); DescriptorManager* manager = new DescriptorManager("test"); - //smanager->generatePreviews(imagename); - //cout << "Intializing..." << endl; manager->Init(); - //cout << endl; - //manager->outputMap(); - //manager->outputRank(imagename, method); - //cout << "done!" << endl; -// manager->runExperiments(); - return 0; }