使用CvMat类型时,数据类型对于保持程序运行至关重要.
例如,根据您的数据是类型float还是unsigned char,您可以选择以下两个命令之一:
cvmGet(mat, row, col);
cvGetReal2D(mat, row, col);
Run Code Online (Sandbox Code Playgroud)
对此有通用的方法吗?如果将错误的数据类型矩阵传递给这些调用,它们将在运行时崩溃.这已经成为一个问题,因为我定义的函数正在传递几种不同类型的矩阵.
如何确定矩阵的数据类型,以便始终可以访问其数据?
我尝试使用"type()"函数.
CvMat* tmp_ptr = cvCreateMat(t_height,t_width,CV_8U);
std::cout << "type = " << tmp_ptr->type() << std::endl;
Run Code Online (Sandbox Code Playgroud)
这不会编译,说"term does not evaluate to a function taking 0 arguments".如果我删除单词后的括号type,我得到一个类型1111638032
编辑最小的应用程序,再现这个......
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;
}
Run Code Online (Sandbox Code Playgroud)
输出: tmp2 type = 1111638021 and CV_32FC1 = 5 and 0
这type是一个变量,而不是一个函数:
CvMat* tmp_ptr = cvCreateMat(t_height,t_width,CV_8U);
std::cout << "type = " << tmp_ptr->type << std::endl;
Run Code Online (Sandbox Code Playgroud)
编辑:
至于type打印的异常值,根据这个答案,这个变量存储的不仅仅是数据类型.
因此,检查cvMat数据类型的适当方法是使用宏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;
Run Code Online (Sandbox Code Playgroud)
数据类型的命名约定是:
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9183 次 |
| 最近记录: |