Ask Your Question
0

Mat.inv() yielding all zeroes in opencv

asked 2013-02-24 06:59:13 -0600

ritwik gravatar image

I have the following code :

    cv::Mat temp0 = R.t();
    cv::Mat temp1 = R * temp0;
    cv::Mat temp2(960, 960, CV_32FC1);
    temp2 = temp1.inv();

    cv::Size s = temp1.size();
    std::cout<<s.height<<" "<<s.width<<std::endl;

    std::cout<<cv::format(temp1, "numpy" ) << std::endl;
    std::cout<<cv::format(temp2, "numpy" ) << std::endl;

The Transpose works correctly, so does the matrix multiplication. Thus the Mat temp1 has a size of 960x960. However, when I do temp2 =temp1.inv(), I recieve all zeroes in temp2. I mean zeroes is all of the 960x960 cells. Also, R is of type CV_32FC1 only. So it is probably not a datatype issue. I cannot understand the issue here. I googled so much. Can you please help.

EDIT

I am copying below the gdb output for the Mat::inv() function. I am having a hard time figuring it all out, but if someone is more familiar with opencv, maybe it will be of help :)

Breakpoint 1, CreateShares::ConstructShares (this=0x80556d0, channel=..., k=2, n=4) at CreateShares.cpp:165
165     temp2 = temp1.inv();
(gdb) step

cv::Mat::operator= (this=0xbffff294, e=...) at /usr/include/opencv2/core/mat.hpp:1373
1373        e.op->assign(e, *this);
(gdb) 
1374        return *this;
(gdb) step
1375    }    
(gdb) step
cv::MatExpr::~MatExpr (this=0xbfffef64, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:1167
1167    class CV_EXPORTS MatExpr
(gdb) step
cv::Mat::~Mat (this=0xbfffefdc, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295     release();
(gdb) step
cv::Mat::release (this=0xbfffefdc) at /usr/include/opencv2/core/mat.hpp:381
381     if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383     data = datastart = dataend = datalimit = 0;
(gdb) step
384     size.p[0] = 0;
(gdb) step
385     refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefdc, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296     if( step.p != step.buf )
(gdb) step
298 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefa4, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295     release();
(gdb) step
cv::Mat::release (this=0xbfffefa4) at /usr/include/opencv2/core/mat.hpp:381
381     if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383     data = datastart = dataend = datalimit = 0;
(gdb) step
384     size.p[0] = 0;
(gdb) step
385     refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffefa4, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296     if( step.p != step.buf )
(gdb) step
298 }
(gdb) step
cv::Mat::~Mat (this=0xbfffef6c, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:295
295     release();
(gdb) step
cv::Mat::release (this=0xbfffef6c) at /usr/include/opencv2/core/mat.hpp:381
381     if( refcount && CV_XADD(refcount, -1) == 1 )
(gdb) step
383     data = datastart = dataend = datalimit = 0;
(gdb) step
384     size.p[0] = 0;
(gdb) step
385     refcount = 0;
(gdb) step
386 }
(gdb) step
cv::Mat::~Mat (this=0xbfffef6c, __in_chrg=<optimized out>) at /usr/include/opencv2/core/mat.hpp:296
296     if( step.p ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-02-24 09:20:29 -0600

SR gravatar image

Are you sure matrix temp1 is invertible? What is its determinant?

edit flag offensive delete link more

Comments

its not a determinant zero issue since I am generrating inverse on randomly generated matrices and the resulltant random matrix cannot have a zero determinant all the time !. I have used all three methods with the inverse function. In 2 the cells are all zero. and in one(DECOMP_SVD) I get all wierd values like , 7.5003686e-06, -5.3263466e-06, -8.0332615e-07, 1.0127796e-06, 4.732472e-06, -2.2615104e-06, 6.1906544e-06, 8.8100833e-06.

ritwik gravatar imageritwik ( 2013-02-25 12:00:13 -0600 )edit

Try to set the type of temp to CV_64FC1. There was an issue with inv() on float matrices. I do not remember if it was fixed in 2.4.3 already.

SR gravatar imageSR ( 2013-02-25 13:13:28 -0600 )edit

Thanks, I will try that. But as a side question, what is the best way to handle such different type requirements and compatibilities on Mat objects with different functions ? Is there a way to convert of typecast (say, Eg.) a CV_8UC1 Mat to a CV_64FC1 Mat ? or is converting each value using Mat.at<float>(i,j) in a loop my only way out ?

ritwik gravatar imageritwik ( 2013-02-25 22:30:08 -0600 )edit

mat8u.convertTo(dst, CV_64FC1); would do that. (Assuming dst is empty yet and therefore has not a type associated yet)

SR gravatar imageSR ( 2013-02-26 01:56:03 -0600 )edit

Question Tools

Stats

Asked: 2013-02-24 06:59:13 -0600

Seen: 730 times

Last updated: Feb 24 '13