0
votes

I have a 3D vector and a number of images.I am trying to save the pixel data of the images into this vector. The first parameter contains the order number of the image in the set and the next two parameters should contain the pixel data at (x,y) location. Considering that I wrote the below code:-

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));
    }
}

But I am getting an error:

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

and I cant even try to print result of the vector out in the cmd screen. Any suggestions?

Edit 2- I tried another code by making the input_bin vector two-dimensional and float datatype

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)];
  }}

still the same error

1
Can we see the error?Moreira
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 894Abhishek V. Pai
out should be empty (zero rows, zero columns) in your code sample. Where does that error occur? The loop should not be entered at all...Micka
Consider out has an image in it. I should have mentioned it on the query,sorry about thatAbhishek V. Pai
if you in fact create the Mat somewhere (use CV_32SC1 because you use int) you are trying to access the Mat in the wrong order: out.at<int>(i,0) means ROW i, COLUMN 0, but your i variable loops over column and j loops over row.Micka

1 Answers

2
votes

Assuming that your images are of type CV_8UC1, i.e. single channel matrices of int, you can store a set of images in your data structure as follows:

// 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;
}

Please note that if you know that all images in the set have the same size you can avoid a few data copies.

Also, usually images are stored in continuous memory. So you'd better use a single vector<uchar> for each image, with size img.rows * img.cols, and access it using indices instead of (row, col).