Skip to content

util2d::getDepth(): use the mean for the neighbor depth tolerance - #1780

Merged
matlabbe merged 2 commits into
introlab:masterfrom
Arthur031221:fix-getdepth-neighbor-tolerance
Oct 1, 2026
Merged

matlabbe merged 2 commits into
introlab:masterfrom
Arthur031221:fix-getdepth-neighbor-tolerance

Conversation

@Arthur031221

Copy link
Copy Markdown
Contributor

Anyone calling util2d::getDepth() with estWithNeighborsIfNull = true on a pixel that has no depth can get an estimate that averages in a neighbor the depthErrorRatio check should have rejected. In this repository that caller is the automatic marker length estimation in MarkerDetector (Marker/Length = 0, the default), which samples the marker center and its four corners this way with a ratio of 0.02.

The fallback accepts a neighbor if fabs(d - tmp/count) < depthError, which compares it to the mean of the neighbors accepted so far, but the tolerance was depthErrorRatio * tmp, the running sum. Since tmp is count times the mean, the tolerance is the ratio times the number of neighbors accepted so far: up to 2x the ratio for the third neighbor and 3x for the fourth. The second neighbor is checked with count = 1, so it is not affected, and the change never turns a returned estimate into 0; it only keeps inconsistent neighbors out of the average. The comparison was changed to the mean in fb527e5 ("util2d::getDepth() fixed tmp not divided"), and the tolerance became depthErrorRatio * tmp when maxZError was replaced by a ratio in 4452e63. This PR computes the mean once and uses it for both.

Example: a 5x5 CV_32FC1 image with the center at 0 and its four neighbors set, visited in the order (2,1), (1,2), (3,2), (2,3):

neighbors (m)              ratio   master (0f98c63e)   this PR
1.00 1.02 0.98 1.25        0.10    1.062500            1.000000
1.00 1.01 0.99 1.05        0.02    1.012500            1.000000

In both rows the last neighbor (25% and 5% from the mean of the other three) is accepted on master. The same images converted to CV_16UC1 give the same values.

Through MarkerDetector::detect() with Marker/Length = 0: a 100 px marker (DICT_4X4_50, id 7) rendered in a 640x480 image, f = 525, and a constant depth of 1.05 m gives an estimated length of 0.198000. With the depth at the sampled center pixel (row 240, col 320) set to 0 and its last visited neighbor set 5% farther, master estimates 0.198707 and this PR 0.198000. The bias is small, but with Marker/Length = 0 the first estimate is reused for all later detections.

Testing: added Util2dTest.GetDepthEstimationFromNeighborsRejectsOutlier, which checks both images (and the first one as CV_16UC1). Its three assertions fail on master and pass with the fix. The full test_corelib run (Linux, GCC, OpenCV 5.0.0, PCL 1.15.1, built without Qt and most optional backends) gives 1361 passed and 260 skipped on master, and 1362 passed and 260 skipped with this PR.

Arthur031221 and others added 2 commits September 29, 2026 11:30
When estimating a missing depth from its 4-connected neighbors, a
neighbor is accepted if it is within depthErrorRatio of the mean of the
neighbors accepted so far. The tolerance was computed from the running
sum instead of the mean, so it grew to 2x and then 3x the ratio for the
third and fourth neighbor, letting inconsistent depths into the average.

@matlabbe matlabbe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The changes are sound. Thanks for reporting!

@matlabbe
matlabbe merged commit 20b040a into introlab:master Oct 1, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants