Ask Your Question
0

Unusual behavior of Matx

asked 2014-03-26 16:45:29 -0600

btak gravatar image

Hi!

I have this piece of code where before and after reading the intensity of an image, the last element(initially non zero, say 100) of a 3x3 matrix Matx33f becomes zero.

           Matx33f woah;
           woah(1,1) = 0.0;
           woah(1,2) = 0.0;
           woah(1,3) = 0.0;
           woah(2,1) = 0.0;
           woah(2,2) = 0.0;
           woah(2,3) = 0.0;
           woah(3,1) = 0.0;
           woah(3,2) = 10.0;
           woah(3,3) = 10.0;

           cout << "what" <<woah(3,2)<< " "<<woah(3,3)<<endl;
          Scalar intensity = image.at<uchar>(1, 1);
           cout << "this" <<woah(3,2)<< " "<<woah(3,3)<<endl;

and weirdly enough, the output is

what10 10
this10 0

This happens with the last element of Matx44f too! but not with Matx22f.

For completion, i'll copy the whole code below:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <ctime>

#define PIXEL_LENGTH 0.000006
#define FOCAL_LENGTH 611.739321
#define CAM_DIST_X -0.079115608 
#define CAM_DIST_Y -0.000558381
#define CAM_DIST_Z  0.000922792
#define Uo 343.228336
#define Vo 244.798311

using namespace cv;
using namespace std;

string convertInt(int number)
{
   stringstream ss;//create a stringstream
   ss << number;//add number to the stream
   return ss.str();//return a string with the contents of the stream
}

void eular2rotmatx(vector<float> & data, Matx33f & rot){
float alpha,beta,gamma;
alpha = data[5];
beta = data[6];
gamma = data[7];
rot(1,1) = cos(gamma)*cos(beta)*cos(alpha) - sin(gamma)*sin(alpha);
rot(1,2) = cos(gamma)*cos(beta)*sin(alpha) + sin(gamma)*cos(alpha);
rot(1,3) = - cos(gamma)*sin(beta);
rot(2,1) = - sin(gamma)*cos(beta)*cos(alpha) - cos(gamma)*sin(alpha);
rot(2,2) = - sin(gamma)*cos(beta)*sin(alpha) + cos(gamma)*cos(alpha);
rot(2,3) = sin(gamma)*sin(beta);
rot(3,1) = sin(beta)*cos(alpha);
rot(3,2) = sin(beta)*sin(alpha);
rot(3,3) = cos(beta);

}

bool camOrientation(string & data_line, Point3f & translation, Matx33f & rotation){
stringstream lineStream(data_line);
string cell;
vector<float> data;
while(getline(lineStream,cell,','))
    {
    data.push_back(::atof(cell.c_str()));
    }
//cout << data[1] << " " << data[2]<<" "<< data[1] + data[2]<< endl;
if(data[2] == data[3] &&  data[3] == data[4] && data[4] == 0){
cout <<"No position: Vicon not initialized"<<endl;
return false;
}else{
    translation.x = data[2];
    translation.y = data[3];
    translation.z = data[4];

    eular2rotmatx(data,rotation);       

    return true;
}

}

int main( int argc, char** argv )
{

ofstream myfile;
ifstream viconFile;
//myfile.open ("/home/bharat/opencv_trial/dataset/loc.txt");

string imageName,pathToImage,imageExt,pathToPointFolder,pointFileName, pathToViconFile, viconFileName;
pathToImage = "/home/bharat/opencv_trial/dataset/";
imageExt = ".png";
pathToPointFolder  = "/home/bharat/opencv_trial/dataset/Points/";

pathToViconFile = "/home/bharat/opencv_trial/dataset/";
viconFileName = pathToViconFile + "log1.csv";

viconFile.open(viconFileName.c_str());

Point3f cam_trans;
Matx33f cam_rot;

int i = 0;
Mat image;

string camLine;
getline(viconFile,camLine);//see off the first field line

while(i<22){
    if(getline(viconFile,camLine)){
        int start_s=clock();
        cout << camLine<<endl;
        if(camOrientation(camLine, cam_trans, cam_rot)){

            cout << cam_rot(1,1) <<" "<< cam_rot(1,2) <<" "<< cam_rot(1,3) <<"\n";
            cout << cam_rot(2,1 ...
(more)
edit retag flag offensive close merge delete

Comments

please also look at Rodrigues , you don't have to roll your own eular2rotmat()

berak gravatar imageberak ( 2014-03-27 02:28:38 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-03-27 02:21:10 -0600

Moster gravatar image

updated 2014-03-27 03:25:05 -0600

I dont know how long you have been programming, but since when do indices start with 1? I personally dont know any modern language that does that. So of course your matx33 object starts with (0,0) and ends with (2,2).

Matx33f woah;
woah(0,0) = 0.0;
woah(0,1) = 0.0;
woah(0,2) = 0.0;
woah(1,0) = 0.0;
woah(1,1) = 0.0;
woah(1,2) = 0.0;
woah(2,0) = 0.0;
woah(2,1) = 10.0;
woah(2,2) = 10.0;

cout << woah << endl;

Edit: Btw, one interesting thing to know. Only the Mat object has a range check as far as I know. If you use Matx or Mat_< type > you wont notice if you accidently access some random memory region.

edit flag offensive delete link more

Comments

1

@Moster By default in Fortran arrays are started from 1 (but you can specify other indexation order: http://www.obliquity.com/computer/fortran/array.html)

Daniil Osokin gravatar imageDaniil Osokin ( 2014-03-27 02:44:05 -0600 )edit

Thanks, good to know. I havent touched fortran yet.

Moster gravatar imageMoster ( 2014-03-27 03:20:08 -0600 )edit

In Matlab, indices start with 1.

xaffeine gravatar imagexaffeine ( 2014-03-27 14:29:37 -0600 )edit

But matlab is not a classical programming language to me. Its rather a scripting language that is also open to non-programmers, most likely a reason to start with 1 :)

Moster gravatar imageMoster ( 2014-03-27 15:20:10 -0600 )edit

Question Tools

Stats

Asked: 2014-03-26 16:45:29 -0600

Seen: 295 times

Last updated: Mar 27 '14