68 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD");
74 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD\n");
78 const int m = A->
rows;
83 SVD(A, &W, 0, &V,
false,
false,
false);
93 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSVD");
99 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSVD\n");
112 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSimple");
118 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSimple\n");
131 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSVD");
141 const int m = pInputMatrix->
rows;
142 const int n = pInputMatrix->
columns;
147 SVD(A, &W, &UT, &V,
false,
true,
false);
152 const int min =
MY_MIN(m, n);
155 float fThreshold = 0.0f;
157 for (i = 0; i < min; i++)
158 fThreshold += WT(i, i);
160 fThreshold *= 2 * FLT_EPSILON;
163 for (i = 0; i < min; i++)
164 if (WT(i, i) < fThreshold)
167 WT(i, i) = 1.0f / WT(i, i);
172 Multiply(&temp, &UT, pResultMatrix);
179 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSimple");
187 const int m = pInputMatrix->
rows;
188 const int n = pInputMatrix->
columns;
194 Invert(&ATA, &ATA_inverted);
195 Multiply(&ATA_inverted, &AT, pResultMatrix);
202 printf(
"error: input is not square matrix in LinearAlgebraCV::Invert");
208 printf(
"error: input and output matrix are not the same in LinearAlgebraCV::Invert");
212 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_32FC1, pInputMatrix->
data);
213 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
215 cvInvert(&inputMatrix, &resultMatrix);
222 printf(
"error: input and output matrix do not match LinearAlgebraCV::Transpose");
226 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_32FC1, pInputMatrix->
data);
227 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
229 cvTranspose(&inputMatrix, &resultMatrix);
237 const int columns = pMatrix->
columns;
238 const int rows = pMatrix->
rows;
240 CvMat covarianceMatrix = cvMat(columns, columns, CV_32FC1, pCovarianceMatrix->
data);
242 CvMat **ppInput =
new CvMat*[rows];
243 for (
int i = 0; i < rows; i++)
245 CvMat *vector = cvCreateMatHeader(1, columns, CV_32FC1);
246 cvInitMatHeader(vector, 1, columns, CV_32FC1, pMatrix->
data + i * columns);
250 CvMat *avg = cvCreateMat(1, columns, CV_32FC1);
252 #ifdef CV_COVAR_NORMAL 253 cvCalcCovarMatrix((
const CvArr **) ppInput, rows, &covarianceMatrix, avg, CV_COVAR_NORMAL);
255 cvCalcCovarMatrix((
const CvArr **) ppInput, &covarianceMatrix, avg);
265 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
271 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
282 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
284 cvGEMM(&matrixA, &matrixB, 1, 0, 1, &result_matrix, flags);
293 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_32FC1, pResultMatrix->
data);
295 if (bTransposeSecond)
296 cvGEMM(&matrix, &matrix, 1, 0, 1, &result_matrix, CV_GEMM_B_T);
298 cvGEMM(&matrix, &matrix, 1, 0, 1, &result_matrix, CV_GEMM_A_T);
303 const int columns = A->
columns;
304 const int rows = A->
rows;
308 printf(
"error: W should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, rows);
314 printf(
"error: U should have %i columns and %i rows for LinearAlgebra::SVD\n", rows, rows);
318 if (V && (V->
columns != columns || V->
rows != columns))
320 printf(
"error: V should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, columns);
329 if (bReturnUTransposed)
332 if (bReturnVTransposed)
335 CvMat matrixA = cvMat(rows, columns, CV_32FC1, A->
data);
336 CvMat matrixW = cvMat(rows, columns, CV_32FC1, W->
data);
340 CvMat matrixU = cvMat(rows, rows, CV_32FC1, U->
data);
341 CvMat matrixV = cvMat(columns, columns, CV_32FC1, V->
data);
343 cvSVD(&matrixA, &matrixW, &matrixU, &matrixV, flags);
347 CvMat matrixU = cvMat(rows, rows, CV_32FC1, U->
data);
349 cvSVD(&matrixA, &matrixW, &matrixU, 0, flags);
353 CvMat matrixV = cvMat(columns, columns, CV_32FC1, V->
data);
355 cvSVD(&matrixA, &matrixW, 0, &matrixV, flags);
359 cvSVD(&matrixA, &matrixW, 0, 0, flags);
365 if (nTargetDimension > pData->
columns)
367 printf(
"error: target dimension is greater than number of columns in training data matrix in LinearAlgebraCV::PCA\n");
371 const int samples = pData->
rows;
372 const int dimension = pData->
columns;
374 if (pTransformationMatrix->
columns != dimension || pTransformationMatrix->
rows != nTargetDimension ||
375 pTransformedData->
columns != samples || pTransformedData->
rows != nTargetDimension)
377 printf(
"error: input to LinearAlgebraCV::PCA does not match\n");
386 printf(
"subtracting mean from columns...\n");
389 printf(
"calculating covariance matrix...\n");
392 printf(
"calculating SVD on %ix%i matrix...\n", dimension, dimension);
395 printf(
"SVD calculated\n");
397 for (
int i = 0,
offset = 0; i < nTargetDimension; i++)
399 for (
int j = 0; j < dimension; j++)
401 pTransformationMatrix->
data[
offset] = eigenVectors.
data[i * dimension + j];
411 const int samples = pData->
rows;
412 const int dimension = pData->
columns;
414 if (pTransformationMatrix->
columns != dimension || pTransformationMatrix->
rows != dimension || pEigenValues->
columns != 1 || pEigenValues->
rows != dimension)
422 printf(
"subtracting mean from columns...\n");
425 printf(
"calculating covariance matrix...\n");
428 printf(
"calculating SVD on %ix%i matrix...\n", dimension, dimension);
431 printf(
"SVD calculated\n");
433 for (
int i = 0; i < dimension; i++)
434 pEigenValues->
data[i] = eigenValues.
data[i * dimension + i];
441 printf(
"error: not enough input point pairs for LinearAlgebraCV::DetermineAffineTransformation (must be at least 3)\n");
450 for (
int i = 0,
offset = 0; i < nPoints; i++,
offset += 12)
452 data[
offset] = pSourcePoints[i].
x;
453 data[offset + 1] = pSourcePoints[i].
y;
454 data[offset + 2] = 1;
455 data[offset + 3] = 0;
456 data[offset + 4] = 0;
457 data[offset + 5] = 0;
459 data[offset + 6] = 0;
460 data[offset + 7] = 0;
461 data[offset + 8] = 0;
462 data[offset + 9] = pSourcePoints[i].
x;
463 data[offset + 10] = pSourcePoints[i].
y;
464 data[offset + 11] = 1;
466 const int index = 2 * i;
468 b.
data[index + 1] = pTargetPoints[i].
y;
487 printf(
"error: not enough input point pairs for LinearAlgebraCV::DetermineHomography (must be at least 4)\n");
498 for (
int i = 0,
offset = 0; i < nPoints; i++,
offset += 16)
500 data[
offset] = pSourcePoints[i].
x;
501 data[offset + 1] = pSourcePoints[i].
y;
502 data[offset + 2] = 1;
503 data[offset + 3] = 0;
504 data[offset + 4] = 0;
505 data[offset + 5] = 0;
506 data[offset + 6] = -pSourcePoints[i].
x * pTargetPoints[i].
x;
507 data[offset + 7] = -pSourcePoints[i].
y * pTargetPoints[i].
x;
509 data[offset + 8] = 0;
510 data[offset + 9] = 0;
511 data[offset + 10] = 0;
512 data[offset + 11] = pSourcePoints[i].
x;
513 data[offset + 12] = pSourcePoints[i].
y;
514 data[offset + 13] = 1;
515 data[offset + 14] = -pSourcePoints[i].
x * pTargetPoints[i].
y;
516 data[offset + 15] = -pSourcePoints[i].
y * pTargetPoints[i].
y;
518 const int index = 2 * i;
520 b.
data[index + 1] = pTargetPoints[i].
y;
546 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD");
552 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresHomogeneousSVD\n");
556 const int m = A->
rows;
561 SVD(A, &W, 0, &V,
false,
false,
false);
571 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSVD");
577 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSVD\n");
582 CvMat *AA = cvCreateMat(A->
rows, A->
columns, CV_64FC1);
583 CvMat *BB = cvCreateMat(b->
dimension, 1, CV_64FC1);
584 CvMat *XX = cvCreateMat(x->
dimension, 1, CV_64FC1);
589 AA->data.db[i] = A->
data[i];
592 BB->data.db[i] = b->
data[i];
594 cvSolve(AA, BB, XX, CV_SVD);
596 for (
int k = 0; k < 8; k++)
597 x->
data[k] = XX->data.db[k];
613 printf(
"error: A, b, x do not match LinearAlgebraCV::SolveLinearLeastSquaresSimple");
619 printf(
"error: equation system is underdetermined in LinearAlgebraCV::SolveLinearLeastSquaresSimple\n");
632 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSVD");
642 const int m = pInputMatrix->
rows;
643 const int n = pInputMatrix->
columns;
648 SVD(A, &W, &UT, &V,
false,
true,
false);
653 const int min =
MY_MIN(m, n);
656 double dThreshold = 0.0;
658 for(i = 0; i < min; i++)
659 dThreshold += WT(i, i);
661 dThreshold *= 2 * DBL_EPSILON;
664 for (i = 0; i < min; i++)
665 if (WT(i, i) < dThreshold)
668 WT(i, i) = 1.0 / WT(i, i);
673 Multiply(&temp, &UT, pResultMatrix);
680 printf(
"error: input and output matrix do not match LinearAlgebraCV::CalculatePseudoInverseSimple");
688 const int m = pInputMatrix->
rows;
689 const int n = pInputMatrix->
columns;
695 Invert(&ATA, &ATA_inverted);
696 Multiply(&ATA_inverted, &AT, pResultMatrix);
703 printf(
"error: input is not square matrix in LinearAlgebraCV::Invert");
709 printf(
"error: input and output matrix are not the same in LinearAlgebraCV::Invert");
713 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_64FC1, pInputMatrix->
data);
714 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
716 cvInvert(&inputMatrix, &resultMatrix);
723 printf(
"error: input and output matrix do not match LinearAlgebraCV::Transpose");
727 CvMat inputMatrix = cvMat(pInputMatrix->
rows, pInputMatrix->
columns, CV_64FC1, pInputMatrix->
data);
728 CvMat resultMatrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
730 cvTranspose(&inputMatrix, &resultMatrix);
737 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
743 printf(
"error: matrices A, B, and pResultMatrix do not satisfy requirements for LinearAlgebraCV::Multiply\n");
754 CvMat result_matrix = cvMat(pResultMatrix->
rows, pResultMatrix->
columns, CV_64FC1, pResultMatrix->
data);
756 cvGEMM(&matrixA, &matrixB, 1, 0, 1, &result_matrix, flags);
761 const int columns = A->
columns;
762 const int rows = A->
rows;
766 printf(
"error: W should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, rows);
772 printf(
"error: U should have %i columns and %i rows for LinearAlgebra::SVD\n", rows, rows);
776 if (V && (V->
columns != columns || V->
rows != columns))
778 printf(
"error: V should have %i columns and %i rows for LinearAlgebra::SVD\n", columns, columns);
787 if (bReturnUTransposed)
790 if (bReturnVTransposed)
793 CvMat matrixA = cvMat(rows, columns, CV_64FC1, A->
data);
794 CvMat matrixW = cvMat(rows, columns, CV_64FC1, W->
data);
798 CvMat matrixU = cvMat(rows, rows, CV_64FC1, U->
data);
799 CvMat matrixV = cvMat(columns, columns, CV_64FC1, V->
data);
801 cvSVD(&matrixA, &matrixW, &matrixU, &matrixV, flags);
805 CvMat matrixU = cvMat(rows, rows, CV_64FC1, U->
data);
807 cvSVD(&matrixA, &matrixW, &matrixU, 0, flags);
811 CvMat matrixV = cvMat(columns, columns, CV_64FC1, V->
data);
813 cvSVD(&matrixA, &matrixW, 0, &matrixV, flags);
817 cvSVD(&matrixA, &matrixW, 0, 0, flags);
void SolveLinearLeastSquaresSimple(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
void Transpose(const CFloatMatrix *A, const CFloatMatrix *pResultMatrix)
bool Invert(const CByteImage *pInputImage, CByteImage *pOutputImage)
Calculates the inverted image of a CByteImage and writes the result to a CByteImage.
bool CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)
void CalculateCovarianceMatrix(const CFloatMatrix *pMatrix, CFloatMatrix *pCovarianceMatrix)
Data structure for the representation of a vector of values of the data type double.
void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U=0, CFloatMatrix *V=0, bool bAllowModifyA=false, bool bReturnUTransposed=false, bool bReturnVTransposed=false)
void Invert(const CFloatMatrix *A, const CFloatMatrix *pResultMatrix)
Data structure for the representation of a matrix of values of the data type float.
GLsizei GLsizei GLenum GLenum const GLvoid * data
bool DetermineHomography(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
void SubtractMeanFromColumns(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void MulMatVec(const CFloatMatrix *pMatrix, const CFloatVector *pVector, CFloatVector *pResultVector)
void Multiply(const CFloatMatrix *A, const CFloatMatrix *B, CFloatMatrix *pResultMatrix, bool bTransposeB=false)
void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix)
Data structure for the representation of a vector of values of the data type float.
void SelfProduct(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix, bool bTransposeSecond=false)
void SVD(const CFloatMatrix *A, CFloatMatrix *W, CFloatMatrix *U=0, CFloatMatrix *V=0, bool bAllowModifyA=false, bool bReturnUTransposed=false, bool bReturnVTransposed=false)
Data structure for the representation of a 2D vector.
Data structure for the representation of a matrix of values of the data type double.
void Transpose(const CFloatMatrix *pMatrix, CFloatMatrix *pResultMatrix)
void SolveLinearLeastSquaresSVD(const CFloatMatrix *A, const CFloatVector *b, CFloatVector *x)
void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9)
bool DetermineAffineTransformation(const Vec2d *pSourcePoints, const Vec2d *pTargetPoints, int nPoints, Mat3d &A, bool bUseSVD=false)
Data structure for the representation of a 3x3 matrix.
void SolveLinearLeastSquaresHomogeneousSVD(const CFloatMatrix *A, CFloatVector *x)
void CalculatePseudoInverseSVD(const CFloatMatrix *pInputMatrix, CFloatMatrix *pOutputMatrix)
void PCA(const CFloatMatrix *pData, CFloatMatrix *pTransformationMatrix, CFloatMatrix *pTransformedData, int nTargetDimension)
void CalculatePseudoInverseSimple(const CFloatMatrix *pInputMatrix, CFloatMatrix *pResultMatrix)