Convert vector<Mat> to vector file Opencv

1.1k Views Asked by At

How can I convert a vector of Mat files into a Mat file? I have got a vector with size N. Every mat file is of size 1xM. I want to create a new Mat file of size 1xNM? How can I do so in Opencv? Or better to create a new vector with size MN which will contain the pixels of every Mat file. Is there a reshape function in Opencv?

   Mat flat;       // output

    for (size_t i=0; i<patch_image.size(); i++)
    {
        // you said, those would be 1xM, but let's make sure.
        flat.push_back( patch_image[i].reshape(1,1) ); 
    }

    flat = flat.reshape(1,1);

    int temp;   
    for(int i=0; i<flat.rows; i++){
        for(int j=0; j<flat.cols; j++){

            temp = (int)flat.at<uchar>(i,j);
            cout << temp << "\t";
        }       
        cout << endl;
    }

However that assignment to int temp it seems to work.

1

There are 1 best solutions below

1
On BEST ANSWER

yes, there is a reshape function.

vector<Mat> vm; // input
Mat flat;       // output

for (size_t i=0; i<vm.size(); i++)
{
    // you said, those would be 1xM, but let's make sure.
    flat.push_back( vm[i].reshape(1,1) ); 
}

flat = flat.reshape(1,1); // all in one row, 1xN*M now

note, that reshape returns a new Mat header, it does not work inplace (common pitfall here!)