Skip to content

Fix Robertson Calibration NaN Bug#20054

Merged
alalek merged 6 commits intoopencv:masterfrom
danielenricocahall:fix-robertson-calibration-bug
May 30, 2021
Merged

Fix Robertson Calibration NaN Bug#20054
alalek merged 6 commits intoopencv:masterfrom
danielenricocahall:fix-robertson-calibration-bug

Conversation

@danielenricocahall
Copy link
Copy Markdown
Contributor

@danielenricocahall danielenricocahall commented May 9, 2021

Merge with extra: opencv/opencv_extra#873

Addresses #18180. The issue seemed to be with numerical stability when w_sum matrix contained zeroes. Should be merged in conjunction with opencv/opencv_extra#873, which contains test data taken from original issue.

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 other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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

@danielenricocahall danielenricocahall changed the title Fix Robertson Calibration Bug Fix Robertson Calibration NaN Bug May 9, 2021
@alalek
Copy link
Copy Markdown
Member

alalek commented May 19, 2021

@danielenricocahall Please take a look on failed builds (you may start from "debug" local build)

Mat response_no_nans = response.clone();
patchNaNs(response_no_nans);
// since there should be no NaNs, original response vs. response with NaNs patched should be identical
bool isEqual = (sum(response != response_no_nans) == Scalar(0,0,0));
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.

norm(response, response_no_nans, NORM_INF) == 0.0 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's smarter - let me do that. Thanks!

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.

NORM_INF doesn't properly handle NaNs.

There is still a problem:

EXPECT_EQ(0.0, cv::norm(response, response_no_nans, NORM_L2));

Results to this error message (Linux Debug):

Expected equality of these values:
  0.0
    Which is: 0
  cv::norm(response, response_no_nans, NORM_L2)
    Which is: -nan
[  FAILED  ] Photo_CalibrateRobertson.bug_18180 (1789 ms)

Copy link
Copy Markdown
Contributor Author

@danielenricocahall danielenricocahall May 21, 2021

Choose a reason for hiding this comment

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

Ah I figured out the issue, I had to move the epsilon back. Addressed. Thank you!

Comment on lines +256 to +258
glob(string(cvtest::TS::ptr()->get_data_path()) + "hdr/exposures/bug_18180/*.jpg", fn, false);
for (auto & i : fn)
images.push_back(imread(i));
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.

Please avoid using glob() in tests. Use fixed input instead.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Got it!

@danielenricocahall danielenricocahall force-pushed the fix-robertson-calibration-bug branch 2 times, most recently from 46543be to c9feccc Compare May 20, 2021 14:17
@danielenricocahall danielenricocahall requested a review from alalek May 20, 2021 15:15
@danielenricocahall
Copy link
Copy Markdown
Contributor Author

@danielenricocahall Please take a look on failed builds (you may start from "debug" local build)

I think all issues are resolved - please feel free to re-review at earliest convenience.

fix windows build warnings

fix vector type for tests

update tests

make threshold float

address test comments

fix tests and move epsilon again
@danielenricocahall danielenricocahall force-pushed the fix-robertson-calibration-bug branch from db7479f to 434daba Compare May 21, 2021 00:02
@vpisarev vpisarev self-assigned this May 28, 2021
@vpisarev vpisarev self-requested a review May 28, 2021 06:04
@vpisarev
Copy link
Copy Markdown
Contributor

thank you! 👍

wsum += times.at<float>((int)i) * times.at<float>((int)i) * w;
wsum += times.at<float>((int)i) * times.at<float>((int)i) * (w + DBL_EPSILON);
}
result = result.mul(1 / wsum);
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.

Patching wsum doesn't look valid.

Patch divide operation instead (Scalar::all() is necessary as we work with multi-channel data):

result = result.mul(1 / (wsum + Scalar::all(DBL_EPSILON)));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@alalek Ah good catch. It technically works, but a warning pops up:

assign OpenCV/MatExpr: processing of multi-channel arrays might be changed in the future: https://github.com/opencv/opencv/issues/16739

Addressed the issue and actually moved the epsilon to where the division occurs. Thank you!

@danielenricocahall danielenricocahall requested a review from alalek May 30, 2021 17:51
@alalek alalek merged commit eb9b5fa into opencv:master May 30, 2021
@alalek alalek mentioned this pull request Jun 4, 2021
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
…-calibration-bug

Fix Robertson Calibration NaN Bug

* add epsilon value for numerical stability in robertson merge

* update test to use range based for loop

* add comment to test

* move the epsilon

* address test comments

fix windows build warnings

fix vector type for tests

update tests

make threshold float

address test comments

fix tests and move epsilon again

* use scalar::all, move epsilon, and remove print
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.

4 participants