73 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD");
79 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD\n");
83 const int m = A->
rows;
88 SVD(A, &W, 0, &V,
false,
false,
false);
90 for (
int i = 0, offset = n - 1; i < n; i++, offset += n)
98 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSVD");
104 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSVD\n");
117 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSimple");
123 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSimple\n");
136 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSVD");
146 const int m = pInputMatrix->
rows;
147 const int n = pInputMatrix->
columns;
152 SVD(A, &W, &UT, &V,
false,
true,
false);
157 const int min =
MY_MIN(m, n);
160 float fThreshold = 0.0f;
162 for (i = 0; i < min; i++)
163 fThreshold += WT(i, i);
165 fThreshold *= 2 * FLT_EPSILON;
168 for (i = 0; i < min; i++)
169 if (WT(i, i) < fThreshold)
172 WT(i, i) = 1.0f / WT(i, i);
177 Multiply(&temp, &UT, pResultMatrix);
184 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSimple");
192 const int m = pInputMatrix->
rows;
193 const int n = pInputMatrix->
columns;
199 Invert(&ATA, &ATA_inverted);
200 Multiply(&ATA_inverted, &AT, pResultMatrix);
207 printf(
"error: input is not square matrix in LinearAlgebraCV::Invert");
213 printf(
"error: input and output matrix are not the same in LinearAlgebraCV::Invert");
217 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_32FC1, pInputMatrix->
data);
218 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
220 cvInvert(&inputMatrix, &resultMatrix);
227 printf(
"error: input and output matrix do not match LinearAlgebraCV::Transpose");
231 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_32FC1, pInputMatrix->
data);
232 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
234 cvTranspose(&inputMatrix, &resultMatrix);
242 const int columns = pMatrix->
columns;
243 const int rows = pMatrix->
rows;
245 CvMat covarianceMatrix = cvMat(columns, columns, CV_32FC1, pCovarianceMatrix->
data);
247 CvMat **ppInput =
new CvMat*[rows];
248 for (
int i = 0; i < rows; i++)
250 CvMat *vector = cvCreateMatHeader(1, columns, CV_32FC1);
251 cvInitMatHeader(vector, 1, columns, CV_32FC1, pMatrix->
data + i * columns);
255 CvMat *avg = cvCreateMat(1, columns, CV_32FC1);
257 #ifdef CV_COVAR_NORMAL
258 cvCalcCovarMatrix((
const CvArr **) ppInput, rows, &covarianceMatrix, avg, CV_COVAR_NORMAL);
260 cvCalcCovarMatrix((
const CvArr **) ppInput, &covarianceMatrix, avg);
270 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
276 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
287 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
289 cvGEMM(&matrixA, &matrixB, 1, 0, 1, &result_matrix, flags);
297 CvMat matrix = cvMat(pMatrix->
rows, pMatrix->
columns, CV_32FC1, pMatrix->
data);
298 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
300 if (bTransposeSecond)
301 cvGEMM(&matrix, &matrix, 1, 0, 1, &result_matrix, CV_GEMM_B_T);
303 cvGEMM(&matrix, &matrix, 1, 0, 1, &result_matrix, CV_GEMM_A_T);
308 const int columns = A->
columns;
309 const int rows = A->
rows;
313 printf(
"error: W should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, rows);
319 printf(
"error: U should have %i columns and %i rows for LinearAlgebra::SVD\n", rows, rows);
323 if (V && (V->
columns != columns || V->
rows != columns))
325 printf(
"error: V should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, columns);
334 if (bReturnUTransposed)
337 if (bReturnVTransposed)
340 CvMat matrixA = cvMat(rows, columns, CV_32FC1, A->
data);
341 CvMat matrixW = cvMat(rows, columns, CV_32FC1, W->
data);
345 CvMat matrixU = cvMat(rows, rows, CV_32FC1, U->
data);
346 CvMat matrixV = cvMat(columns, columns, CV_32FC1, V->
data);
348 cvSVD(&matrixA, &matrixW, &matrixU, &matrixV, flags);
352 CvMat matrixU = cvMat(rows, rows, CV_32FC1, U->
data);
354 cvSVD(&matrixA, &matrixW, &matrixU, 0, flags);
358 CvMat matrixV = cvMat(columns, columns, CV_32FC1, V->
data);
360 cvSVD(&matrixA, &matrixW, 0, &matrixV, flags);
364 cvSVD(&matrixA, &matrixW, 0, 0, flags);
370 if (nTargetDimension > pData->
columns)
372 printf(
"error: target dimension is greater than number of columns in training data matrix in LinearAlgebraCV::PCA\n");
376 const int samples = pData->
rows;
377 const int dimension = pData->
columns;
379 if (pTransformationMatrix->
columns != dimension || pTransformationMatrix->
rows != nTargetDimension ||
380 pTransformedData->
columns != samples || pTransformedData->
rows != nTargetDimension)
382 printf(
"error: input to LinearAlgebraCV::PCA does not match\n");
391 printf(
"subtracting mean from columns...\n");
394 printf(
"calculating covariance matrix...\n");
397 printf(
"calculating SVD on %ix%i matrix...\n", dimension, dimension);
400 printf(
"SVD calculated\n");
402 for (
int i = 0, offset = 0; i < nTargetDimension; i++)
404 for (
int j = 0; j < dimension; j++)
406 pTransformationMatrix->
data[offset] = eigenVectors.
data[i * dimension + j];
416 const int samples = pData->
rows;
417 const int dimension = pData->
columns;
419 if (pTransformationMatrix->
columns != dimension || pTransformationMatrix->
rows != dimension || pEigenValues->
columns != 1 || pEigenValues->
rows != dimension)
427 printf(
"subtracting mean from columns...\n");
430 printf(
"calculating covariance matrix...\n");
433 printf(
"calculating SVD on %ix%i matrix...\n", dimension, dimension);
436 printf(
"SVD calculated\n");
438 for (
int i = 0; i < dimension; i++)
439 pEigenValues->
data[i] = eigenValues.
data[i * dimension + i];
446 printf(
"error: not enough input point pairs for LinearAlgebraCV::DetermineAffineTransformation (must be at least 3)\n");
453 float *data = M.
data;
455 for (
int i = 0, offset = 0; i < nPoints; i++, offset += 12)
457 data[offset] = pSourcePoints[i].
x;
458 data[offset + 1] = pSourcePoints[i].
y;
459 data[offset + 2] = 1;
460 data[offset + 3] = 0;
461 data[offset + 4] = 0;
462 data[offset + 5] = 0;
464 data[offset + 6] = 0;
465 data[offset + 7] = 0;
466 data[offset + 8] = 0;
467 data[offset + 9] = pSourcePoints[i].
x;
468 data[offset + 10] = pSourcePoints[i].
y;
469 data[offset + 11] = 1;
471 const int index = 2 * i;
472 b.
data[index] = pTargetPoints[i].
x;
473 b.
data[index + 1] = pTargetPoints[i].
y;
492 printf(
"error: not enough input point pairs for LinearAlgebraCV::DetermineHomography (must be at least 4)\n");
501 double *data = M.
data;
503 for (
int i = 0, offset = 0; i < nPoints; i++, offset += 16)
505 data[offset] = pSourcePoints[i].
x;
506 data[offset + 1] = pSourcePoints[i].
y;
507 data[offset + 2] = 1;
508 data[offset + 3] = 0;
509 data[offset + 4] = 0;
510 data[offset + 5] = 0;
511 data[offset + 6] = -pSourcePoints[i].
x * pTargetPoints[i].
x;
512 data[offset + 7] = -pSourcePoints[i].
y * pTargetPoints[i].
x;
514 data[offset + 8] = 0;
515 data[offset + 9] = 0;
516 data[offset + 10] = 0;
517 data[offset + 11] = pSourcePoints[i].
x;
518 data[offset + 12] = pSourcePoints[i].
y;
519 data[offset + 13] = 1;
520 data[offset + 14] = -pSourcePoints[i].
x * pTargetPoints[i].
y;
521 data[offset + 15] = -pSourcePoints[i].
y * pTargetPoints[i].
y;
523 const int index = 2 * i;
524 b.
data[index] = pTargetPoints[i].
x;
525 b.
data[index + 1] = pTargetPoints[i].
y;
551 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD");
557 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD\n");
561 const int m = A->
rows;
566 SVD(A, &W, 0, &V,
false,
false,
false);
568 for (
int i = 0, offset = n - 1; i < n; i++, offset += n)
576 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSVD");
582 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSVD\n");
587 CvMat *AA = cvCreateMat(A->
rows, A->
columns, CV_64FC1);
588 CvMat *BB = cvCreateMat(b->
dimension, 1, CV_64FC1);
589 CvMat *XX = cvCreateMat(x->
dimension, 1, CV_64FC1);
594 AA->data.db[i] = A->
data[i];
597 BB->data.db[i] = b->
data[i];
599 cvSolve(AA, BB, XX, CV_SVD);
601 for (
int k = 0; k < 8; k++)
602 x->
data[k] = XX->data.db[k];
618 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSimple");
624 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSimple\n");
637 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSVD");
647 const int m = pInputMatrix->
rows;
648 const int n = pInputMatrix->
columns;
653 SVD(A, &W, &UT, &V,
false,
true,
false);
658 const int min =
MY_MIN(m, n);
661 double dThreshold = 0.0;
663 for(i = 0; i < min; i++)
664 dThreshold += WT(i, i);
666 dThreshold *= 2 * DBL_EPSILON;
669 for (i = 0; i < min; i++)
670 if (WT(i, i) < dThreshold)
673 WT(i, i) = 1.0 / WT(i, i);
678 Multiply(&temp, &UT, pResultMatrix);
685 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSimple");
693 const int m = pInputMatrix->
rows;
694 const int n = pInputMatrix->
columns;
700 Invert(&ATA, &ATA_inverted);
701 Multiply(&ATA_inverted, &AT, pResultMatrix);
708 printf(
"error: input is not square matrix in LinearAlgebraCV::Invert");
714 printf(
"error: input and output matrix are not the same in LinearAlgebraCV::Invert");
718 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_64FC1, pInputMatrix->
data);
719 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
721 cvInvert(&inputMatrix, &resultMatrix);
728 printf(
"error: input and output matrix do not match LinearAlgebraCV::Transpose");
732 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_64FC1, pInputMatrix->
data);
733 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
735 cvTranspose(&inputMatrix, &resultMatrix);
742 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
748 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
759 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
761 cvGEMM(&matrixA, &matrixB, 1, 0, 1, &result_matrix, flags);
766 const int columns = A->
columns;
767 const int rows = A->
rows;
771 printf(
"error: W should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, rows);
777 printf(
"error: U should have %i columns and %i rows for LinearAlgebra::SVD\n", rows, rows);
781 if (V && (V->
columns != columns || V->
rows != columns))
783 printf(
"error: V should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, columns);
792 if (bReturnUTransposed)
795 if (bReturnVTransposed)
798 CvMat matrixA = cvMat(rows, columns, CV_64FC1, A->
data);
799 CvMat matrixW = cvMat(rows, columns, CV_64FC1, W->
data);
803 CvMat matrixU = cvMat(rows, rows, CV_64FC1, U->
data);
804 CvMat matrixV = cvMat(columns, columns, CV_64FC1, V->
data);
806 cvSVD(&matrixA, &matrixW, &matrixU, &matrixV, flags);
810 CvMat matrixU = cvMat(rows, rows, CV_64FC1, U->
data);
812 cvSVD(&matrixA, &matrixW, &matrixU, 0, flags);
816 CvMat matrixV = cvMat(columns, columns, CV_64FC1, V->
data);
818 cvSVD(&matrixA, &matrixW, 0, &matrixV, flags);
822 cvSVD(&matrixA, &matrixW, 0, 0, flags);
void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U=0, CFloatMatrix *V=0, bool bAllowModifyA=false, bool bReturnUTransposed=false, bool bReturnVTransposed=false)
void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U=0, CFloatMatrix *V=0, bool bAllowModifyA=false, bool bReturnUTransposed=false, bool bReturnVTransposed=false)
bool DetermineAffineTransformation(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
Data structure for the representation of a 2D vector.
void SubtractMeanFromColumns(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void SolveLinearLeastSquaresSimple(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
Data structure for the representation of a matrix of values of the data type double.
void PCA(const CFloatMatrix *pData, CFloatMatrix *pTransformationMatrix, CFloatMatrix *pTransformedData, int nTargetDimension)
Data structure for the representation of a vector of values of the data type double.
void SolveLinearLeastSquaresSVD(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix)
bool DetermineHomography(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
void SelfProduct(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix, bool bTransposeSecond=false)
Data structure for the representation of a matrix of values of the data type float.
void Transpose(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
bool CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)
void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix)
void SolveLinearLeastSquaresHomogeneousSVD(const CFloatMatrix *A, CFloatVector *x)
bool Invert(const CByteImage *pInputImage, CByteImage *pOutputImage)
Calculates the inverted image of a CByteImage and writes the result to a CByteImage.
void CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)
Data structure for the representation of a vector of values of the data type float.
Data structure for the representation of a 3x3 matrix.
void Invert(const CFloatMatrix *A, const CFloatMatrix *pResultMatrix)
void Multiply(const CFloatMatrix *A, const CFloatMatrix *B, CFloatMatrix *pResultMatrix, bool bTransposeB=false)
void MulMatVec(const CFloatMatrix *pMatrix, const CFloatVector *pVector, CFloatVector *pResultVector)
void CalculateCovarianceMatrix(const CFloatMatrix *pMatrix, CFloatMatrix *pCovarianceMatrix)
void Transpose(const CFloatMatrix *A, const CFloatMatrix *pResultMatrix)