bool AreImagesEqual( const ImageData& image1, const ImageData& image2, const double diff_tolerance) { const int num_channels = image1.GetNumChannels(); if (num_channels != image2.GetNumChannels()) { std::cout << "Images do not have the same number of channels: " << num_channels << " vs. " << image2.GetNumChannels() << std::endl; return false; } // If the given diff_tolerance is zero, use "epsilon" to account for possible // double precision numerical errors. double applied_diff_tolerance = diff_tolerance; if (diff_tolerance < std::numeric_limits<double>::epsilon()) { applied_diff_tolerance = std::numeric_limits<double>::epsilon(); } for (int channel_index = 0; channel_index < num_channels; ++channel_index) { if (!AreMatricesEqual( image1.GetChannelImage(channel_index), image2.GetChannelImage(channel_index), applied_diff_tolerance)) { return false; } } return true; }
int MatrixTest::DoesCreateValidEigenSystem( MATRIX* matrix ) { const float TOLERANCE = 2e-5; int eigenSystemState = EIGENSYSTEM_INVALID; int size = matrix->rows; int cols = matrix->cols; float *eigenValues = new float[ size ]; MATRIX *eigenVectors = MatrixAlloc( size, cols, MATRIX_REAL ); MATRIX *eigenSystem = MatrixEigenSystem( matrix, eigenValues, eigenVectors ); if (eigenSystem == NULL) { return EIGENSYSTEM_INVALID; } bool isInDecendingOrder = true; for ( int i=1; i<size; i++ ) { if ( fabs( eigenValues[ i-1 ] ) < fabs( eigenValues[i] ) ) { isInDecendingOrder = false; std::cerr << "DoesCreateValidEigenSystem::not in descending order: (" << eigenValues[ i-1 ] << ", " << eigenValues[i] << ")\n"; } } if ( !isInDecendingOrder ) { eigenSystemState = EIGENSYSTEM_NOT_DESCENDING; } MATRIX* eigenValuesMatrix = MatrixAlloc( size, size, MATRIX_REAL ); for ( int i=0; i<size; i++ ) { // index of the diagonal int index = i + i * size; eigenValuesMatrix->data[index] = eigenValues[i]; } MATRIX* xv = MatrixMultiply( matrix, eigenVectors, NULL ); MATRIX* vd = MatrixMultiply( eigenVectors, eigenValuesMatrix, NULL ); if ( isInDecendingOrder && AreMatricesEqual( xv, vd, TOLERANCE ) ) { eigenSystemState = EIGENSYSTEM_VALID; } delete []eigenValues; DeleteMatrix( eigenVectors ); DeleteMatrix( eigenValuesMatrix ); return eigenSystemState; }
void MatrixTest::TestMatrixSVDPseudoInverse() { float tolerance = 1e-5; std::cout << "\rMatrixTest::TestMatrixSVDPseudoInverse()\n"; // check the low-level routine first MATRIX *Ux = MatrixRead( (char*) ( SVD_U_MATRIX.c_str() ) ); MATRIX *Vx = MatrixRead( (char*) ( SVD_V_MATRIX.c_str() ) ); VECTOR *Sx = MatrixRead( (char*) ( SVD_S_VECTOR.c_str() ) ); CPPUNIT_ASSERT (Ux != NULL); CPPUNIT_ASSERT (Vx != NULL); CPPUNIT_ASSERT (Sx != NULL); MATRIX *U=MatrixCopy(mSquareMatrix,NULL); MATRIX *V=MatrixAlloc(U->cols, U->cols, MATRIX_REAL) ; VECTOR *S=VectorAlloc(U->cols, MATRIX_REAL) ; OpenSvdcmp( U, S, V ) ; CPPUNIT_ASSERT (U != NULL); CPPUNIT_ASSERT (V != NULL); CPPUNIT_ASSERT (S != NULL); CPPUNIT_ASSERT ( AreMatricesEqual( U, Ux, tolerance ) ); CPPUNIT_ASSERT ( AreMatricesEqual( V, Vx, tolerance ) ); CPPUNIT_ASSERT ( AreMatricesEqual( S, Sx, tolerance ) ); // now check MatrixSVDPseudoInverse, which uses sc_linalg_SV_decomp tolerance = 1e-5; MATRIX *actualInverse = MatrixSVDPseudoInverse( mNonSquareMatrix, NULL ); MATRIX *expectedInverse = MatrixRead( (char*) ( NON_SQUARE_MATRIX_PSEUDO_INVERSE.c_str() ) ); CPPUNIT_ASSERT( AreMatricesEqual( actualInverse, expectedInverse ) ); actualInverse = MatrixSVDPseudoInverse( mSquareMatrix, NULL ); expectedInverse = MatrixRead( (char*) ( SQUARE_MATRIX_PSEUDO_INVERSE.c_str() ) ); CPPUNIT_ASSERT ( AreMatricesEqual( actualInverse, expectedInverse, tolerance ) ); }
bool AreMatricesEqualCroppedBorder( const cv::Mat& mat1, const cv::Mat& mat2, const int crop_border_size, const double diff_tolerance) { CHECK_GE(crop_border_size, 0); const cv::Size size = mat1.size(); const cv::Rect region_of_interest( crop_border_size, // Left index of crop. crop_border_size, // Top index of crop. size.width - crop_border_size * 2, // Width of crop. size.height - crop_border_size * 2); // Height of crop. const cv::Mat cropped_mat1 = mat1(region_of_interest); const cv::Mat cropped_mat2 = mat2(region_of_interest); return AreMatricesEqual(cropped_mat1, cropped_mat2, diff_tolerance); }
bool MatrixTest::AreInversesEqual( MATRIX *matrix, const std::string inverseFile ) { // NJS: had to change the tolerance for the test case to pass with VXL float tolerance = 0.0022; MATRIX *expectedInverse = MatrixRead((char*)( inverseFile.c_str() )); MATRIX *actualInverse = MatrixInverse(matrix, NULL); CPPUNIT_ASSERT( expectedInverse != NULL ); CPPUNIT_ASSERT( actualInverse != NULL ); bool areEqual=AreMatricesEqual( expectedInverse, actualInverse, tolerance ); DeleteMatrix(expectedInverse); DeleteMatrix(actualInverse); return areEqual; }