问题描述
我在做一个复杂矩阵的逆时遇到麻烦.据我所知,复数矩阵只是一个两通道矩阵(CV_32FC2/CV_64FC2).
I have trouble in doing inverse of a complex matrix. As far as I know, complex matrix is simply a two-channel matrix (CV_32FC2 / CV_64FC2).
假设我有一个矩阵C:
Mat C(2, 2, CV_64FC2);
C.at<Vec2d>(0,0)[0] = 1;
C.at<Vec2d>(0,0)[1] = 1;
C.at<Vec2d>(0,1)[0] = 3;
C.at<Vec2d>(0,1)[1] = 4;
C.at<Vec2d>(1,0)[0] = 2;
C.at<Vec2d>(1,0)[1] = -1;
C.at<Vec2d>(1,1)[0] = 5;
C.at<Vec2d>(1,1)[1] = 2;
Mat InverseMat;
invert(C, InverseMat, DECOMP_SVD);
执行反转功能后,我不断收到此错误:
After I perform the invert function, I keep getting this error:
invert函数可以很好地处理灰度加载的图像(1通道),但是我很难对包含实部和虚部的复杂矩阵进行逆运算.
The invert function works well with a grayscale loaded image (1 channel), but I have hard time to do inverse on complex matrix which contains real and imaginary part.
有人可以告诉我如何解决复杂矩阵的逆问题吗?最好使用DECOMP_SVD方法,因为尝试单通道图像时由于使用DECOMP_LU或DECOMP_CHOLESKY方法无法获得理想的结果,可能是由于奇异矩阵的问题.谢谢.
Can someone please tell me how to solve the inverse problem of a complex matrix? Preferably using DECOMP_SVD method, as I can't get desired result using DECOMP_LU or DECOMP_CHOLESKY method when I tried with a single channel image, probably because of the matter of singular matrix. Thanks.
推荐答案
OpenCV不支持复杂矩阵的求逆.您必须以某种方式操纵复杂矩阵,以形成包含复杂矩阵的实部和虚部的实数矩阵. 此页面说明了该过程.
OpenCV does not support inversion of complex matrices. You have to manipulate the complex matrix in a way to form a real matrix containing the real and imaginary parts of the complex matrix. This page explains the process.
以下是使用上述过程对复杂矩阵求逆的代码:
Here is the code to perform inverse of a complex matrix using the above mentioned process:
//Perform inverse of complex matrix.
cv::Mat invComplex(const cv::Mat& m)
{
//Create matrix with twice the dimensions of original
cv::Mat twiceM(m.rows * 2, m.cols * 2, CV_MAKE_TYPE(m.type(), 1));
//Separate real & imaginary parts
std::vector<cv::Mat> components;
cv::split(m, components);
cv::Mat real = components[0], imag = components[1];
//Copy values in quadrants of large matrix
real.copyTo(twiceM({ 0, 0, m.cols, m.rows })); //top-left
real.copyTo(twiceM({ m.cols, m.rows, m.cols, m.rows })); //bottom-right
imag.copyTo(twiceM({ m.cols, 0, m.cols, m.rows })); //top-right
cv::Mat(-imag).copyTo(twiceM({ 0, m.rows, m.cols, m.rows })); //bottom-left
//Invert the large matrix
cv::Mat twiceInverse = twiceM.inv();
cv::Mat inverse(m.cols, m.rows, m.type());
//Copy back real & imaginary parts
twiceInverse({ 0, 0, inverse.cols, inverse.rows }).copyTo(real);
twiceInverse({ inverse.cols, 0, inverse.cols, inverse.rows }).copyTo(imag);
//Merge real & imaginary parts into complex inverse matrix
cv::merge(components, inverse);
return inverse;
}
这篇关于如何在OpenCV中对复杂矩阵求逆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!