You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
3.1 KiB
109 lines
3.1 KiB
11 years ago
|
#include "wavelet.hpp"
|
||
10 years ago
|
#include <wavelet.hpp>
|
||
|
#include <utilities.hpp>
|
||
|
#include <image_io.hpp>
|
||
11 years ago
|
|
||
|
#include <ostream>
|
||
|
#include <cmath>
|
||
|
|
||
10 years ago
|
static const int size = 512;
|
||
|
|
||
|
rgb_wavelet_coefficients::pre_fingerprint rgb_wavelet_coefficients::pre_calculate(av::frame const & frame) {
|
||
|
auto const image = to_raw_rgb_image(crop_to_square(frame), size, size);
|
||
|
rgb_wavelet_coefficients::pre_fingerprint ret;
|
||
|
|
||
|
// for every color
|
||
|
for(unsigned int color = 0; color < 3; ++color){
|
||
|
auto & vector = ret[color];
|
||
|
vector.assign(make_u(image.width() * image.height()), 0);
|
||
|
|
||
|
for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){
|
||
|
vector[n] = 2.0 * image.data[3*n + color] / double(255) - 1.0;
|
||
|
}
|
||
|
|
||
|
wvlt::wavelet_2D(vector.data(), make_u(image.width()), make_u(image.height()));
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
rgb_wavelet_coefficients rgb_wavelet_coefficients::calculate(av::frame const & frame){
|
||
|
auto const image = to_raw_rgb_image(crop_to_square(frame), size, size);
|
||
11 years ago
|
rgb_wavelet_coefficients ret;
|
||
|
|
||
10 years ago
|
std::vector<double> vector(make_u(image.width() * image.height()), 0);
|
||
11 years ago
|
|
||
|
// for every color
|
||
|
for(unsigned int color = 0; color < 3; ++color){
|
||
|
auto& coefficient_array = color == 0 ? ret.reds : (color == 1 ? ret.greens : ret.blues);
|
||
|
unsigned int array_index = 0;
|
||
|
|
||
|
for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){
|
||
|
vector[n] = 2.0 * image.data[3*n + color] / double(255) - 1.0;
|
||
|
}
|
||
|
|
||
|
wvlt::wavelet_2D(vector.data(), make_u(image.width()), make_u(image.height()));
|
||
|
|
||
|
auto copy = vector;
|
||
|
for(auto & x : copy) x = std::abs(x);
|
||
|
|
||
10 years ago
|
auto const n_coefficients = coefficient_array.size();
|
||
11 years ago
|
std::nth_element(copy.begin(), copy.begin() + n_coefficients, copy.end(), std::greater<double>());
|
||
10 years ago
|
auto const threshold = copy[n_coefficients-1];
|
||
11 years ago
|
|
||
|
for(unsigned int n = 0; n < vector.size(); ++n){
|
||
10 years ago
|
auto const x = vector[n];
|
||
11 years ago
|
if(std::abs(x) >= threshold) {
|
||
|
coefficient_array[array_index++] = std::make_pair(n, x);
|
||
|
}
|
||
|
if(array_index >= coefficient_array.size()) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
static double square(double x){
|
||
|
return x*x;
|
||
|
}
|
||
|
|
||
10 years ago
|
double rgb_wavelet_coefficients::distance_to(pre_fingerprint const & fingerprint) const {
|
||
11 years ago
|
double distance = 0;
|
||
|
|
||
|
for(unsigned int color = 0; color < 3; ++color){
|
||
10 years ago
|
auto const & coefficients = color == 0 ? reds : (color == 1 ? greens : blues);
|
||
11 years ago
|
|
||
10 years ago
|
for(auto&& p : coefficients){
|
||
|
auto const x = p.second;
|
||
|
auto const y = fingerprint[color][p.first];
|
||
|
distance += square(x - y) - square(y);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
return distance;
|
||
|
}
|
||
|
|
||
|
namespace std {
|
||
|
template <typename U, typename V>
|
||
|
ostream& operator<<(ostream& out, pair<U, V> const & p){
|
||
|
return out << '(' << p.first << ", " << p.second << ')';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
std::ostream& operator<<(std::ostream& out, rgb_wavelet_coefficients const & x){
|
||
|
out << "rgb_wavelet_coefficients" << std::endl;
|
||
|
|
||
|
for(int color = 0; color < 3; ++color){
|
||
10 years ago
|
auto const & coefficient_array = color == 0 ? x.reds : (color == 1 ? x.greens : x.blues);
|
||
11 years ago
|
|
||
|
out << '[' << coefficient_array[0];
|
||
10 years ago
|
for(unsigned int i = 1; i < coefficient_array.size(); ++i) {
|
||
|
out << ", " << coefficient_array[i];
|
||
|
}
|
||
11 years ago
|
out << ']' << std::endl;
|
||
|
}
|
||
|
return out;
|
||
|
}
|