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?
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With