void StereoMatch::saveOneDimension(cv::Mat& pointClouds, const char* filename)
{
const double max_z = 1.0e4;
try
{
FILE* fp = fopen(filename, "wt");
fprintf(fp, "%02d\n", pointClouds.rows);
fprintf(fp, "%02d\n", pointClouds.cols);
for (int y = 0; y < pointClouds.rows; y++)
{
for (int x = 0; x < pointClouds.cols; x++)
{
//cv::Vec3f point = pointClouds.at<cv::Vec3f>(y, x);
int point = pointClouds.at<unsigned char>(y, x); //float point = pointClouds.at<float>(y, x)不行,原因还没有弄清楚
//if (fabs(point - max_z) < FLT_EPSILON || fabs(point) > max_z)
if (fabs(point - max_z) < FLT_EPSILON || point > max_z)
fprintf(fp, "%d \n", 0);
else
fprintf(fp, "%d \n", point);
}
}
fclose(fp);
}
在保存矩阵时出现如下错误:
运行出错
template<typename _Tp> inline _Tp& Mat::at(int i0, int i1)
{
CV_DbgAssert( dims <= 2 && data && (unsigned)i0 < (unsigned)size.p[0] &&
(unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) &&
CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1());
return ((_Tp*)(data + step.p[0]*i0))[i1];
}
此问题主要出现在
CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1())这里得出来的值部位真。为什么?
首先要知道 int point = pointClouds.at<unsigned char>(y, x); 中at后面的数据类型,其类型有如下:
IPL_DEPTH_1U 1
IPL_DEPTH_8U - 无符号8位整型
IPL_DEPTH_8S - 有符号8位整型
IPL_DEPTH_16U - 无符号16位整型
IPL_DEPTH_16S - 有符号16位整型
IPL_DEPTH_32S - 有符号32位整型
IPL_DEPTH_32F - 单精度浮点数
IPL_DEPTH_64F - 双精度浮点数
enum{CV_8U,CV_8S,CV_16U,CV_16S,CV_32S,CV_32F,CV_64F}
而elemSize1()的值与你矩阵的数据类型有关,具体去CSDN上有,只有当得到的CV_ELEM_SIZE1(DataType<_Tp>::depth) == elemSize1())相等时才能够进行矩阵保存。