// Compute pairwise distances between the rows of X and the rows of Y. // Choose either Euclidean or great circle distance MatrixXd pairwise_distance(const MatrixXd &X, const MatrixXd &Y) { if (!dist_metric.compare("greatcirc")) { return (greatcirc_dist(X,Y)); } else { return (euclidean_dist(X,Y)); } }
// Compute pairwise distances between the rows of X and the rows of Y. // Choose either Euclidean or great circle distance Eigen::MatrixXd pairwise_dist(const Eigen::MatrixXd &X, const Eigen::MatrixXd &Y, const std::string &distm) { if (!distm.compare("greatcirc")) { return (greatcirc_dist(X,Y)); } else { return (euclidean_dist(X,Y)); } }
void operator() (const parallel::range_t& r) const { for (size_t i = r.begin(); i != r.end(); ++i) { float d = 0.0f; if (_pop[i]->fit().objs()[0] <= -10000) d=-1000; else { for (size_t j = 0; j < _pop.size(); ++j) d += euclidean_dist(_pop[i]->data(),_pop[j]->data()); d /= (float)_pop.size(); } int l = _pop[i]->fit().objs().size()-1; _pop[i]->fit().set_obj(l, d); } }
/**************************************************************************//** * @author Zachary Pierson, Hannah Aker, Steven Huerta * * @par Description: * directed_hausdorff - Box Reverse Hausdorff. Returns a list of the * directed hausorff distances. An ordered list with best matching distances * first. This is ORDERS of magnitueds FASTER than the original hausdorff * distance calculation. * * @param[in,out] B - list of points in the model image * @param[in,out] A - list of points in the target image * * @returns vector<double> list of sorted distances * *****************************************************************************/ vector<double> directed_hausdorff(vector<point> &B, vector<point>& A) { //clock_t t = clock(); double max = -1; double min = 9999; double dist; vector<double> distance; if(A.size() == 0) return distance; //find maximum of the smallest distances from B to A for(unsigned int i = 0; i < B.size(); i++) { for(unsigned int j = 0; j < A.size(); j++) { dist = euclidean_dist(B[i], A[j]); if(dist < min) { min = dist; } } //keep track of maximum if(min > max) { max = min; } //save the the smallest distance from B[i] to A distance.push_back(min); min = 9999; } // sort the distances sort(distance.begin(), distance.end()); cout << "Hausdorff Distance: " << distance.back() << endl; //printf("time to calculate hausdorff: %f\n", (double)(clock() - t)/CLOCKS_PER_SEC ); return distance; }
/** * Convert the features of images into histogram * @param features features of the image * @param code_book code book of the data sets * @return histogram of bovw */ Hist describe(arma::Mat<T> const &features, arma::Mat<T> const &code_book) const { arma::Mat<T> dist(features.n_cols, code_book.n_cols); for(arma::uword i = 0; i != features.n_cols; ++i){ dist.row(i) = euclidean_dist(features.col(i), code_book); } //dist.print("dist"); Hist hist = create_hist(dist.n_cols, armd::is_two_dim<Hist>::type()); for(arma::uword i = 0; i != dist.n_rows; ++i){ arma::uword min_idx; dist.row(i).min(min_idx); ++hist(min_idx); } //hist.print("\nhist"); return hist; }
/* Function: eigen ---------------- Internal function. Find the largest eigenvalue from the matrix. Parameters: a - square matrix to be calculated n - dimension of its column/row space Returns: the largest eigenvalue */ double eigenL(double **a, int n) { int parity = 0; // Parity of loop rounds double result = 0; double norm = 0; double **eigV = (double**)malloc(2 * sizeof(double*)); double *temp = (double*)malloc(n * sizeof(double)); if ((eigV == NULL) || temp == NULL) { fprintf(stderr, "Fatal Error: Program runs out of memory!\n"); getchar(); exit(1); } // Initialization for (int i = 0; i < 2; ++i) { eigV[i] = (double*)malloc(n * sizeof(double)); if (eigV[i] == NULL) { fprintf(stderr, "Fatal Error: Program runs out of memory!\n"); getchar(); exit(1); } for (int j = 0; j < n; ++j) { eigV[i][j] = i; } } int loop = 0; // Find approximate eigenvector while (euclidean_dist( eigV[(parity + 1) % 2], eigV[parity], n) > 1.0e-10) { for (int i = 0; i < n; ++i) { temp[i] = 0; for (int j = 0; j < n; ++j) { temp[i] += a[i][j] * eigV[(parity + 1) % 2][j]; } norm += pow(temp[i], 2); } norm = sqrt(norm); if (norm != 0) { // Normalization for (int i = 0; i < n; ++i) { eigV[parity][i] = temp[i] / norm; } ++parity; parity = parity % 2; } else { // This is a null space break; } } // Find eigenvalue int count = 0; for (int i = 0; i < n; ++i) { if (eigV[(parity + 1) % 2][i] != 0) { temp[i] = 0; for (int j = 0; j < n; ++j) { temp[i] += a[i][j] * eigV[(parity + 1) % 2][j]; } result += temp[i] / eigV[(parity + 1) % 2][i]; ++count; } } if (count != 0) { result = result / count; } clear2D(&eigV, 2); free(temp); return result; }