I am trying to make converting between a custom class and OpenCV easier.
Converting constructors allow us to declare a class without the explicit
keyword and construct from an alternate data type.
For example, we could declare two classes:
class Foo
{
public:
std::vector<int32_t> data;
Foo(int k) : data(k) {}
};
class FooProxy : public cv::Mat
{
public:
FooProxy(const Foo & foo) : cv::Mat(foo.data.size(), 1, CV_32SC1, (void*)foo.data.data()) {}
};
And then we can create a FooProxy
by setting it equal and then then using any OpenCV function we want:
Foo myFoo(3);
myFoo.data = { 1,2,3 };
FooProxy fp = myFoo;
fp.setTo(1);
float myNorm = cv::norm(fp);
std::cout << "myFoo: " << myFoo.data.at(0) << ", " << myFoo.data.at(1) << ", " << myFoo.data.at(2) << "\n";
std::cout << "fp: " << fp.at<int32_t>(0) << ", " << fp.at<int32_t>(1) << ", " << fp.at<int32_t>(2) << "\n";
std::cout << "The Norm is: " << myNorm << "\n";
with output:
myFoo: 1, 1, 1
fp: 1, 1, 1
The Norm is: 1.73205
However, I would much rather write:
float myNorm = cv::norm(myFoo);
and have c++ automatically convert myFoo
. Is this possible?
This works:
float myNorm2 = cv::norm(FooProxy(myFoo));
but not the simpler version.