How to determine the data type of a CvMat

10,676

Solution 1

The type is a variable, NOT a function:

CvMat* tmp_ptr = cvCreateMat(t_height,t_width,CV_8U);
std::cout << "type = " << tmp_ptr->type << std::endl;

EDIT:

As for the unusual value of type being printed, according to this answer, this variable stores more than the data type.

So the appropriate way of checking cvMat data type is using the macro CV_MAT_TYPE():

CvMat *tmp2 = cvCreateMat(3,1, CV_32FC1);
std::cout << "tmp2 type = " << tmp2->type << " and CV_32FC1 = " << CV_32FC1 << " and " << (CV_MAT_TYPE(tmp2->type) == CV_32FC1) << std::endl;

The naming convention of the data type is:

CV_<bit_depth>(S|U|F)C<number_of_channels>

S = Signed integer
U = Unsigned integer
F = Float 

E.g.: CV_8UC1 means an 8-bit unsigned single-channel matrix, 
      CV_32FC2 means a 32-bit float matrix with two channels.

Solution 2

There is a function called

CV_MAT_TYPE()

So you can do something like this

CV_MAT_TYPE(tmp2->type)

That is going to return the 5 you need which is equivalent to the CV_32FC1.

PO. I came looking for what was the meaning of the 5 I was getting with CV_MAT_TYPE, so you gave me the answer with your question. Thanks.

Share:
10,676

Related videos on Youtube

Chris
Author by

Chris

Updated on June 04, 2022

Comments

  • Chris
    Chris over 1 year

    When using the CvMat type, the type of data is crucial to keeping your program running.

    For example, depending on whether your data is type float or unsigned char, you would choose one of these two commands:

    cvmGet(mat, row, col);
    cvGetReal2D(mat, row, col);
    

    Is there a universal approach to this? If the wrong data type matrix is passed to these calls, they crash at runtime. This is becoming an issue, since a function I have defined is getting passed several different types of matrices.

    How do you determine the data type of a matrix so you can always access its data?

    I tried using the "type()" function as such.

    CvMat* tmp_ptr = cvCreateMat(t_height,t_width,CV_8U);
    std::cout << "type = " << tmp_ptr->type() << std::endl;
    

    This does not compile, saying "term does not evaluate to a function taking 0 arguments". If I remove the brackets after the word type, I get a type of 1111638032

    EDIT minimal application that reproduces this...

    int main( int argc, char** argv )
    {
        CvMat *tmp2 = cvCreateMat(10,10, CV_32FC1);
        std::cout << "tmp2 type = " << tmp2->type << " and CV_32FC1 = " << CV_32FC1 << " and " << (tmp2->type == CV_32FC1) << std::endl;
    }
    

    Output: tmp2 type = 1111638021 and CV_32FC1 = 5 and 0

  • Chris
    Chris almost 12 years
    Thanks. I saw these functions previously, but the 'type' function is not working for me. If I use mat->type(), this no longer compiles. "Term does not evaluate to a function taking 0 arguments"
  • Chris
    Chris almost 12 years
    See, this is what I thought, but the result of type is 1111638032, which is totally wrong.CV_8U should result in the integer 0. This is where my confusion comes from. Why would the type be so wrong?
  • karlphillip
    karlphillip almost 12 years
    What if you create the mat with CV_8UC1? What is the size of the mat (width/height) you are trying to create? You are not testing the success of cvCreateMat(), it might be a good idea.
  • Chris
    Chris almost 12 years
    Same idea.. I tried again with 32FC1. CvMat *tmp2 = cvCreateMat(10,10, CV_32FC1); I compared tmp2->type against CV_32FC1. tmp2->type gives 1111638021 and the cv float gives 5.
  • karlphillip
    karlphillip almost 12 years
    Please write a complete minimal application and put it in your question so we can try to reproduce your problem.
  • Chris
    Chris almost 12 years
    Added the minimal application to the question