From 9bde653a2ad6991f8b6887a36a87b2c4ea0a677a Mon Sep 17 00:00:00 2001 From: Mark Hoekveen Date: Tue, 4 Jun 2019 19:44:49 +0200 Subject: [PATCH] First two descriptors migrated --- Descriptor.h | 86 +++++++++++++++++++++---------------------- DescriptorManager.cpp | 61 ++++++++++++++---------------- DescriptorManager.h | 6 +-- main.cpp | 4 +- 4 files changed, 76 insertions(+), 81 deletions(-) diff --git a/Descriptor.h b/Descriptor.h index d80f331..03f1786 100644 --- a/Descriptor.h +++ b/Descriptor.h @@ -16,11 +16,11 @@ namespace fs = boost::filesystem; //but should be overloaded by a child class class Descriptor { public: - virtual bitset<64> feature(Image* img) = 0; + virtual uint64_t feature(Image* img) = 0; //Interpret the bitsets as integers and calculate their distance. - virtual unsigned long long distance(bitset<64> a, bitset<64> b) { - return abs(((long long)a.to_ullong() - (long long)b.to_ullong())); + virtual uint64_t distance(uint64_t a, uint64_t b) { + return abs((long long)a - (long long)b); }; virtual string ToString() = 0; virtual fs::path ToPath() { @@ -28,7 +28,7 @@ public: } //should return true if a > b - virtual bool bigger(bitset<64> a, bitset<64> b) { + virtual bool bigger(uint64_t a, uint64_t b) { return true; } @@ -49,7 +49,7 @@ public: // Pixels view(*img); // int chan = (int)img->channels(); //should be 1, just intensity // const Quantum *p = view.getConst(0, 0, n, m); //entire image - // bitset<64> retval(0); + // uint64_t retval(0); // int N = n*m; // Image* newimg = new Image(Geometry(n, m), Color("white")); // newimg->type(GrayscaleType); @@ -92,14 +92,14 @@ public: //with 8 bits per color. class averageColor : public Descriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { ssize_t n = img->columns(); ssize_t m = img->rows(); Pixels view(*img); const Quantum *p = view.getConst(0, 0, n, m); //entire image unsigned long long red = 0, green = 0, blue = 0; float numpixels = (float)(n*m); - if (img->channels() < 3) return bitset<64>(0); + if (img->channels() < 3) return 0; for (ssize_t j = 0; j < m; j++) { for (ssize_t i = 0; i < n; i++) { //LTR scanning @@ -114,36 +114,36 @@ public: int g = ((green/numpixels) / QuantumRange) * 255; int b = ((blue/numpixels) / QuantumRange) * 255; //now store all this into a bitset.. assuming 8 bits per color, if everything went correctly.. which we should check.. probably.. - bitset<64> retval(r); + uint64_t retval = r; retval <<= 8; - retval |= bitset<64>(g); + retval += g; retval <<= 8; - retval |= bitset<64>(b); + retval += b; return retval; } - unsigned long long distance(bitset<64> a, bitset<64> b) { + uint64_t distance(uint64_t a, uint64_t b) { //do some bit shifting magic to get ints back - int red_a = (a >> 16).to_ulong(); - int green_a = (a << (64-16) >> (64-8)).to_ulong(); - int blue_a = (a << (64-8) >> (64-8)).to_ulong(); - int red_b = (b >> 16).to_ulong(); - int green_b = (b << (64 - 16) >> (64 - 8)).to_ulong(); - int blue_b = (b << (64 - 8) >> (64 - 8)).to_ulong(); + int red_a = (a >> 16); + int green_a = (a << (64-16) >> (64-8)); + int blue_a = (a << (64-8) >> (64-8)); + int red_b = (b >> 16); + int green_b = (b << (64 - 16) >> (64 - 8)); + int blue_b = (b << (64 - 8) >> (64 - 8)); return abs(red_a - red_b) + abs(green_a - green_b) + abs(blue_a - blue_b); } string ToString() { return "averageColor"; } }; - +/* //Counts the total number of colors //and stores it in bit representation. //Upper limit 16777216 //Only uses least significant 24 bits class numColors : public Descriptor { public: - bitset<64> feature(Image* img) { - return bitset<64>(img->totalColors()-1); + uint64_t feature(Image* img) { + return uint64_t(img->totalColors()-1); } string ToString() { return "numColors"; @@ -155,7 +155,7 @@ public: //higher than median and 0 otherwise. class median : public Descriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); local_image->type(GrayscaleType); local_image->sample(Geometry("8x8!")); //64 pixels @@ -181,13 +181,13 @@ public: sort(intensities, intensities + numpixels); //We know where the median is going to be: intensity = (float)(intensities[31] + intensities[32]) / 2; - bitset<64> retval(0); + uint64_t retval(0); const Quantum *q = view.getConst(0, 0, n, m); //entire image for (ssize_t j = 0; j < m; j++) { for (ssize_t i = 0; i < n; i++) { retval <<= 1; if (q[0] > intensity) - retval |= bitset<64>(1); + retval |= uint64_t(1); q = q + local_image->channels(); } } @@ -197,7 +197,7 @@ public: } //biterror - unsigned long long distance(bitset<64> a, bitset<64> b) { + unsigned long long distance(uint64_t a, uint64_t b) { int biterror = 0; for (int i = 0; i < 64; i++) { if (a[i] != b[i]) biterror++; @@ -214,7 +214,7 @@ public: //sobel convolution applied in both directions class sobel : public Descriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); //TODO: Some preprocessing for this? //img->reduceNoise(4.0); @@ -242,7 +242,7 @@ public: } int scaledIntensity = ((float)(intensity / (n*m)) / QuantumRange) * 255; delete local_image; - return bitset<64>(scaledIntensity); + return uint64_t(scaledIntensity); } string ToString() { return "sobel"; @@ -258,7 +258,7 @@ private: //0 otherwise class pearson : public Descriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); local_image->type(GrayscaleType); local_image->sample(Geometry("8x8!")); //64 pixels @@ -267,14 +267,14 @@ public: Pixels view(*local_image); int chan = (int)local_image->channels(); const Quantum *p = view.getConst(0, 0, n, m); //entire image - bitset<64> retval(0); + uint64_t retval(0); int prev = p[0]; //so we always start with a 0 for (ssize_t j = 0; j < m; j++) { for (ssize_t i = 0; i < n; i++) { //LTR scanning //NOTE: Scanning order might be quite relevant for this descriptor int cur = p[0]; - if(prev < cur) retval |= bitset<64>(1); //XOR + if(prev < cur) retval |= uint64_t(1); //XOR prev = cur; p = p + chan; @@ -284,7 +284,7 @@ public: delete local_image; return retval; } - unsigned long long distance(bitset<64> a, bitset<64> b) { + unsigned long long distance(uint64_t a, uint64_t b) { int biterror = 0; for (int i = 0; i < 64; i++) { if (a[i] != b[i]) biterror++; @@ -301,7 +301,7 @@ public: //neighbourhood and stores it in a LTR map class threshold : public Descriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); local_image->type(GrayscaleType); local_image->sample(Geometry("8x8!")); //64 pixels @@ -311,11 +311,11 @@ public: Pixels view(*local_image); int chan = (int)local_image->channels(); const Quantum *p = view.getConst(0, 0, n, m); //entire image - bitset<64> retval(0); + uint64_t retval(0); for (ssize_t j = 0; j < m; j++) { for (ssize_t i = 0; i < n; i++) { if(p[0] != 0) - retval |= bitset<64>(1); //XOR + retval |= uint64_t(1); //XOR retval <<= 1; p = p + chan; @@ -324,7 +324,7 @@ public: delete local_image; return retval; } - unsigned long long distance(bitset<64> a, bitset<64> b) { + unsigned long long distance(uint64_t a, uint64_t b) { int biterror = 0; for (int i = 0; i < 64; i++) { if (a[i] != b[i]) biterror++; @@ -382,15 +382,15 @@ public: //neighbourhood and stores it in a LTR map class DCTPearson : public DCTDescriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); double* converted = CalculateDCT(local_image); double prev = 0; - bitset<64> retval(0); + uint64_t retval(0); for (int i = 0; i < 64; i++) { if (converted[i] > prev) { - retval |= bitset<64>(1); //XOR + retval |= uint64_t(1); //XOR } retval <<= 1; prev = converted[i]; @@ -398,7 +398,7 @@ public: delete local_image; return retval; } - unsigned long long distance(bitset<64> a, bitset<64> b) { + unsigned long long distance(uint64_t a, uint64_t b) { int biterror = 0; for (int i = 0; i < 64; i++) { if (a[i] != b[i]) biterror++; @@ -427,7 +427,7 @@ public: class DCTMedian : public DCTDescriptor { public: - bitset<64> feature(Image* img) { + uint64_t feature(Image* img) { Image* local_image = new Image(*img); double* converted = CalculateDCT(local_image); double bullshit[64]; @@ -437,16 +437,16 @@ public: sort(bullshit, bullshit + 64); float median = (float)(bullshit[31] + bullshit[32]) / 2; - bitset<64> retval(0); + uint64_t retval(0); for (ssize_t i = 0; i < 64; i++) { retval <<= 1; if (converted[i] > median) - retval |= bitset<64>(1); + retval |= uint64_t(1); } delete local_image; return retval; } - unsigned long long distance(bitset<64> a, bitset<64> b) { + unsigned long long distance(uint64_t a, uint64_t b) { int biterror = 0; for (int i = 0; i < 64; i++) { if (a[i] != b[i]) biterror++; @@ -456,4 +456,4 @@ public: string ToString() { return "DCTMedian"; } -}; +};*/ diff --git a/DescriptorManager.cpp b/DescriptorManager.cpp index 5be040b..b106eab 100644 --- a/DescriptorManager.cpp +++ b/DescriptorManager.cpp @@ -10,13 +10,13 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) { img_path = image_path; //Manual definitions.. SAD! - descriptor_types[7] = new DCTMedian(); + /*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[1] = new numColors();*/ descriptor_types[0] = new averageColor(); mutators[0] = new Grayscale(); @@ -47,7 +47,7 @@ void DescriptorManager::Init() { } fs::ifstream ifs; fs::ofstream ofs; - LoadFeatures(); + //LoadFeatures(); numImages = std::count_if( fs::directory_iterator(img_path), @@ -56,17 +56,18 @@ void DescriptorManager::Init() { //foreach image in directory (non-recursive) int filecount = 0; for (fs::directory_iterator itr(img_path); itr != fs::directory_iterator(); ++itr) { - cout << "\r" << "Progress: " << filecount*100/numImages << "% - (" << filecount << "/" << numImages << ")" << flush; 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; + //cout << "We already have feature for " << image_name << endl; continue; } //folder to store descriptors; create if not exists @@ -76,24 +77,20 @@ void DescriptorManager::Init() { //cout << "Starting with descriptor " << i << " on location " << descriptorBase.generic_string() << endl; fs::path featurePath(descriptorBase / itr->path().stem()); - bitset<64>* b1; + uint64_t b1; if (fs::exists(featurePath)) { //feature on disk, load it in ifs.open(featurePath, fs::fstream::binary); - unsigned long long n; - ifs.read(reinterpret_cast(&n), sizeof(n)); + ifs.read(reinterpret_cast(&b1), sizeof(b1)); ifs.close(); - b1 = new bitset<64>(n); } else { //not on disk, calculate it - b1 = new bitset<64>(descriptor_types[i]->feature(image)); - unsigned long long n = b1->to_ullong(); + b1 = descriptor_types[i]->feature(image); ofs.open(featurePath, fs::fstream::binary); - ofs.write(reinterpret_cast(&n), sizeof(n)); + ofs.write(reinterpret_cast(&b1), sizeof(b1)); ofs.close(); } - features[i][image_name] = *b1; - SaveFeatures(); - delete b1; + features[i][image_name] = b1; + //SaveFeatures(); } for (int i = 0; i < numMutations; i++) { //Check if we have somewhere to store the mutation @@ -114,7 +111,7 @@ void DescriptorManager::Init() { mutated = mutators[i]->getMutated(image); mutated->write(mutPath.generic_string()); auto duration = duration_cast(high_resolution_clock::now() - start); - cout << "Calculating mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl; + //cout << "Calculating mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl; } else { @@ -123,7 +120,7 @@ void DescriptorManager::Init() { mutated = new Image; mutated->read(mutPath.generic_string()); auto duration = duration_cast(high_resolution_clock::now() - start); - cout << "Loading mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl; + //cout << "Loading mutation " << mutators[i]->ToString() << " took " << duration.count() << "μs." << endl; } //now calculate all the features for the mutated images as well @@ -136,27 +133,23 @@ void DescriptorManager::Init() { fs::path featurePath(descriptorBase / itr->path().stem()); //cout << featurePath << endl; string image_name = itr->path().filename().generic_string(); - bitset<64>* b1 = NULL; + uint64_t b1; if (!fs::exists(featurePath)) { //feature not yet calculated //descriptor_types[i]->distance(b1, b1); //descriptors[i][image_name] = b1; - b1 = new bitset<64>(descriptor_types[j]->feature(mutated)); - unsigned long long n = b1->to_ullong(); + 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(&n), sizeof(n)); + ofs.write(reinterpret_cast(&b1), sizeof(b1)); ofs.close(); } else { ifs.open(featurePath, fs::fstream::binary); - unsigned long long n; - ifs.read(reinterpret_cast(&n), sizeof(n)); + ifs.read(reinterpret_cast(&b1), sizeof(b1)); ifs.close(); - b1 = new bitset<64>(n); } - mutatedFeatures[j][i][image_name] = *b1; - if(b1) delete b1; + mutatedFeatures[j][i][image_name] = b1; } delete mutated; } @@ -211,7 +204,7 @@ void DescriptorManager::LoadFeatures() { //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) { - bitset<64> target; + uint64_t target; if (features[method].count(image_name) && initialized) { //if we have image target = features[method][image_name]; } @@ -219,7 +212,7 @@ void DescriptorManager::rankImages(string image_name, int method) { return; } distances.clear(); //start fresh - for (map>::iterator it = features[method].begin(); it != features[method].end(); ++it) { + for (map::iterator it = features[method].begin(); it != features[method].end(); ++it) { unsigned long long d = descriptor_types[method]->distance(target, it->second); pair p(d, it->first); distances.insert(p); //pairs are compared by their first element.. @@ -230,7 +223,7 @@ void DescriptorManager::rankImages(string image_name, int method) { //It inserts these mutated descriptors in a sorted way //Assumes the distances array is filled with the normal distances multiset> DescriptorManager::rankMutations(string image_name, int method) { - bitset<64> target; + uint64_t target; if (features[method].count(image_name) && initialized) { //this should always be the case target = features[method][image_name]; } @@ -238,7 +231,7 @@ multiset> DescriptorManager::rankMutations(stri //cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl; multiset> local_distance = distances; //get our local copy of the distances. for (int i = 0; i < numMutations; i++) { - bitset<64> b1 = mutatedFeatures[method][i][image_name]; + uint64_t b1 = mutatedFeatures[method][i][image_name]; unsigned long long d = descriptor_types[method]->distance(target, b1); pair p(d, mutators[i]->ToString()); local_distance.insert(p); @@ -265,7 +258,7 @@ multiset> DescriptorManager::rankMutations(stri void DescriptorManager::runExperiments() { fs::ofstream ofs; for (int i = 0; i < numDescriptors; i++) { - for (map>::iterator it = features[i].begin(); it != features[i].end(); ++it) { + for (map::iterator it = features[i].begin(); it != features[i].end(); ++it) { //cout << "=== " << descriptor_types[i]->ToString() << ": " << it->first << " ===" << endl; rankImages(it->first, i); multiset> local_distance = rankMutations(it->first, i); @@ -300,13 +293,13 @@ void DescriptorManager::runExperiments() { //assumes a < numDescriptors void DescriptorManager::outputMap(int a) { cout << "Outputting map " << descriptor_types[a]->ToString() << endl; - map> a_map = features[a]; + map a_map = features[a]; if (a_map.begin() == a_map.end()) { cout << "------------------------" << endl << "No data found" << endl << endl; } - for (map>::iterator it = a_map.begin(); it != a_map.end(); ++it) { - cout << it->first << " => " << it->second << " (" << it->second.to_ullong() << ")" << endl; + for (map::iterator it = a_map.begin(); it != a_map.end(); ++it) { + cout << it->first << " => " << it->second << " (" << bitset<64>(it->second) << ")" << endl; } } diff --git a/DescriptorManager.h b/DescriptorManager.h index df5390a..020061c 100644 --- a/DescriptorManager.h +++ b/DescriptorManager.h @@ -54,15 +54,15 @@ public: void outputMap(); void outputRank(string imagename, int method); private: - static const int numDescriptors = 8; + static const int numDescriptors = 1; static const int numMutations = 8; //TODO: automagically update this? bool initialized = false; int numImages; - map> features[numDescriptors]; //array of maps that will hold the descriptors - map> mutatedFeatures[numDescriptors][numMutations]; + 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]; diff --git a/main.cpp b/main.cpp index 4ee849c..ebd85ed 100644 --- a/main.cpp +++ b/main.cpp @@ -43,8 +43,10 @@ int main(int argc, char **argv) { //smanager->generatePreviews(imagename); //cout << "Intializing..." << endl; manager->Init(); + cout << endl; + manager->outputMap(); //cout << "done!" << endl; - manager->runExperiments(); +// manager->runExperiments(); return 0; }