Repository navigation
util2d::getDepth(): use the mean for the neighbor depth tolerance - #1780
Merged
matlabbe merged 2 commits intoOct 1, 2026
Merged
Conversation
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
approved these changes
Oct 1, 2026
matlabbe
left a comment
Member
There was a problem hiding this comment.
The changes are sound. Thanks for reporting!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Anyone calling
util2d::getDepth()withestWithNeighborsIfNull = trueon a pixel that has no depth can get an estimate that averages in a neighbor thedepthErrorRatiocheck should have rejected. In this repository that caller is the automatic marker length estimation inMarkerDetector(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 wasdepthErrorRatio * tmp, the running sum. Sincetmpiscounttimes 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 withcount= 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 becamedepthErrorRatio * tmpwhenmaxZErrorwas replaced by a ratio in 4452e63. This PR computes the mean once and uses it for both.Example: a 5x5
CV_32FC1image with the center at 0 and its four neighbors set, visited in the order (2,1), (1,2), (3,2), (2,3):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_16UC1give the same values.Through
MarkerDetector::detect()withMarker/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 withMarker/Length= 0 the first estimate is reused for all later detections.Testing: added
Util2dTest.GetDepthEstimationFromNeighborsRejectsOutlier, which checks both images (and the first one asCV_16UC1). Its three assertions fail on master and pass with the fix. The fulltest_corelibrun (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.