Search code examples
c++opencvimage-processingfeature-extractiondescriptor

OpenCV - parameters in moments() function


The C++ function is:

Moments moments(InputArray array, bool binaryImage=false )

The first one I understand what it is, for the second one it says:

binaryImage – If it is true, all non-zero image pixels are treated as 1’s. The parameter is used for images only.

What does this mean exactly? I say it's true for binary images only and false for non-binary images? In my application I use a binary image to calculate simple moments.


Solution

  • It means that if this value is true the image you insert will be treated as it is a binary image meaning that even if you have values that are bigger than 1 they will be treated as 1.

    I(x,y) > 0 = 1

    I(x,y) == 0 = 0

    If the value you insert is false than the values in the moments will take the real value into the moments calculation.

    For example lets say you have the following 3X3 image:

    100 0 10

    10 0 1

    0 0 0

    m00 which is the area of the image will be:

    If the flag is true than we have 4 pixels that are non zero and the value will be 4.

    If the flag is false we will have 100+10+10+1 = 121

    When should you use what?

    Lets say we have a blob in our image. If we treat the image as binary than the moments will give us spatial information about the blob. For example m01/m00 and m10/m00 will give ust the center of mass of the object.

    But if we treat the image not as binary we than moments will give us texture/color information. For example m00/(number of pixels in the blob) = the mean color of the blob.