21

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!

1 Answer 1

53

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;
}

2
  • 1
    Thank you very much for this complete answer, it's clearer now :)
    – mlnthr
    Jun 3, 2016 at 8:01
  • 3
    If the answer solved your problem. Accept it by checking the green tick.
    – sagar suri
    Nov 7, 2017 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.