wanderer
wanderer

Reputation: 1219

Best way for OpenCV type independent image access?

I am writing a function that takes a OpenCV Mat structure and does some processing. The problem is that OpenCV doesn't seem to have a type independent way for accessing the strucutre so I have to do something like this.

int myfunc(cv::Mat& im, int iRow, int iCol)
{
    int iResult = 0; 
    if (im.type()==CV_16U) iResult = (int)im.at<unsighed short>(iRow, icol);
    else if (im.type()==CV_8U) iResult = (int)im.at<uchar>(iRow, icol);

    return iResult;
}

Is there a clean way to do this?

Upvotes: 3

Views: 817

Answers (2)

Martin Beckett
Martin Beckett

Reputation: 96109

It's because you generally require high performance in image processing and so need different algorithms for 8bit, 16bit or double image types.

You can do it all with templated types but that's often just a less readable way of having a switch statement.

ps if you do care about performance you shouldn't be using the .at<> operator, get a pointer to the start of the line with .ptr() and then step in units of whatever pixel type you have

Upvotes: 1

mevatron
mevatron

Reputation: 14011

The only way I have found to do this is to use the templated Mat class Mat_.

So, for your example, you might try something like the following:

template<class T>
T myfunc(cv::Mat_<T>& im, int iRow, int iCol)
{
    return im(iRow, iCol);
}

Hope that helps!

Upvotes: 0

Related Questions