Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@akretz
Copy link
Contributor

@akretz akretz commented Feb 18, 2024

The problem in CoreTest.testMahalanobis is that the generated covariance matrix is not invertible, because the random matrix has too few samples.

Consider 3 vectors in 3D space. They all lie on a plane, so they can be expressed as their mean + 2 linearly independent vectors. This makes their covariance matrix singular. The same applies to n ≠ 3. You can verify this by observing that in

import numpy as np
n = 10
a = np.random.uniform(-128, 128, (n, n))
print(np.linalg.eigvals(np.cov(a)))

you will for any n always get a zero eigenvalue (actually close to zero due to numerical errors).

The inverses in #24348 are different across platforms because, well, the inverse doesn't exist up to numerical errors. The fix is to add one more random sample to make the covariance matrix non-singular.

This PR resolves #24348

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

Copy link
Contributor

@asmorkalov asmorkalov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thanks a lot for the great catch!

@asmorkalov asmorkalov added this to the 4.10.0 milestone Feb 19, 2024
@asmorkalov asmorkalov merged commit 54be266 into opencv:4.x Feb 19, 2024
@asmorkalov asmorkalov self-assigned this Feb 19, 2024
@asmorkalov asmorkalov mentioned this pull request Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Java test for Core.Mahalanobis fails on arm64

2 participants