Ask Your Question

Revision history [back]

Unusual behavior of Matx

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) <<" "<< cam_rot(2,2) <<" "<< cam_rot(2,3) <<"\n";
            cout << cam_rot(3,1) <<" "<< cam_rot(3,2) <<" "<< cam_rot(3,3) <<"\n";


            imageName = pathToImage + convertInt(i) + imageExt;
            cout <<  imageName << std::endl ;
            image = imread(imageName,0); // Read the file, greyscale

            if(! image.data )                      // Check for invalid input
            {
            cout <<  "Could not open or find the image" << std::endl ;
            break;
            }



           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;



            Mat cropedImage = image(Rect(Point2f(0,0),Point2f(640,480)));

            pointFileName = pathToPointFolder + "loc" + convertInt(i) + ".txt";
            myfile.open (pointFileName.c_str());





/*
            for(int l = 0;l < cropedImage.rows;l++){
            cout << woah(3,3)<<endl;

                for(int k = 0;k < cropedImage.cols;k++){
                Scalar intensity = cropedImage.at<uchar>(k, l)/8;

                if(intensity[0] != 0){
                    Point3f cloc, gloc;
                    cloc.z = FOCAL_LENGTH*abs(CAM_DIST_X)/intensity[0];
                    cloc.x = (k - Uo)*cloc.z/FOCAL_LENGTH;
                    cloc.y = (l - Vo)*cloc.z/FOCAL_LENGTH;

                    gloc.x = cam_trans.x + (cam_rot(1,1)*cloc.x + cam_rot(1,2)*cloc.y + cam_rot(1,3)*cloc.z);
                    gloc.y = cam_trans.y + (cam_rot(2,1)*cloc.x + cam_rot(2,2)*cloc.y + cam_rot(2,3)*cloc.z);
                    gloc.z = cam_trans.z + (cam_rot(3,1)*cloc.x + cam_rot(3,2)*cloc.y + cam_rot(3,3)*cloc.z);
                    //cout << cam_rot(3,3)<<endl;
                    myfile << cloc.x<< ","<< cloc.y<< ","<< cloc.z<<","<< gloc.x<< ","<< gloc.y<< ","<< gloc.z<< "\n";

                }
                }

            }
*/      

            myfile.close ();
        }       

        i++;
        int stop_s=clock();
        cout << "time: " << (stop_s-start_s)/double(CLOCKS_PER_SEC)*1000 << endl;

    }else{
     cout <<"No more data in vicon file"<<endl;
     break;         
     }
}

    return 0;
}