c++ - 使用opencv将像素数据分配到 vector 中

标签 c++ opencv image-processing vector voxel

我有一个 3D vector 和一些图像。我试图将图像的像素数据保存到这个 vector 中。第一个参数包含图像在集合中的顺序号,接下来的两个参数应包含 (x,y) 位置的像素数据。考虑到我写了下面的代码:-

Mat out;// out has an image in it
vector <vector< vector< int> >>  Input_bin;
Input_bin.push_back(vector<vector<int>>(inst_num));// the order number  

for (int i = 0; i <= out.cols; i++)
{

    for (int j = 0; j <= out.rows; j++)
    {
         Input_bin[inst_num].push_back(vector<int>(out.at<int>(i,0)));
         Input_bin[inst_num][out.at<int>(i,0)].push_back(out.at<int>(0,j));
    }
}

但是我得到一个错误:

OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si ze.p[0] && (unsigned)(i1 * DataType<_Tp>::channels) < (unsigned)(size.p[1] * cha nnels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 15) == elemSize1()) in cv::Mat::at, file C:\opencv_3\opencv\bui ld\include\opencv2/core/mat.inl.hpp, line 894

我什至无法尝试在 cmd 屏幕上打印 vector 结果。有什么建议吗?

编辑 2- 我通过使 input_bin vector 成为二维和 float 据类型来尝试另一个代码

for (int i = 0; i <= out.cols; i++)
{

    for (int j = 0; j <= out.rows; j++)
    {
        Input_bin[inst_num][out.at<float>(i, j)];
  }}

还是一样的错误

最佳答案

假设您的图像类型为 CV_8UC1 ,即 int 的单 channel 矩阵,您可以在数据结构中存储一组图像,如下所示:

// Your set of images of type CV_8UC1
vector<Mat> set_of_images; 
// How many images?
size_t n_images = set_of_images.size();

// Data structure
vector<vector<vector<uchar>>> input(n_images);

// For each image in the set
for(size_t i=0; i<n_images; ++i)
{
    // Get the corresponding Mat
    const Mat& img = set_of_images[i];

    // Allocate the vectors
    vector<vector<uchar>> vimg(img.rows, vector<uchar>(img.cols));

    // Copy all values from Mat to vimg
    for(int r = 0; r < img.rows; ++r)
    {
        for(int c = 0; c < img.cols; ++c)
        {
            vimg[r][c] = img.at<uchar>(r,c);
        }
    }
    // Copy vimg into the data structure
    input[i] = vimg;
}

请注意,如果您知道集中的所有图像都具有相同的大小,则可以避免一些数据拷贝。

此外,通常图像存储在连续存储器中。所以你最好使用单个 vector<uchar>对于每个图像,大小为 img.rows * img.cols ,并使用索引而不是 (row, col) 访问它.

关于c++ - 使用opencv将像素数据分配到 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36421100/

相关文章:

Android OpenCV - 从 Houghlines 检测曲线

c - 在C中读取PPM图像

graphics - 如果相机平面对齐,我需要纠正吗?

c++ - USB 传输的 LibUSB C++ 格式不同

c++ - 为什么使用 NULL 指针调用类方法有效?

Python Django 错误

c# - Emgucv Houghlines行长

xml - 如何合并opencv中haartraining生成的两个分类器xml

c++ - 使用模板特化选择合适的类型

opencv - 如何判断处理后图像的色彩空间?