From d0cbf6b4206b0a082b3e2288c68ec4f47da9e753 Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Wed, 10 Oct 2018 14:50:12 +0200 Subject: [PATCH] New init done. Still needs some testing. --- DescriptorManager.cpp | 41 +++++++++++++++++++++++++---------------- DescriptorManager.h | 3 ++- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/DescriptorManager.cpp b/DescriptorManager.cpp index 21f938d..01824a4 100644 --- a/DescriptorManager.cpp +++ b/DescriptorManager.cpp @@ -74,7 +74,7 @@ void DescriptorManager::Init() { ofs.write(reinterpret_cast(&n), sizeof(n)); ofs.close(); } - descriptors[i][image_name] = *b1; + features[i][image_name] = *b1; delete b1; } for (int i = 0; i < numMutations; i++) { @@ -90,25 +90,34 @@ void DescriptorManager::Init() { //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 + //folder to store descriptors; create if not exists fs::path descriptorBase("meta" / descriptor_types[j]->ToPath() / mutators[i]->ToPath()); - //cout << "Doing mutated descriptors for " << descriptorBase << endl; 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(); + bitset<64>* b1; if (!fs::exists(featurePath)) { //feature not yet calculated - //cout << "Calculating " << image_name << endl; //descriptor_types[i]->distance(b1, b1); //descriptors[i][image_name] = b1; - bitset<64> b1 = descriptor_types[j]->feature(mutated); - unsigned long long n = b1.to_ullong(); + b1 = &descriptor_types[j]->feature(mutated); + unsigned long long n = b1->to_ullong(); 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(&n), sizeof(n)); ofs.close(); - }*/ + } + else { + ifs.open(featurePath, fs::fstream::binary); + unsigned long long n; + ifs.read(reinterpret_cast(&n), sizeof(n)); + ifs.close(); + b1 = new bitset<64>(n); + } + //Else...? + //Do we need to store mutated features in a cache? For large experiments it's probably faster. + mutatedFeatures[j][i][image_name] = *b1; } delete mutated; } @@ -146,7 +155,7 @@ bool DescriptorManager::loadDescriptors() { ifs.close(); bitset<64> b1(n); - descriptors[i][image_name] = b1; + features[i][image_name] = b1; } else { //feature not yet calculated Image* image; //only load image from disk if not loaded before @@ -161,7 +170,7 @@ bool DescriptorManager::loadDescriptors() { //cout << "Calculating " << image_name << endl; bitset<64> b1 = descriptor_types[i]->feature(image); //descriptor_types[i]->distance(b1, b1); - descriptors[i][image_name] = b1; + features[i][image_name] = b1; unsigned long long n = b1.to_ullong(); ofs.open(featurePath, fs::fstream::binary); //in binary mode; only open file after calculations have been done @@ -234,8 +243,8 @@ bool DescriptorManager::createMutations() { //essentially insertion sort into a map void DescriptorManager::similarImages(string image_name, int method, int numImages) { bitset<64> target; - if (descriptors[method].count(image_name)) { //if image exists in map - target = descriptors[method][image_name]; + if (features[method].count(image_name)) { //if image exists in map + target = features[method][image_name]; } else { Image* image = new Image; @@ -246,7 +255,7 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag //first we need all distances to this image.. and insert them in a sorted way //multiset > distances; //multiset is probably not needed, but who knows.. //distances.clear(); //start fresh - for (map>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) { + for (map>::iterator it = features[method].begin(); it != features[method].end(); ++it) { //cout << "ho " << endl; unsigned long long d = descriptor_types[method]->distance(target, it->second); pair p(d, it->first); @@ -269,11 +278,11 @@ void DescriptorManager::insertMutations(string image_name, int method) { multiset > local_distance; fs::ifstream ifs; bitset<64> target; - if (descriptors[method].count(image_name)) { //if image exists in map - target = descriptors[method][image_name]; + if (features[method].count(image_name)) { //if image exists in map + target = features[method][image_name]; } cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl; - for (map>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) { + for (map>::iterator it = features[method].begin(); it != features[method].end(); ++it) { //cout << "ho " << endl; unsigned long long d = descriptor_types[method]->distance(target, it->second); pair p(d, it->first); @@ -329,7 +338,7 @@ void DescriptorManager::checkImages(int method) { void DescriptorManager::outputMap(int a) { if (a != 2) return; cout << "Outputting map " << descriptor_types[a]->ToString() << endl; - map> a_map = descriptors[a]; + map> a_map = features[a]; if (a_map.begin() == a_map.end()) { cout << "------------------------" << endl << "No data found" << endl << endl; } diff --git a/DescriptorManager.h b/DescriptorManager.h index 3cd30c9..6b2df39 100644 --- a/DescriptorManager.h +++ b/DescriptorManager.h @@ -39,7 +39,8 @@ private: static const int numMutations = 7; //TODO: automagically update this? - map> descriptors[numDescriptors]; //array of maps that will hold the descriptors + 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];