Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

Nbb gravatar image

How to efficiently access vector of Mat ?

I read this doc here http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html 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
            }


        }
    }

}

How to efficiently access vector of Mat ?

I read this doc here http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html 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
wrong. and not efficient
            }


        }
    }

}