gpt4 book ai didi

c++ - opencv矩阵伪逆失败

转载 作者:行者123 更新时间:2023-11-28 01:57:03 27 4
gpt4 key购买 nike

我找到了一个 pinv() example并成功运行。

但是我的版本失败了:

Mat1b B = Mat(400, 10, CV_32FC1);
for (r = 0; r < 20; r++)
{
for (c = 0; c < 20; c++)
{
B(n,0) = 1;
B(n,1) = r;
B(n,2) = c;
B(n,3) = r*r;
B(n,4) = c*r;
B(n,5) = c*c;
B(n,6) = r*r*r;
B(n,7) = c*r*r;
B(n,8) = c*c*r;
B(n,9) = c*c*c;
n = n + 1;
}
}
Mat1b Bpinv = Mat(10, 400, CV_32FC1);
invert(B, Bpinv, DECOMP_SVD);

invert 中的错误是:

Error message like this:OpenCV Error: Assertion failed (type == CV_32F || type == CV_64F) in cv::invert.

看起来 CV_32FCV_64F 之间发生了一些冲突,但我根本没有使用 CV_64F 格式。怎么会这样?

最佳答案

您正在使用 Mat1b (即 Mat_<uchar> ,类型为 CV_8UC1 ),但它应该是 Mat1f (即 Mat_<float> ,类型为 CV_32FC1 ),或 Mat1d (即 Mat_<double> ,类型为 CV_64FC1 )

然后你可以看到CV_8U既不是 CV_32F也不CV_64F .请注意,检查仅针对深度,而非 channel 数。

所以正确的代码使用Mat_<Tp>是(Mat1fMat1d ):

Mat1f B(400, 10);
for (r = 0; r < 20; r++)
{
for (c = 0; c < 20; c++)
{
B(n,0) = 1;
B(n,1) = r;
B(n,2) = c;
B(n,3) = r*r;
B(n,4) = c*r;
B(n,5) = c*c;
B(n,6) = r*r*r;
B(n,7) = c*r*r;
B(n,8) = c*c*r;
B(n,9) = c*c*c;
n = n + 1;
}
}

// You don't have to initialize 'OutputArray' for OpenCV functions
Mat1f Bpinv;
invert(B, Bpinv, DECOMP_SVD);

或者,使用 Mat :

Mat B(400, 10, CV_32FC1);
for (r = 0; r < 20; r++)
{
for (c = 0; c < 20; c++)
{
B.at<float>(n,0) = 1;
B.at<float>(n,1) = r;
B.at<float>(n,2) = c;
B.at<float>(n,3) = r*r;
B.at<float>(n,4) = c*r;
B.at<float>(n,5) = c*c;
B.at<float>(n,6) = r*r*r;
B.at<float>(n,7) = c*r*r;
B.at<float>(n,8) = c*c*r;
B.at<float>(n,9) = c*c*c;
n = n + 1;
}
}

// You don't have to initialize 'OutputArray' for OpenCV functions
Mat Bpinv;
invert(B, Bpinv, DECOMP_SVD);

关于c++ - opencv矩阵伪逆失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40800753/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com