Ask Your Question
0

How to efficiently access vector of Mat ?

asked 2017-03-07 22:40:27 -0600

Nbb gravatar image

updated 2017-03-07 22:40:59 -0600

I read this doc here http://docs.opencv.org/2.4/doc/tutori... that teaches me how I can efficiently scan an image.

Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() == CV_8U);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols * channels;

    if (I.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            p[j] = table[p[j]];
        }
    }
    return I;
}

What if as input I have an additional vector of Mat and I am trying to copy the value of an element from Mat angle to a certain Mat angle_ii depending on the value is has ? In the code below, am I forced to create multiple angle_ii_row so that each variable points to each Mat of angle_ii ?

void construct_ii( cv::Mat& angle, std::vector<cv::Mat>& angle_ii)
{
    int rows = angle.rows;
    int cols = angle.cols;

    if (angle.isContinuous())
    {
        cols *= rows;
        rows = 1;
    }

    for (int i = 0; i < angle.rows; i++)
    {
        float* angle_row = angle.ptr<float>(i);
        float** angle_ii_row = angle_ii.ptr<float>(i); // wrong

        for (int j = 0; j < angle.rows; j++)
        {
            float angle_temp = angle_row[j];
            int which = floor(angle_temp/20);

            switch (which)
            {
            case 0 :  angle_ii[0].at<float>(row,col) = angle_temp; // also wrong. and not efficient
            }


        }
    }

}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-08 10:03:39 -0600

kbarni gravatar image

updated 2017-03-08 10:19:28 -0600

You are on the right track, basically you need an array of pointers for the current line of each image:

....
float *angle_ii_row[18]; //probably you'll have 18 images if you copy the angles in 20 bins
for(int k=0;k<18;k++) angle_ii_row[i]=angle_ii[i].ptr(i);
for (int j = 0; j < angle.cols; j++) //there you had a bug ( j < angle.rows in your code)
{
    float angle_temp = angle_row[j];
    int which = floor(angle_temp/20);
    angle_row_ii[which][j]=angle_temp; //directly copy the angle to the corresponding matrix
}

You can declare angle_ii_row dynamically also, as a vector<float*> or as a float** (and allocate with new).

A cleaner - but much less efficient - solution would be to create masks using thresholding and copy the images using these masks.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-07 22:40:27 -0600

Seen: 874 times

Last updated: Mar 08 '17