【发布时间】:2015-04-23 21:49:12
【问题描述】:
我正在处理图像对齐问题。
我有几张从一台相机拍摄的图像(每张 480x640)。这些图像形成一个圆柱面,每对调整后的图像共享几个相似的列,以帮助将所有图像对齐为全景图像。
一开始,我假设每对图像之间没有旋转或放大/缩小,所以我只计算两个图像之间的移位向量而不是断层扫描矩阵。
我的代码是:
//Store all images into a cv::Mat vector
std::vector<cv::Mat> image = ...
//Create a result Mat and set its content using the first image
cv::Mat result = image[0];
for (int i = 1; i < image.size(); i++) {
//calculate the shift vector(image[i]-result) based on feature points using SURF.
//this function works correctly and matched_diff[0] is det_x, and matched_diff[1] is det_y
std::vector<int> matched_diff= surfMatch (result,image[i]);
//Create a new Mat with size after combination
cv::Mat temp(image[i].rows+abs(matched_diff[1]),image[i].cols+abs(matched_diff[0]),CV_8UC3);
// Copy old result to its new position in temp
// Copy image[i] to its new position in temp
// Calculate pixel value of mixed range and copy them to their positions in temp
...
//store the new result and go back to the first step
result = temp.clone();
}
每次组合后det_x会增加300px左右,当然程序需要更多时间来完成下一次组合。
当我使用两个或三个图像对其进行测试时,该代码运行良好。
但在正确组合多张图片(大约 14 张)后,程序崩溃并出现错误:
malloc():内存损坏:0x0000000000cdae50 ***
我使用 GDB 检查内存问题发生的位置,但它告诉我问题是在 result = temp.clone(); 引起的。
我认为原因是经过多次组合,图像(Mat)变得相当大,创建temp需要更多内存,导致内存损坏错误。
我的问题是,根据我对 OpenCV 的理解,temp 对象将在每个 for 循环之后被解构。并且当崩溃时,下一个temp 的大小应该是3000px*640px。所以它需要的总内存是 3000*640*3Byte= 5.5Kb。 (对于 cv::Vec3b,每个像素需要 3 个 8 位来存储其 R G B 值。),但 GDB 表示它需要更多来克隆它。
因此,我真的很想知道我的理解是否有任何问题,我如何确定是我自己的错误还是内存问题?
谢谢!
【问题讨论】:
-
要添加解决方案,请在下面写下您的答案。不要在问题中这样做。请修复这个帖子,这是个好东西。
-
抱歉,感谢您的评论@karlphillip