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