First two descriptors migrated

This commit is contained in:
Mark Hoekveen
2019-06-04 19:44:49 +02:00
parent 375626e6f6
commit 9bde653a2a
4 changed files with 76 additions and 81 deletions
+43 -43
View File
@@ -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";
}
};
};*/