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[2] = new median();
|
||||||
descriptor_types[1] = new numColors();
|
descriptor_types[1] = new numColors();
|
||||||
descriptor_types[0] = new averageColor();
|
descriptor_types[0] = new averageColor();
|
||||||
|
|
||||||
|
mutators[0] = new Grayscale();
|
||||||
}
|
}
|
||||||
|
|
||||||
//No need to call destructor.. yet?
|
//No need to call destructor.. yet?
|
||||||
@@ -44,6 +46,7 @@ bool DescriptorManager::loadDescriptors() {
|
|||||||
|
|
||||||
//foreach image
|
//foreach image
|
||||||
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
for (fs::directory_iterator itr(img_path); itr != end_itr; ++itr) {
|
||||||
|
if (!itr->path().has_extension()) continue;
|
||||||
fs::path featurePath(descriptorBase / itr->path().stem());
|
fs::path featurePath(descriptorBase / itr->path().stem());
|
||||||
//string image_name = featurePath.filename().generic_string();
|
//string image_name = featurePath.filename().generic_string();
|
||||||
string image_name = itr->path().filename().generic_string();
|
string image_name = itr->path().filename().generic_string();
|
||||||
@@ -86,6 +89,80 @@ bool DescriptorManager::loadDescriptors() {
|
|||||||
return true;
|
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
|
//essentially insertion sort into a map
|
||||||
void DescriptorManager::similarImages(string image_name, int method, int numImages) {
|
void DescriptorManager::similarImages(string image_name, int method, int numImages) {
|
||||||
bitset<64> target;
|
bitset<64> target;
|
||||||
|
|||||||
+7
-1
@@ -11,6 +11,7 @@
|
|||||||
#include <Magick++.h>
|
#include <Magick++.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include "Descriptor.h"
|
#include "Descriptor.h"
|
||||||
|
#include "Mutation.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Magick;
|
using namespace Magick;
|
||||||
@@ -26,13 +27,18 @@ public:
|
|||||||
DescriptorManager(const fs::path & image_path);
|
DescriptorManager(const fs::path & image_path);
|
||||||
~DescriptorManager();
|
~DescriptorManager();
|
||||||
bool loadDescriptors();
|
bool loadDescriptors();
|
||||||
|
bool createMutations();
|
||||||
void similarImages(string image_name, int method, int numImages);
|
void similarImages(string image_name, int method, int numImages);
|
||||||
void outputMap(int a);
|
void outputMap(int a);
|
||||||
void outputMap();
|
void outputMap();
|
||||||
private:
|
private:
|
||||||
static const int numDescriptors = 8; //TODO: automagically update this?
|
static const int numDescriptors = 8;
|
||||||
|
static const int numMutations = 1;
|
||||||
|
//TODO: automagically update this?
|
||||||
|
|
||||||
map<string, bitset<64>> descriptors[numDescriptors]; //array of maps that will hold the descriptors
|
map<string, bitset<64>> descriptors[numDescriptors]; //array of maps that will hold the descriptors
|
||||||
fs::path img_path; //where the images are
|
fs::path img_path; //where the images are
|
||||||
Descriptor* descriptor_types[numDescriptors];
|
Descriptor* descriptor_types[numDescriptors];
|
||||||
|
Mutation* mutators[numMutations];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+39
@@ -0,0 +1,39 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <string>
|
||||||
|
#include <boost/filesystem.hpp>
|
||||||
|
#include <Magick++.h>
|
||||||
|
|
||||||
|
#include <bitset> //in which we store our feature vectors
|
||||||
|
#include <algorithm> //for sorting
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
using namespace Magick;
|
||||||
|
|
||||||
|
namespace fs = boost::filesystem;
|
||||||
|
|
||||||
|
//Virtual base class
|
||||||
|
//Holds definitions for all common functions of descriptors,
|
||||||
|
//but should be overloaded by a child class
|
||||||
|
class Mutation {
|
||||||
|
public:
|
||||||
|
//virtual void mutate(Image* img) = 0;
|
||||||
|
virtual Image* getMutated(Image* img) = 0;
|
||||||
|
|
||||||
|
virtual string ToString() = 0;
|
||||||
|
virtual fs::path ToPath() {
|
||||||
|
return fs::path(this->ToString());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//Just the way dogs see
|
||||||
|
class Grayscale : public Mutation {
|
||||||
|
public:
|
||||||
|
Image* getMutated(Image* img) {
|
||||||
|
Image* mut = new Image(*img);
|
||||||
|
mut->type(GrayscaleType);
|
||||||
|
return mut;
|
||||||
|
}
|
||||||
|
string ToString() {
|
||||||
|
return "Grayscale";
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -161,6 +161,7 @@
|
|||||||
<ClInclude Include="Descriptor.h" />
|
<ClInclude Include="Descriptor.h" />
|
||||||
<ClInclude Include="Enumstrings.h" />
|
<ClInclude Include="Enumstrings.h" />
|
||||||
<ClInclude Include="DescriptorManager.h" />
|
<ClInclude Include="DescriptorManager.h" />
|
||||||
|
<ClInclude Include="Mutation.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="DescriptorManager.cpp" />
|
<ClCompile Include="DescriptorManager.cpp" />
|
||||||
|
|||||||
@@ -27,6 +27,9 @@
|
|||||||
<ClInclude Include="DescriptorManager.h">
|
<ClInclude Include="DescriptorManager.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="Mutation.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="main.cpp">
|
<ClCompile Include="main.cpp">
|
||||||
|
|||||||
@@ -41,8 +41,9 @@ int main(int argc, char **argv) {
|
|||||||
DescriptorManager* manager = new DescriptorManager("data");
|
DescriptorManager* manager = new DescriptorManager("data");
|
||||||
//cout << "Loading descriptiors..";
|
//cout << "Loading descriptiors..";
|
||||||
manager->loadDescriptors();
|
manager->loadDescriptors();
|
||||||
|
manager->createMutations();
|
||||||
//cout << "done!" << endl << "Calculating distances.." << endl;
|
//cout << "done!" << endl << "Calculating distances.." << endl;
|
||||||
manager->similarImages(imagename, method, count);
|
//manager->similarImages(imagename, method, count);
|
||||||
//cout << "done!" << endl << "Outputting map.." << endl;
|
//cout << "done!" << endl << "Outputting map.." << endl;
|
||||||
//manager->outputMap();
|
//manager->outputMap();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user