Write big OpenCv Mat
Write big OpenCv Mat
我正试图将OpenCV Mat格式的大浮点矩阵写入二进制文件。我在Linux x64的虚拟机上。
并且我在计数器536000000周围粉碎,所以似乎我在p+ I *4的"可疑行"周围有int溢出?(顺便说一下,assert没有检测到这种情况,为什么?)。
所以当我把"int I"改为"int64 I"时,一切似乎都很好,但我不确定添加超过2^32的char*指针是否安全?
另外,OpenCV矩阵的最大大小是多少?
bool WriteFloatMat(string path, const Mat &img)
{
ofstream fs(path, ios::binary);
fs.write( (char*)&(img.cols), 4);
fs.write( (char*)&(img.rows), 4);
int64 sz_ = img.cols*img.rows;//
assert(sz_ < numeric_limits<int>::max());//
int sz = img.cols*img.rows;
cout << sz << endl;
int64 counter=0;
int64 maxVal= numeric_limits<int>::max();
char* p= image.ptr<char>();
for(int i=0; i<sz; ++i)
{
assert(i*4 < numeric_limits<int>::max());//suspicious line
fs.write( p+i*4, 4);
++counter;
if(counter%1000000==0)
cout << counter << endl;
if(counter>maxVal)
cout << "Out of limits!" << endl;
}
return true;
}
void TestIO()
{
//max matrix dimensions?
int rows= 1000*1000*10;
int cols= 100;
Mat mat(rows,cols,CV_32FC1);
mat=mat+7;
double t = (double)getTickCount();
WriteFloatMat("/media/dummy.big", mat);
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Times passed in seconds: " << t << endl;
}
更新:我理解为什么assert(i*4 < numeric_limits<int>::max());
不工作,因为I *4溢出和<0所以assert(i*4 >=0 );
工作。
OpenCV对图像的大小没有内部限制,它实际上是将其视为内存块。因此,限制将与您正在使用的c++系统有关。
对于它的价值,我发现boost数字库中的numeric_cast<>功能非常有助于捕获转换错误,例如您遇到的assert(i*4 < numeric_limits<int>::max())
。(当然,这个问题很微妙,所以它可能也没有帮助。)
相关文章:
- C++中的openCV Mat访问冲突
- OpenCV 和从 OutputArrays 或 (Mat) 读取数据
- 为什么用cv::Mat(opencv)更改指向数据的指针与cv::filter2D有问题
- 在Android Java应用程序中使用OpenCV Mat.OpenCV Mat出现NoClassDefFound错误
- 在MFC图片框中显示IPLimage/ MAT (opencv)
- 不完整的类型 "cv::Mat" opencv c++
- 从NumPy数组到Mat(OpenCV)的C++转换
- 从Mat(OpenCV)生成具有透明度的PNG
- 将2个Mat的内容添加到另一个Mat opencv c++中
- Mat& vs Mat opencv 2 c++
- 堆损坏错误;无法发布 cv::Mat opencv
- 在矢量到矢量中复制数据<float> <Mat> (Opencv/C++)
- 将矢量保存在Mat opencv中
- 如何将使用Qt Paint Application绘制的图像传输到Mat openCV
- 如何在<Mat>opencv c++中有效地清除向量
- 我可以确定cv::Mat Opencv中的频道数量吗
- Mat OpenCv的赋值错误
- 使用Mat OpenCV访问像素
- 如何在不同的线程中将图像数据从BitmapSource(WPF)复制到C++/CLI中的cv::Mat(OpenCV)
- 计算 Mat OpenCV 子集的总和