Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Svd犰狳给出了一些NaN值_C++_Armadillo_Svd - Fatal编程技术网

C++ Svd犰狳给出了一些NaN值

C++ Svd犰狳给出了一些NaN值,c++,armadillo,svd,C++,Armadillo,Svd,我发现Armadillo包可以在图像上执行SVD。我首先尝试使用OpenCV,速度非常慢。因此,现在我正在编写一个示例,使用这个新库执行SVD,但我没有得到与OpenCV相同的结果。经过一些研究,它似乎与拉帕克有关,OpenCV不再使用拉帕克,而是Armadillo使用拉帕克。因此,我在进行SVD计算后重建图像,以验证是否恢复了原始图像。使用OpenCV是可以的,差值接近0,但我不知道为什么犰狳的NaN数字是单数,所以我无法恢复图像 提前感谢您提供的任何帮助/建议 我的C++代码: int ma

我发现
Armadillo
包可以在图像上执行
SVD
。我首先尝试使用
OpenCV
,速度非常慢。因此,现在我正在编写一个示例,使用这个新库执行
SVD
,但我没有得到与
OpenCV
相同的结果。经过一些研究,它似乎与拉帕克有关,
OpenCV
不再使用拉帕克,而是
Armadillo
使用拉帕克。因此,我在进行
SVD
计算后重建图像,以验证是否恢复了原始图像。使用
OpenCV
是可以的,差值接近0,但我不知道为什么犰狳的
NaN
数字是单数,所以我无法恢复图像

提前感谢您提供的任何帮助/建议

<>我的C++代码:

int main()
{
    // Load the image
    cv::Mat img = cv::imread("path/to/the/image.png", 0);
    img.convertTo(img, CV_32FC1);

    // Convert cv::Mat to arma::fmat
    arma::fmat arma_img(reinterpret_cast<float*>(img.data), img.cols, img.rows);

    // Check if the image back from armadillo is okay
    cv::Mat opencv_img(arma_img.n_cols, arma_img.n_rows, CV_32FC1, arma_img.memptr());

    // ------ Perform SVD with OpenCV (2.5s)
    cv::SVD svvd;
    cv::Mat w1, U1, V1t;
    svvd.compute(opencv_img, w1, U1, V1t);

    cv::Mat W1 = cv::Mat::zeros(w1.rows, w1.rows, CV_32FC1);
    for (int i = 0; i<w1.rows; i++)
    {
        W1.at<float>(i, i) = w1.at<float>(i);
    }
    cv::Mat opencv_img_result = U1 * W1 * V1t;

    // ------ Perform SVD with Armadillo (0.05s)
    arma::fmat U2, V2;
    arma::fvec w2;
    arma::svd(U2, w2, V2, arma_img);

    arma::fmat W2 = arma::zeros<arma::fmat>(arma_img.n_rows, arma_img.n_cols);
    for (int i = 0; i < arma_img.n_cols; i++)
    {
        *(W2.memptr() + i * (1 + arma_img.n_rows)) = *(w2.memptr() + i);
    }
    arma::fmat arma_img_result = U2 * W2* V2.t();

    return 0;
}
intmain()
{
//加载图像
cv::Mat img=cv::imread(“path/to/the/image.png”,0);
img.convertTo(img,CV_32FC1);
//将cv::Mat转换为arma::fmat
arma::fmat arma_img(重新解释投射(img.data)、img.cols、img.rows);
//检查犰狳返回的图像是否正常
cv::Mat opencv_img(arma_img.n_cols,arma_img.n_rows,cv_32FC1,arma_img.memptr());
//----使用OpenCV执行SVD(2.5s)
cv:SVD-svvd;
cv::材料w1、U1、V1t;
svvd.compute(opencv_img,w1,U1,V1t);
cv::Mat W1=cv::Mat::Zero(W1.行,W1.行,cv_32FC1);

对于(int i=0;i而言,问题在于计算模式:

method参数是可选的;method是“dc”或“std”

矩阵

dc
模式工作不正常,但是
std
模式工作不正常。可能是
dc
模式下的Lapack库出错

"dc" indicates divide-and-conquer method (default setting)
"std" indicates standard method
the divide-and-conquer method provides slightly different results than the standard method, but is considerably faster for large