File indexing completed on 2025-03-09 03:55:00

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 17-8-2016
0007  * Description : Some matrix utility functions including singular value
0008  *               decomposition, inverse, and pseudo-inverse.
0009  *
0010  * SPDX-FileCopyrightText: 2016      by Omar Amin <Omar dot moh dot amin at gmail dot com>
0011  * SPDX-FileCopyrightText: 2019      by Thanh Trung Dinh <dinhthanhtrung1996 at gmail dot com>
0012  * SPDX-FileCopyrightText: 2016-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0013  *
0014  * SPDX-License-Identifier: GPL-2.0-or-later
0015  *
0016  * ============================================================ */
0017 
0018 #ifndef DIGIKAM_MATRIX_OPERATIONS_H
0019 #define DIGIKAM_MATRIX_OPERATIONS_H
0020 
0021 // C++ includes
0022 
0023 #include <vector>
0024 #include <iostream>
0025 
0026 // Local includes
0027 
0028 #include "digikam_opencv.h"
0029 
0030 namespace Digikam
0031 {
0032 
0033 namespace MatrixOperations
0034 {
0035 
0036 std::vector<std::vector<float> > inv2(const std::vector<std::vector<float> >& mat);
0037 
0038 std::vector<std::vector<float> > pinv(const std::vector<std::vector<float> >& mat);
0039 
0040 void stdmattocvmat(const std::vector<std::vector<float> >& src, cv::Mat& dst);
0041 
0042 void cvmattostdmat(const cv::Mat& dst, std::vector<std::vector<float> >& src);
0043 
0044 template <typename T>
0045 inline T signdlib(const T& a, const T& b)
0046 {
0047     if (b < 0)
0048     {
0049         return -std::abs(a);
0050     }
0051     else
0052     {
0053         return std::abs(a);
0054     }
0055 }
0056 
0057 template <typename T>
0058 inline T pythag(const T& a, const T& b)
0059 {
0060     T absa = std::abs(a);
0061     T absb = std::abs(b);
0062 
0063     if (absa > absb)
0064     {
0065         T val = absb/absa;
0066         val  *= val;
0067 
0068         return (absa * std::sqrt(1.0F + val));
0069     }
0070     else
0071     {
0072         if (absb == 0.0)
0073         {
0074             return 0.0;
0075         }
0076         else
0077         {
0078             T val = absa/absb;
0079             val  *= val;
0080 
0081             return (absb * std::sqrt(1.0F + val));
0082         }
0083     }
0084 }
0085 
0086 void transpose(std::vector<std::vector<float> >& src,
0087                std::vector<std::vector<float> >& dst);
0088 
0089 float trace(const std::vector<std::vector<float> >& src);
0090 
0091 bool svd3(std::vector<std::vector<float> >& a,
0092           std::vector<float >& w,
0093           std::vector<std::vector<float> >& v,
0094           std::vector<float >& rv1);
0095 
0096 void svd(const std::vector<std::vector<float> >& m,
0097          std::vector<std::vector<float> >& u,
0098          std::vector<std::vector<float> >& w,
0099          std::vector<std::vector<float> >& v);
0100 
0101 float determinant(const std::vector<std::vector<float> >& u);
0102 
0103 } // namespace MatrixOperations
0104 
0105 } // namespace Digikam
0106 
0107 #endif // DIGIKAM_MATRIX_OPERATIONS_H