Adds mutations
This commit is contained in:
@@ -18,6 +18,8 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
|
||||
descriptor_types[2] = new median();
|
||||
descriptor_types[1] = new numColors();
|
||||
descriptor_types[0] = new averageColor();
|
||||
|
||||
mutators[0] = new Grayscale();
|
||||
}
|
||||
|
||||
//No need to call destructor.. yet?
|
||||
@@ -44,6 +46,7 @@ bool DescriptorManager::loadDescriptors() {
|
||||
|
||||
//foreach image
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
if (!itr->path().has_extension()) continue;
|
||||
fs::path featurePath(descriptorBase / itr->path().stem());
|
||||
//string image_name = featurePath.filename().generic_string();
|
||||
string image_name = itr->path().filename().generic_string();
|
||||
@@ -86,6 +89,80 @@ bool DescriptorManager::loadDescriptors() {
|
||||
return true;
|
||||
}
|
||||
|
||||
//Loads all images and makes and stores the mutations
|
||||
bool DescriptorManager::createMutations() {
|
||||
if (!fs::exists(img_path)) return false;
|
||||
fs::directory_iterator end_itr; //last file in dir
|
||||
map<string, Image*> images; //image cache
|
||||
fs::ifstream ifs;
|
||||
fs::ofstream ofs;
|
||||
|
||||
//foreach mutation
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
fs::path mutationBase(img_path / mutators[i]->ToPath());
|
||||
fs::path descriptorBase("meta" / descriptor_types[i]->ToPath());
|
||||
if (!fs::is_directory(mutationBase))
|
||||
if (fs::create_directory(mutationBase)) {}
|
||||
|
||||
//foreach image
|
||||
int z = 0;
|
||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||
fs::path mutPath(mutationBase / itr->path().filename().generic_string());
|
||||
if (z++ > 50) break; //for quick execution
|
||||
//if (fs::exists(mutPath)) continue; //already exists
|
||||
if (!itr->path().has_extension()) continue; //skip directories
|
||||
|
||||
Image* image = new Image(itr->path().string());
|
||||
Image* mutated = mutators[i]->getMutated(image);
|
||||
mutated->write(mutPath.generic_string());
|
||||
|
||||
//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();
|
||||
if (fs::exists(featurePath)) { //feature already calculated
|
||||
ifs.open(featurePath, fs::fstream::binary); //in binary mode
|
||||
unsigned long long n;
|
||||
ifs.read(reinterpret_cast<char*>(&n), sizeof(n));
|
||||
ifs.close();
|
||||
bitset<64> b1(n);
|
||||
//descriptors[j][image_name] = b1;
|
||||
}
|
||||
else { //feature not yet calculated
|
||||
Image* image; //only load image from disk if not loaded before
|
||||
if (images.count(image_name)) { //if image exists in map
|
||||
image = images[image_name];
|
||||
}
|
||||
else {
|
||||
image = new Image;
|
||||
image->read(itr->path().string());
|
||||
images[image_name] = image;
|
||||
}
|
||||
//cout << "Calculating " << image_name << endl;
|
||||
bitset<64> b1 = descriptor_types[i]->feature(image);
|
||||
//descriptor_types[i]->distance(b1, b1);
|
||||
//descriptors[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
|
||||
ofs.write(reinterpret_cast<const char*>(&n), sizeof(n));
|
||||
ofs.close();
|
||||
}
|
||||
}
|
||||
delete mutated;
|
||||
delete image;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//essentially insertion sort into a map
|
||||
void DescriptorManager::similarImages(string image_name, int method, int numImages) {
|
||||
bitset<64> target;
|
||||
|
||||
Reference in New Issue
Block a user