如何访问 OpenCV 中的矢量垫对象
How to get access to vector mat object in OpenCV
我想将文件中的一些图片加载到 Mat 对象 (OpenCV) 中,并希望将它们存储在矢量中。此外,OpenCV calls/objects 需要向量(如:AlignMTB)作为参数。但是在用 Mat 对象填充向量之后,我只能访问我添加到向量中的最后一个元素。
在示例中,我首先将图像加载到中间 Mat 对象并将其转换为 CV_32FC3。然后我打印出一个样本像素的 BGR 值。打印出来的是:
File 0: 13 13 157
File 1: 17 20 159
File 2: 8 8 152
然后我将这个中间垫添加到垫矢量图像中。
之后我试图打印出第一张和第二张图像的样本像素值,但我总是得到第三张图像的值:
File 0: 8 8 152
File 1: 8 8 152
我在访问矢量数据时哪里出错了?
我正在尝试使用这个例程:
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img);
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;
问题不在于vector::push_back
,因为它将构造给定元素的副本。但是,问题是 Mat
的复制构造函数不复制关联数据:
No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it.
解决问题的方法是显式 Mat::clone
也复制数据的操作,或者在 for 循环中移动矩阵声明。
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img.clone());
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;
我想将文件中的一些图片加载到 Mat 对象 (OpenCV) 中,并希望将它们存储在矢量中。此外,OpenCV calls/objects 需要向量(如:AlignMTB)作为参数。但是在用 Mat 对象填充向量之后,我只能访问我添加到向量中的最后一个元素。
在示例中,我首先将图像加载到中间 Mat 对象并将其转换为 CV_32FC3。然后我打印出一个样本像素的 BGR 值。打印出来的是:
File 0: 13 13 157
File 1: 17 20 159
File 2: 8 8 152
然后我将这个中间垫添加到垫矢量图像中。
之后我试图打印出第一张和第二张图像的样本像素值,但我总是得到第三张图像的值:
File 0: 8 8 152
File 1: 8 8 152
我在访问矢量数据时哪里出错了?
我正在尝试使用这个例程:
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img);
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;
问题不在于vector::push_back
,因为它将构造给定元素的副本。但是,问题是 Mat
的复制构造函数不复制关联数据:
No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it.
解决问题的方法是显式 Mat::clone
也复制数据的操作,或者在 for 循环中移动矩阵声明。
vector<Mat> images;
images.reserve(3);
Mat img;
for (int i = 0; i < 3; i++)
{
imread("F:/Test/file" + to_string(i) + ".jpg").convertTo(img, CV_32FC3);
cout << "File " << i << ": " << img.at<Vec3f>(800, 800)[0] << " " << img.at<Vec3f>(800, 800)[1] << " " << img.at<Vec3f>(800, 800)[2] << endl;
images.push_back(img.clone());
}
cout << endl;
cout << "File " << 0 << ": " << images[0].at<Vec3f>(800, 800)[0] << " " << images[0].at<Vec3f>(800, 800)[1] << " " << images[0].at<Vec3f>(800, 800)[2] << endl;
cout << "File " << 1 << ": " << images[1].at<Vec3f>(800, 800)[0] << " " << images[1].at<Vec3f>(800, 800)[1] << " " << images[1].at<Vec3f>(800, 800)[2] << endl;