Build OpenCV Element Type at Run Time

In OpenCV, when I need to create cv :: Mat, I will need to do something along the line

cv::Mat new_mat(width, height, CV_32FC3)

What happens if I only know that I need the elements to be either float or double +, do I need 1/2/3 of the channel at runtime?

In other words, given the type of the element (float) and the number of channels (int), how can I build the term: CV_32FC3?

+3
source share
1 answer

Read the source cxtypes.h. It contains the following lines:

#define CV_32FC1 CV_MAKETYPE(CV_32F,1)
#define CV_32FC2 CV_MAKETYPE(CV_32F,2)
#define CV_32FC3 CV_MAKETYPE(CV_32F,3)
#define CV_32FC4 CV_MAKETYPE(CV_32F,4)
#define CV_32FC(n) CV_MAKETYPE(CV_32F,(n))

#define CV_64FC1 CV_MAKETYPE(CV_64F,1)
#define CV_64FC2 CV_MAKETYPE(CV_64F,2)
#define CV_64FC3 CV_MAKETYPE(CV_64F,3)
#define CV_64FC4 CV_MAKETYPE(CV_64F,4)
#define CV_64FC(n) CV_MAKETYPE(CV_64F,(n))

CV_MAKETYPE defined as:

#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))

This suggests the following code:

bool isdouble;
int nchannels;
// ...
if (isdouble) typeflag = CV_64FC(nchannels);
else typeflag = CV_32FC(nchannels);

I have not tested this; let me know if that works. Also: I hate opencv terrible type of security.

+5
source

Source: https://habr.com/ru/post/1751853/


All Articles