Ask Your Question
0

copy row from one Mat to another (is it safe to pad descriptors with 0s)

asked 2016-01-15 19:37:53 -0600

phirestalker gravatar image

updated 2016-01-15 20:54:12 -0600

I am putting all the descriptors for multiple images from an ORB detector into one Mat to feed to cv::flann::index->build. Here is the code I am trying.

EDIT: I guess the main question right now. Is it safe to pad the descriptors with zeros at the end to make them the same number of columns?

while (true)
{
    buffer = indexBuilder.value("descriptors").toString().toStdString();
    Mat mat;
    Mat matonerow;
    FileStorage fs(buffer,FileStorage::READ | FileStorage::MEMORY | FileStorage::FORMAT_YAML);
    fs["descriptors"] >> mat;
    fs.release();
    matonerow =  mat.reshape(1,1);
    qDebug() << "matonerow cols" << matonerow.cols << "rows" << matonerow.rows;
    indexTemp.push_back(matonerow);
    progress->setValue(iterations);
    iterations++;
    if (!indexBuilder.next())
        break;
}

the problem is that some of the descriptors are smaller, so the call to push_back fails with

OpenCV Error: Sizes of input arguments do not match () in push_back, file /tmp/opencv320160111-57911-i2x9h0/opencv-3.1.0/modules/core/src/matrix.cpp, line 834
libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv320160111-57911-i2x9h0/opencv-3.1.0/modules/core/src/matrix.cpp:834: error: (-209)  in function push_back

so how do I copy the one row mat to the tempindex mat? NOTE: I am using mat because I intend to convert it to UMat before passing it to build the index so I can take advantage of opencl through TAPI.

UPDATE: ok it works now but only for rows with the same amount of columns, which is no better than push_back so I still need another method!

    try
    {
        if (iterations == 1)
            indexTemp = Mat(row_count,matonerow.cols,CV_8U);

            matonerow.copyTo(indexTemp.row(iterations - 1));
    }
    catch (Exception e)
    {
        qCritical() << "failed to copy row of Mat at loop" << iterations << e.err.c_str() << e.msg.c_str();
    }

NOTE: iterations starts at 1

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-01-18 19:08:38 -0600

phirestalker gravatar image

got it, all I had to do was add the lines

        if (mat.rows < 500)
        mat.push_back(Mat::zeros((500 - mat.rows),32,mat.type()));
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-01-15 19:37:53 -0600

Seen: 363 times

Last updated: Jan 18 '16