Ask Your Question
0

efficient access of matrices

asked 2017-03-28 04:27:00 -0600

Nbb gravatar image

I know I can access matrices in 2 ways, http://docs.opencv.org/2.4/doc/tutori...

Either using Mat.at<uchar>(row,col) or using the method below. My question is, if I am using Visual Studio and am targetting real-time applications. Will there be a difference between the two methods ? Visual Studio has a release build so I am not sure if accessing matrices becomes a lot more optimized.

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;

}

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2017-03-28 04:45:33 -0600

LBerger gravatar image

updated 2017-03-28 04:46:10 -0600

Yes it makes difference and you should use pointer in loops

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

If you want to improve code you can use ParallelLoopBody. Reading your code I think all is already written in LUT function

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-28 04:27:00 -0600

Seen: 121 times

Last updated: Mar 28 '17