Code-cleanup

Some refactoring was in order.
This commit is contained in:
Mark Hoekveen
2018-06-30 14:31:46 +02:00
parent 8b95040a2b
commit dba94c125e
+6 -49
View File
@@ -32,6 +32,8 @@ public:
return true; return true;
} }
//Helper functions
//TODO: Move these somewhere else. Maybe a friend-class?
double* CalculateDCT(Image* img) { double* CalculateDCT(Image* img) {
double pi = 3.14159265359; double pi = 3.14159265359;
img->type(GrayscaleType); img->type(GrayscaleType);
@@ -60,26 +62,21 @@ public:
for (ssize_t x = 0; x < m; x++) { for (ssize_t x = 0; x < m; x++) {
for (ssize_t y = 0; y < n; y++) { for (ssize_t y = 0; y < n; y++) {
int n = (x*m) + y; int n = (x*m) + y;
double pixel = ((double)p_s[0] / QuantumRange); //rescaled from 0 to 1 double pixel = ((double)p_s[0] / QuantumRange); //rescaled to [0..1]
//cout << "pixel: " << pixel << endl;
sum += pixel * std::cos((pi / N)*(n + 0.5)*k); sum += pixel * std::cos((pi / N)*(n + 0.5)*k);
p_s = p_s + chan; p_s = p_s + chan;
} }
} }
q[0] = sum; q[0] = sum;
/*if (p[0] != 0)
retval |= bitset<64>(1); //XOR
retval <<= 1;*/
q = q++; q = q++;
p = p + chan; p = p + chan;
} }
} }
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
c[0] = converted[i] * 255.0; c[0] = converted[i] * 255.0;
c++; c++;
} }
newimg->write("Out.png"); //newimg->write("Out.png"); //for debugging purposes
return converted; return converted;
} }
}; };
@@ -340,55 +337,17 @@ public:
class DCTPearson: public Descriptor { class DCTPearson: public Descriptor {
public: public:
bitset<64> feature(Image* img) { bitset<64> feature(Image* img) {
img->type(GrayscaleType);
img->filterType(LanczosFilter); //fast!
img->resize(Geometry(8, 8)); //64 pixels
ssize_t n = 8;
ssize_t m = 8;
Pixels view(*img);
int numpixels = (int)(n*m);
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);
int N = n*m;
/*Image* converted = new Image(Geometry(n, m), Color("white"));
Pixels conview(*converted);
const Quantum *q = conview.getConst(0, 0, n, m); //entire converted image*/
double* converted = CalculateDCT(img); double* converted = CalculateDCT(img);
/*double* q = converted;
for (ssize_t j = 0; j < m; j++) {
for (ssize_t i = 0; i < n; i++) {
double sum = 0;
int k = (j*m) + i;
const Quantum *p_s = view.getConst(0, 0, n, m); //entire image
for (ssize_t x = 0; x < m; x++) {
for (ssize_t y = 0; y < n; y++) {
int n = (x*m) + y;
double pixel = ((double)p_s[0] / QuantumRange); //rescaled from 0 to 1
//cout << "pixel: " << pixel << endl;
sum += pixel * std::cos((pi/N)*(n+0.5)*k);
p_s = p_s + chan;
}
}
q[0] = sum;
q = q++;
p = p + chan;
}
//cout << endl;
}*/
double prev = 0; double prev = 0;
bitset<64> 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 |= bitset<64>(1); //XOR
//cout << "1";
} }
//else cout << "0";
retval <<= 1; retval <<= 1;
prev = converted[i]; prev = converted[i];
} }
//cout << "----" << endl << endl;
return retval; return retval;
} }
unsigned long long distance(bitset<64> a, bitset<64> b) { unsigned long long distance(bitset<64> a, bitset<64> b) {
@@ -399,8 +358,6 @@ public:
return biterror; return biterror;
}; };
string ToString() { string ToString() {
return "Discrete Cosine Transform"; return "Discrete Cosine Transform - Pearson Code";
} }
private:
double pi = 3.14159265359;
}; };