0

I have 2 matrices as follows

    R = [1,0,0,0
         0,1,0,0
         0,0,1,0
         0,0,0,1]

and

   T = [1,0,0]

Can I make a 4X4 Matrix from the above 2 in this format?

    [    R | T
     0 0 0   1]

This is basically obtaining the transformation matrix from the rotation and translation. I am trying using for loops and combining them into one matrix. But is there an easy way or a function that can help me do this in a shorter way?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92
  • In your example R is 4x4 and T is 1x3 matrix. How you can do `R|T` (the dimensions don't agree)? Can you show the desired result for your example? – Alexey Sep 09 '13 at 13:33

1 Answers1

1

Here is a way to approach this. You can create an output matrix first and then operate on a rectangular sub-regions of the output (ROI – “Region of Interest”):

  1. Allocate a matrix that will keep the result. Fill in the matrix with desired initial values (optional). Make sure the matrix has the correct dimension and data type. For example:

    // create output matrix
    // rows and cols specify the disired size for the output matrix
    // CV_32F is data type for matrix elements
    Mat out(rows, cols, CV_32F, Scalar(0));   
    
  2. Set regions of interest (ROI) in your output matrix to desired sub-matrices. For example

    // your input matrices
    Mat R, T; 
    
    // set ROI for R
    cv::Rect rect_R(0, 0, R.rows, R.cols);
    cv::Mat out_R = out(rect_R);
    // this assignment does not copy data
    // out and out_R now share data
    
    // assign out_R to R
    out_R = R;
    
    // similarly you can set another area of out to matrix T, etc.
    
  3. out is set. you are done.

Alexey
  • 5,898
  • 9
  • 44
  • 81
  • Thank you for your response, but for some reason, the copy is not happening. I faced the same with many methods. The value of Z seems to be all 0's, instead of the format am expecting. Can you please help me here? – Lakshmi Narayanan Sep 09 '13 at 15:27
  • what's Z? What do you mean copy is not happening? Where do you have problems exactly? – Alexey Sep 09 '13 at 15:28
  • Did you create R and T matrices first? – Alexey Sep 09 '13 at 15:30
  • What is the type of R and T matrices (32 bit float - CV_32F, unsigned char - CV_8U, or signed 32 bit integer CV_32S, etc)? output matrix should be created with the same type. – Alexey Sep 09 '13 at 15:36
  • every matrix is of type 64_CS1. My apologies, the error was elsewhere. The method is working fine. Thank you :). – Lakshmi Narayanan Sep 09 '13 at 15:39