c++opencvobject-recognition

OpenCV Contours Moments?


What are moments of a contour? Could someone explain this in simplistic, non-mathematical terms? Possibly with an example? The official explanation is "integration over all the pixels in a contour". I have no idea what integration. And also what can contour moments be used for?


Solution

  • Moment of 0th degree for a black/white image with black=0 and white=1: this is simply the sum of the pixels, i.e. the number of white pixels.

    Moment of 1st degree for x-axis and some particular point X on the x-axis: this is the sum of the white pixel distances from X. I.e. it is the sum of their positions wrt. X. If you divide this by the number of white pixels (0th moment) you get the average white pixel position wrt. X.

    And similarly for y-axis.

    This idea generalizes to sumOf( pixelValue(position)*position^degree ). For degree 0 the last part is just 1 so that you simply sum the pixel values. For degree 1 it becomes a sum of positions, which can give you an average position, and for degree 2 it can reportedly give you a kind of direction.