Ask Your Question
0

Mat Image data access problem

asked 2013-03-14 02:39:00 -0500

Hello everyone,

First please forgive my english, i'm not native so i may write in a strange manner :S

I recently had to work with openCV for a research project, so i'm quite new and have quite strange problems with it.

My current problem is when I'm trying to use the at function of the Mat class:

Mat cameraTransform(4,4,CV_32F); // I filled this matrix
Mat homogeneousVector1 = (Mat_<double>(4,1)<< projection1->x, projection1->y, projection1->z, 1.0);
Mat coord4DInCurrentFrame1(4,1,CV_32FC1);
 double sum = 0;

            for(int i = 0; i < cameraTransform.rows; i++)
            {
                for(int k = 0; k<homogeneousVector1.cols; k ++)
                {
                    for(int j = 0; j< cameraTransform.cols; j++)
                    {
                        sum += cameraTransform.at<double>(i,j)*homogeneousVector1.at<double>(j,k);
                    }
                    coord4DInCurrentFrame1.at<double>(i,k) = sum;
                    cout << coord4DInCurrentFrame1.at<double>(i,k) << endl; //(2)
                    sum = 0;
                }
            }
cout << coord4DInCurrentFrame1.at<double>(0,0) << ", " << coord4DInCurrentFrame1.at<double>(1,0) << ", " << coord4DInCurrentFrame1.at<double>(2,0) << ")"; //(1)

The values of coord4dInCurrentFrame1 at the point (0,0), (1,0), (2,0), evaluated outside of the for loop(1) are not the same as it is when I evaluated them in the for loop(2).

I've made this little program for test purpose, I know there is more simple way to do matrix multiplication but the error I got here seems very strange to me and I really want to understand where I am wrong in my program.

Thanks for your help

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-03-14 05:06:27 -0500

berak gravatar image

i see a problem with:

homogeneousVector1.at<double>(j,k);

and

coord4DInCurrentFrame1.at<double>(i,k);

both have just one column, if you iterate over k here[0-4], you're overflowing,

wellcome to undefined behaviour !

edit flag offensive delete link more

Comments

And it's declared as CV_32FC1, that correspondence to float, not double. For double use CV_64FC1 type.

Daniil Osokin gravatar imageDaniil Osokin ( 2013-03-14 08:40:33 -0500 )edit

oh yes thanks, i missed that, even ;)

berak gravatar imageberak ( 2013-03-14 10:46:57 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-03-14 02:39:00 -0500

Seen: 215 times

Last updated: Mar 14 '13