0
votes

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
"assignment to int temp it seems to work" <-- yes, that cast is nessecary for printing single items (otherwise cout sees a uchar and thinks it's a letter) . but ofc. you can avoid the whole loopings, and just do a cout << flat << endl; (print the whole Mat in 1 go)berak

1 Answers

2
votes

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!)