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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With