Visual Servoing Platform  version 3.6.1 under development (2025-03-06)
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
vpMatrix_pseudo_inverse_opencv.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Pseudo inverse computation.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #include "private/vpMatrix_pseudo_inverse.h"
37 
38 BEGIN_VISP_NAMESPACE
39 
40 #ifndef DOXYGEN_SHOULD_SKIP_THIS
41 
42 #if defined(VISP_HAVE_OPENCV)
83 vpMatrix vpMatrix::pseudoInverseOpenCV(double svThreshold) const
84 {
85  unsigned int nrows = getRows();
86  unsigned int ncols = getCols();
87  int rank_out;
88  vpMatrix U, V, Ap;
89  vpColVector sv;
90 
91  U = *this;
92  U.svdOpenCV(sv, V);
93 
94  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
95 
96  return Ap;
97 }
98 
143 unsigned int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, double svThreshold) const
144 {
145  unsigned int nrows = getRows();
146  unsigned int ncols = getCols();
147  int rank_out;
148  vpMatrix U, V;
149  vpColVector sv;
150 
151  Ap.resize(ncols, nrows, false, false);
152 
153  U = *this;
154  U.svdOpenCV(sv, V);
155 
156  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
157 
158  return static_cast<unsigned int>(rank_out);
159 }
211 unsigned int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold) const
212 {
213  unsigned int nrows = getRows();
214  unsigned int ncols = getCols();
215  int rank_out;
216  vpMatrix U, V;
217 
218  Ap.resize(ncols, nrows, false, false);
219 
220  U = *this;
221  U.svdOpenCV(sv, V);
222 
223  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, nullptr, nullptr, nullptr);
224 
225  return static_cast<unsigned int>(rank_out);
226 }
227 
338 unsigned int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, double svThreshold, vpMatrix &imA,
339  vpMatrix &imAt, vpMatrix &kerAt) const
340 {
341  unsigned int nrows = getRows();
342  unsigned int ncols = getCols();
343  int rank_out;
344  vpMatrix U, V;
345 
346  if (nrows < ncols) {
347  U.resize(ncols, ncols, true);
348  U.insert(*this, 0, 0);
349  }
350  else {
351  U = *this;
352  }
353 
354  U.svdOpenCV(sv, V);
355 
356  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, nullptr, &imA, &imAt, &kerAt);
357 
358  return static_cast<unsigned int>(rank_out);
359 }
360 
401 vpMatrix vpMatrix::pseudoInverseOpenCV(int rank_in) const
402 {
403  unsigned int nrows = getRows();
404  unsigned int ncols = getCols();
405  int rank_out;
406  double svThreshold = 1e-26;
407  vpMatrix U, V, Ap;
408  vpColVector sv;
409 
410  U = *this;
411  U.svdOpenCV(sv, V);
412 
413  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
414 
415  return Ap;
416 }
417 
468 int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, int rank_in) const
469 {
470  unsigned int nrows = getRows();
471  unsigned int ncols = getCols();
472  int rank_out;
473  double svThreshold = 1e-26;
474  vpMatrix U, V;
475  vpColVector sv;
476 
477  Ap.resize(ncols, nrows, false, false);
478 
479  U = *this;
480  U.svdOpenCV(sv, V);
481 
482  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
483 
484  return rank_out;
485 }
486 
544 int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in) const
545 {
546  unsigned int nrows = getRows();
547  unsigned int ncols = getCols();
548  int rank_out;
549  double svThreshold = 1e-26;
550  vpMatrix U, V;
551 
552  Ap.resize(ncols, nrows, false, false);
553 
554  U = *this;
555  U.svdOpenCV(sv, V);
556 
557  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, nullptr, nullptr, nullptr);
558 
559  return rank_out;
560 }
561 
677 int vpMatrix::pseudoInverseOpenCV(vpMatrix &Ap, vpColVector &sv, int rank_in, vpMatrix &imA, vpMatrix &imAt,
678  vpMatrix &kerAt) const
679 {
680  unsigned int nrows = getRows();
681  unsigned int ncols = getCols();
682  int rank_out;
683  double svThreshold = 1e-26;
684  vpMatrix U, V;
685 
686  if (nrows < ncols) {
687  U.resize(ncols, ncols, true);
688  U.insert(*this, 0, 0);
689  }
690  else {
691  U = *this;
692  }
693 
694  U.svdOpenCV(sv, V);
695 
696  compute_pseudo_inverse(U, sv, V, nrows, ncols, svThreshold, Ap, rank_out, &rank_in, &imA, &imAt, &kerAt);
697 
698  return rank_out;
699 }
700 #endif
701 
702 #endif // #ifndef DOXYGEN_SHOULD_SKIP_THIS
703 
704 END_VISP_NAMESPACE
unsigned int getCols() const
Definition: vpArray2D.h:417
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:442
unsigned int getRows() const
Definition: vpArray2D.h:427
Implementation of column vector and the associated operations.
Definition: vpColVector.h:191
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:169
vpMatrix pseudoInverseOpenCV(double svThreshold=1e-6) const
void svdOpenCV(vpColVector &w, vpMatrix &V)
void insert(const vpMatrix &A, unsigned int r, unsigned int c)
Definition: vpMatrix.cpp:1139