Reputation: 3814
A Mat can be CV_8UC3, CV_8UC1, CV_32FC3
and etc. For example, for a Mat which is CV_8UC3
, I can set a pointer: Vec3b *p
to the Mat. However, If I only know the Mat's datatype which can be obtained using Mat.type()
, How can I set a proper pointer?
Upvotes: 2
Views: 4861
Reputation: 674
If you know a type, you can set a pointer to the first element of the first row of cv::Mat using ptr
(documentation)
cv::Mat matrix = cv::Mat::zeros(3, 3, CV_32F);
float* firstElement = matrix.ptr<float>(0);
Upvotes: 0
Reputation: 4438
The sad answer is: you can't. The type of data should be set in compilation time. But in your example actual type will be decided only during run time. You will have to put switch somewhere in your code. i.e. you will need to have different implementations of your algorithm for all possible types. Note however that you can prevent code duplication by using templates.
If you do know type of data, and the only thing you don't know is number of channels, then the problem is a bit simpler. For example if your image contains unsigned char you can write
uchar* p = img.ptr<uchar>();
And it doesn't matter whether your image have one channel or three. Of course when it comes to actually working with the pixels you do need this information.
Upvotes: 1
Reputation: 716
Use a void pointer. Void pointers can point to any data type.
http://www.learncpp.com/cpp-tutorial/613-void-pointers/
Upvotes: 0