【发布时间】:2020-01-04 14:16:44
【问题描述】:
我编写了一个自定义类来存储图像并最终根据这些图像计算校准,但我遇到了图像存储方式的问题。我有两个执行此操作的重载函数,一个使用cv::imread 从文件中读取图像,另一个使用中间Snapshot 数据结构来保存数据。使用cv::imread 的函数可以正常工作,但使用自定义数据结构的函数不行。我现在正在尝试存储三张图像,问题是当我将图像推入矢量时,第二张图像的数据被复制到第一张中。
这是工作函数:
bool CalibClass::AddImage(const std::string& snapshotPath) {
cv::Mat img = cv::imread(snapshotPath);
// _snapshots is a private member declared as a std::vector<cv::Mat>
_snapshots.push_back(img);
return true;
}
这是不起作用的功能:
bool CalibClass::AddImage(const ImageSet& snapshot) {
RGBImage *rgb_image_ptr = snapshot.GetRGBImage();
std::vector<unsigned char> img_data(rgb_image_ptr->GetData());
cv::Mat img(rgb_image_ptr->GetHeight(), rgb_image_ptr->GetWidth(), CV_8UC3, img_data.data());
_snapshots.push_back(img);
return true;
}
ImageSet 类将图像存储为std::unique_ptr<RGBImage>。 RGBImage 类将图像数据存储为std::vector<unsigned char>。
这是图像从main 加载到类中的方式:
cv::Mat img1 = cv::imread("img1.png");
cv::Mat img2 = cv::imread("img2.png");
cv::Mat img3 = cv::imread("img3.png");
int length = img1.total() * img1.elemSize();
std::vector<unsigned char> data1;
std::vector<unsigned char> data2;
std::vector<unsigned char> data3;
for (int i = 0; i < length; i++) {
data1.push_back(img1.data[i]);
}
for (int i = 0; i < length; i++) {
data2.push_back(img2.data[i]);
}
for (int i = 0; i < length; i++) {
data3.push_back(img3.data[i]);
}
CalibClass calib_test;
std::unique_ptr<RGBImage> rgb_image_ptr1(new RGBImage(img1.rows, img1.cols, data1));
ImageSet new_snap1(rgb_image_ptr1, nullptr, 0);
calib_test.AddImage(new_snap1);
std::unique_ptr<RGBImage> rgb_image_ptr2(new RGBImage(img2.rows, img2.cols, data2));
ImageSet new_snap2(rgb_image_ptr2, nullptr, 0);
calib_test.AddImage(new_snap2);
std::unique_ptr<RGBImage> rgb_image_ptr3(new RGBImage(img3.rows, img3.cols, data3));
ImageSet new_snap3(rgb_image_ptr3, nullptr, 0);
calib_test.AddImage(new_snap3);
当我在函数内部放置断点并检查_snapshots的内容时,第一个元素是第二个图像,第二个和第三个元素是第三个图像。当我在所有AddImage() 调用后设置断点时,_snapshots 的内容有第二个图像作为第一个元素,第三个图像作为第二个元素,第三个元素有一个带有无效数据的cv::Mat。
这两种方法存储图像不同的原因是什么?解决此问题的方法是什么?
【问题讨论】:
-
RGBImage *rgb_image_ptr = snapshot.GetRGBImage();你如何将唯一指针转换为原始指针? -
snapshot.GetRGBImage()返回一个指向RGBImage的指针,它在内部将图像数据存储为std::vector<unsigned char> -
但是图像集将其存储为唯一指针?一个最小的完整可重现示例怎么样?
-
是的,
ImageSet将其存储为std::unique_ptr<RGBImage> -
我无法想象用最少 段代码来重现您的问题。请发帖minimal reproducible example
标签: c++ opencv c++11 smart-pointers