Skip to content

cv::moments does not handle the degenerated cases of a single point or aligned points #100

Description

@lstahlman

Mirrored from upstream: opencv#28222
Original author: @PierreChatelier · created 2025-12-18T07:41:27Z
Upstream labels: category: core, category: documentation
Selection: mirrored by mirror-issues.py


System Information

OpenCV 4.12.0
Windows 7
MSVC 2019+

Detailed description

std::vector<cv::Point> contour = {...};
cv::Moments m = cv::moments(contour);

as documented, cv:moments() can be used to determine the mass center of a set of points : $\bar{x}=\frac{m_{10}}{m_{00}}$, $\bar{y}=\frac{m_{01}}{m_{00}}$

However, it fails if :

  • the contour is a single point
  • the contour is a set of aligned points

in that case, m00 will be 0

I undersand that 2nd and 3rd order might be ill defined in that case, but my main use case if the mass center, and it's a pity to see it fail there.

Should it be fixed or only documented ?

Steps to reproduce

cv::vectorcv::Point p1 = {{1, 2}};
cv::vectorcv::Point ps = {{1, 2}, {2, 2}, {3, 2}};

cv::Moment m1 = cv::moments(p1); //m1.m00 is 0
cv::Moment ms = cv::moments(ps); //ms.m00 is 0

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions