WIP Mutations ranking
This commit is contained in:
@@ -94,6 +94,7 @@ public:
|
||||
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);
|
||||
for (ssize_t j = 0; j < m; j++) {
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
//LTR scanning
|
||||
|
||||
+55
-27
@@ -20,6 +20,7 @@ DescriptorManager::DescriptorManager(const fs::path & image_path) {
|
||||
descriptor_types[0] = new averageColor();
|
||||
|
||||
mutators[0] = new Grayscale();
|
||||
mutators[1] = new Flip();
|
||||
}
|
||||
|
||||
//No need to call destructor.. yet?
|
||||
@@ -100,7 +101,6 @@ bool DescriptorManager::createMutations() {
|
||||
//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)) {}
|
||||
|
||||
@@ -121,35 +121,19 @@ bool DescriptorManager::createMutations() {
|
||||
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());
|
||||
//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;
|
||||
//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;
|
||||
}
|
||||
if (!fs::exists(featurePath)) { //feature not yet calculated
|
||||
//cout << "Calculating " << image_name << endl;
|
||||
bitset<64> b1 = descriptor_types[i]->feature(image);
|
||||
//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();
|
||||
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<const char*>(&n), sizeof(n));
|
||||
ofs.close();
|
||||
@@ -174,9 +158,10 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
|
||||
image->read(image_name); //assuming http url
|
||||
target = descriptor_types[method]->feature(image);
|
||||
} //TODO: Add support for uploaded images
|
||||
|
||||
//cout << "HEY?!" << endl;
|
||||
//first we need all distances to this image.. and insert them in a sorted way
|
||||
multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows..
|
||||
//multiset<pair<unsigned long long, string> > distances; //multiset is probably not needed, but who knows..
|
||||
//distances.clear(); //start fresh
|
||||
for (map<string, bitset<64>>::iterator it = descriptors[method].begin(); it != descriptors[method].end(); ++it) {
|
||||
//cout << "ho " << endl;
|
||||
unsigned long long d = descriptor_types[method]->distance(target, it->second);
|
||||
@@ -188,19 +173,62 @@ void DescriptorManager::similarImages(string image_name, int method, int numImag
|
||||
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end() && i < numImages; ++it) {
|
||||
//cout << "hey " << endl;
|
||||
if (true || it->second != image_name) {
|
||||
cout << it->second << endl;
|
||||
//cout << "(" << it->first << ", " << it->second << ")" << endl;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//inserts the descriptors of the mutated versions of the image.
|
||||
//Assumes the distances array is filled with the normal distances
|
||||
void DescriptorManager::insertMutations(string image_name, int method) {
|
||||
fs::ifstream ifs;
|
||||
bitset<64> target;
|
||||
if (descriptors[method].count(image_name)) { //if image exists in map
|
||||
target = descriptors[method][image_name];
|
||||
}
|
||||
cout << "Finding hits for " << image_name << " which has feature-number " << target.to_ullong() << endl;
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
fs::path descriptorBase("meta" / descriptor_types[method]->ToPath() / mutators[i]->ToPath());
|
||||
fs::path imagePath(image_name);
|
||||
fs::path featurePath(descriptorBase / imagePath.stem());
|
||||
cout << "Finding mutation for this image at: " << featurePath << endl;
|
||||
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();
|
||||
cout << "Number: " << n << endl;
|
||||
bitset<64> b1(n);
|
||||
unsigned long long d = descriptor_types[method]->distance(target, b1);
|
||||
pair<unsigned long long, string> p(d, mutators[i]->ToString());
|
||||
distances.insert(p);
|
||||
}
|
||||
else {
|
||||
cout << "data not set properly" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
cout << "..." << endl;
|
||||
for (int i = 0; i < numMutations; i++) {
|
||||
int j = 0;
|
||||
for (std::multiset<pair<unsigned long long, string>>::iterator it = distances.begin(); it != distances.end(); ++it) {
|
||||
//cout << "hey " << endl;
|
||||
if (it->second == mutators[i]->ToString()) {
|
||||
cout << "Hit. On the " << j << "th position: " << endl;
|
||||
cout << "(" << it->first << ", " << it->second << ")" << endl;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//outputs the gathered data to iostream
|
||||
//can be called directly or is called by its overload
|
||||
//assumes a < numDescriptors
|
||||
void DescriptorManager::outputMap(int a) {
|
||||
if (a != 7) return;
|
||||
if (a != 2) return;
|
||||
cout << "Outputting map " << descriptor_types[a]->ToString() << endl;
|
||||
map<string, bitset<64>> a_map = descriptors[a];
|
||||
if (a_map.begin() == a_map.end()) {
|
||||
|
||||
+3
-1
@@ -29,14 +29,16 @@ public:
|
||||
bool loadDescriptors();
|
||||
bool createMutations();
|
||||
void similarImages(string image_name, int method, int numImages);
|
||||
void insertMutations(string image_name, int method);
|
||||
void outputMap(int a);
|
||||
void outputMap();
|
||||
private:
|
||||
static const int numDescriptors = 8;
|
||||
static const int numMutations = 1;
|
||||
static const int numMutations = 2;
|
||||
//TODO: automagically update this?
|
||||
|
||||
map<string, bitset<64>> descriptors[numDescriptors]; //array of maps that will hold the descriptors
|
||||
multiset<pair<unsigned long long, string> > distances; //multiset that stores the distances
|
||||
fs::path img_path; //where the images are
|
||||
Descriptor* descriptor_types[numDescriptors];
|
||||
Mutation* mutators[numMutations];
|
||||
|
||||
+16
@@ -31,9 +31,25 @@ public:
|
||||
Image* getMutated(Image* img) {
|
||||
Image* mut = new Image(*img);
|
||||
mut->type(GrayscaleType);
|
||||
/*mut->quantizeColorSpace(GRAYColorspace);
|
||||
mut->quantizeColors(256);
|
||||
mut->quantize();
|
||||
mut->type(TrueColorType);*/
|
||||
return mut;
|
||||
}
|
||||
string ToString() {
|
||||
return "Grayscale";
|
||||
}
|
||||
};
|
||||
|
||||
class Flip : public Mutation {
|
||||
public:
|
||||
Image* getMutated(Image* img) {
|
||||
Image* mut = new Image(*img);
|
||||
mut->flip();
|
||||
return mut;
|
||||
}
|
||||
string ToString() {
|
||||
return "Flip";
|
||||
}
|
||||
};
|
||||
@@ -13,6 +13,7 @@ using namespace std;
|
||||
using namespace Magick;
|
||||
|
||||
//Methods:
|
||||
//6 DCT Pearson
|
||||
//5 threshold
|
||||
//4 pearson
|
||||
//3 sobel
|
||||
@@ -22,9 +23,9 @@ using namespace Magick;
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
string imagename = "im71.jpg";
|
||||
int method = 2;
|
||||
int count = 100;
|
||||
string imagename = "im124.jpg";
|
||||
int method = 6;
|
||||
int count = 1000;
|
||||
if (argc > 2) {
|
||||
method = atoi(argv[2]);
|
||||
imagename = argv[1];
|
||||
@@ -42,8 +43,10 @@ int main(int argc, char **argv) {
|
||||
//cout << "Loading descriptiors..";
|
||||
manager->loadDescriptors();
|
||||
manager->createMutations();
|
||||
//cout << "done?" << endl;
|
||||
manager->similarImages(imagename, method, count);
|
||||
manager->insertMutations(imagename, method);
|
||||
//cout << "done!" << endl << "Calculating distances.." << endl;
|
||||
//manager->similarImages(imagename, method, count);
|
||||
//cout << "done!" << endl << "Outputting map.." << endl;
|
||||
//manager->outputMap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user