Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open CV generic Mat function headers

In OpenCV it's common to access a pixel in a Mat object like so:

float b = A.at<float>(4,5);

The problem is, if you don't know the type of data apriori, you're stuck. Is there a way of writing generic function headers that accept a Mat with template type T? I'd like to build functions for linear algebra calculations, and I don't want to have an if clause separating double and float. Something like:

 void func(Mat <T> a) { 
     a.at<T>(3,4) = ...

Is this possible in OpenCV?

like image 992
nbubis Avatar asked Apr 12 '13 07:04

nbubis


2 Answers

It appears one more way to do this would be to use the Mat_ object instead of Mat:

template<typename T>
void func(Mat_ <T> a) {
   cout << a(0,0) << endl;
}

If you want to pass a Mat to func, you must specify the type:

Mat a;
func(Mat_<float>(a)); 

If you use a differnt type than the original Mat type, OpenCV will preform the conversion for you.

like image 177
nbubis Avatar answered Oct 05 '22 05:10

nbubis


This is possible simply by templating your function :

template<typename T>
void func(Mat a) { 
    a.at<T>(3,4) = ...

But note that you have no easy way to constraint the type T to be only double or float, and your algorithm won't probably work on other types, but it may not be an actual problem.

Also note the drawbacks of using templates : What are the disadvantages of using templates?

like image 21
zakinster Avatar answered Oct 05 '22 05:10

zakinster