bool bayesian_nnetwork<T>::calculate(const math::vertex<T>& input, math::vertex<T>& mean, math::matrix<T>& covariance) { if(nnets.size() <= 0) return false; const unsigned int D = nnets[0]->output_size(); mean.resize(D); covariance.resize(D,D); mean.zero(); covariance.zero(); T ninv = T(1.0f/nnets.size()); T ninv2 = T(1.0f/(nnets.size() - 1)); if(nnets.size() <= D) covariance.identity(); // regularizer term for small datasize for(unsigned int i=0;i<nnets.size();i++){ nnets[i]->input() = input; nnets[i]->calculate(); math::vertex<T> out = nnets[i]->output(); mean += ninv*out; covariance += ninv2*out.outerproduct(); } covariance -= mean.outerproduct(); return true; }
/** Returns reflected version of a matrix */ const math::matrix<float> Convolution::reflection(const math::matrix<float> A) { int size = A.rowno(); math::matrix<float> C(size, size); int col = A.colno(); int row = A.rowno(); for (int x = 0; x < row; x++){ for (int y = 0; y < col; y++){ C((size - 1 - x), (size - 1 - y)) = A(x, y); } } return C; }
/** Sums all of the matrixes elements */ const float Convolution::sum(const math::matrix<float> A) { float sum = 0.0; int col = A.colno(); int row = A.rowno(); for(int x=0; x<row; x++){ for(int y=0; y<col; y++){ sum += A(x,y); } } return sum; }
double BayesianClassifier::calculate_recognition_error(const math::matrix<double> & x, const QVector<Distribution> & distributions, const math::matrix<double> & regretMatrix) { double R = 0.0; bool first = true; for (size_t i = 0; i < regretMatrix.RowNo(); ++i) { double tR = 0.0; for (int j = 0; j < distributions.size(); ++j) tR += regretMatrix(i, j)*g(x, distributions[j]); if (first) { R = tR; first = false; } if (R > tR) R = tR; } return -R; }
const int MorphDilate::morph(math::matrix<float> window, math::matrix<bool> se) { float min = PIXEL_VAL_MAX+1; int tempx,tempy; tempx = window.rowno(); tempy = window.colno(); for(int i=0;i<tempx;i++){ for(int j=0;j<tempy;j++){ if(se(i,j)==true){ if(window(i,j)<min){ min = window(i,j); } } } } return (int) min; }
void bind(bool force = false) const { if ((flags_ & TRANSFORM_BIND_DISABLED) && !force) return; if ((flags_ & TRANSFORM_CACHE) == 0) { transform_matrix_.set_identity(); transform_matrix_.set_scale(scale_); transform_matrix_ *= orientation_.to_matrix(); transform_matrix_.apply_translation(location_); flags_ |= TRANSFORM_CACHE; } }
const int MorphErode::morph(math::matrix<float> window, math::matrix<bool> se) { float max=0.0; int row = se.rowno(); int col = se.colno(); for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { if(se(i,j)==true && window(i,j)>max) { max = window(i,j); } } } return max; }
/** Joins to matrices by multiplying the A[i,j] with B[i,j]. * Warning! Both Matrices must be squares with the same size! */ const math::matrix<float> Convolution::join(math::matrix<float> A, math::matrix<float> B) { int size = A.rowno(); math::matrix<float> C(size, size); for(int x=0; x<size; x++){ for(int y=0; y<size; y++) { C(x, y) = A(x, y) * B(x, y); } } return C; }
/** Joins to matrices by multiplying the A[i,j] with B[i,j]. * Warning! Both Matrices must be squares with the same size! */ const math::matrix<double> Convolution::join(math::matrix<double> A, math::matrix<double> B) { int size = A.RowNo(); math::matrix<double> C(size, size); for(int i = 0; i < size; i++) for(int j = 0; j < size; j++) { C(i, j) = A(i, j) * B(i, j); } return C; }
/** Sums all of the matrixes elements */ const double Convolution::sum(const math::matrix<double> A) { double sum = 0.0; int size = A.RowNo(); for(int i = 0; i < size; i++) for(int j = 0; j < size; j++) { sum += A(i, j); } return sum; }
const int MorphDilate::morph(math::matrix<double> window, math::matrix<bool> se) { double min = PIXEL_VAL_MAX+1; int size = window.ColNo(); for (int i=0;i<size;i++){ for (int j=0;j<size;j++){ if (se(i,j)) { if (window(i,j)<min) { min=window(i,j); } } } } return min; }