mlnthr
mlnthr

Reputation: 371

Difference between OpenCV type CV_32F and CV_32FC1

I would like to know if there is any difference between OpenCV types CV_32F and CV_32FC1? I already know that 32F stands for a "32bits floating point" and C1 for "single channel", but further explanations would be appreciated.

If yes, how are they different / which one should I use in which specific cases? As you might know that openCV types can get very tricky...

Thank you all in advance for your help!

Upvotes: 25

Views: 44252

Answers (1)

Miki
Miki

Reputation: 41765

The value for both CV_32F and CV_32FC1 is 5 (see explanation below), so numerically there is no difference.

However:

  • CV_32F defines the depth of each element of the matrix, while
  • CV_32FC1 defines both the depth of each element and the number of channels.

A few examples...

Many functions, e.g. Sobel or convertTo, require the destination depth (and not the number of channels), so you do:

Sobel(src, dst, CV_32F, 1, 0);

src.convertTo(dst, CV_32F);

But, when creating a matrix for example, you must also specify the number of channels, so:

Mat m(rows, cols, CV_32FC1);

Basically, every time you should also specify the number of channels, use CV_32FCx. If you just need the depth, use CV_32F


CV_32F is defined as:

 #define CV_32F  5

while CV_32FC1 is defined as:

#define CV_CN_SHIFT   3
#define CV_DEPTH_MAX  (1 << CV_CN_SHIFT)
#define CV_MAT_DEPTH_MASK       (CV_DEPTH_MAX - 1)
#define CV_MAT_DEPTH(flags)     ((flags) & CV_MAT_DEPTH_MASK)
#define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT))

#define CV_32FC1 CV_MAKETYPE(CV_32F,1)

which evaluates to 5.

You can check this with:

#include <opencv2\opencv.hpp>
#include <iostream>
int main()
{
    std::cout <<  CV_32F << std::endl;
    std::cout <<  CV_32FC1 << std::endl;

    return 0;
}

Upvotes: 55

Related Questions