From: yuki takehara Date: Tue, 12 Jun 2018 18:05:44 +0000 (+0900) Subject: Merge pull request #11706 from take1014:setTo_Nan_10507 X-Git-Tag: accepted/tizen/6.0/unified/20201030.111113~1^2~617^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=4fe648b15c0cf90e9e47f01454261f731f5e92ea;p=platform%2Fupstream%2Fopencv.git Merge pull request #11706 from take1014:setTo_Nan_10507 * setTo_#10507 * setTo_Nan_10507 * setTo: update check / test for NaNs --- diff --git a/modules/core/src/copy.cpp b/modules/core/src/copy.cpp index 5e7f4a8..e67e58b 100644 --- a/modules/core/src/copy.cpp +++ b/modules/core/src/copy.cpp @@ -463,9 +463,14 @@ static bool ipp_Mat_setTo_Mat(Mat &dst, Mat &_val, Mat &mask) return false; if (dst.depth() == CV_32F) + { for (int i = 0; i < (int)(_val.total()); i++) - if (_val.at(i) < iwTypeGetMin(ipp32f) || _val.at(i) > iwTypeGetMax(ipp32f)) + { + float v = (float)(_val.at(i)); // cast to float + if (cvIsNaN(v) || cvIsInf(v)) // accept finite numbers only return false; + } + } if(dst.dims <= 2) { diff --git a/modules/core/test/test_mat.cpp b/modules/core/test/test_mat.cpp index 18906f8..ad480eb 100644 --- a/modules/core/test/test_mat.cpp +++ b/modules/core/test/test_mat.cpp @@ -1612,6 +1612,32 @@ TEST(Mat, regression_7873_mat_vector_initialize) ASSERT_EQ(2, sub_mat.size[2]); } +TEST(Mat, regression_10507_mat_setTo) +{ + Size sz(6, 4); + Mat test_mask(sz, CV_8UC1, cv::Scalar::all(255)); + test_mask.at(1,0) = 0; + test_mask.at(0,1) = 0; + for (int cn = 1; cn <= 4; cn++) + { + cv::Mat A(sz, CV_MAKE_TYPE(CV_32F, cn), cv::Scalar::all(5)); + A.setTo(cv::Scalar::all(std::numeric_limits::quiet_NaN()), test_mask); + int nans = 0; + for (int y = 0; y < A.rows; y++) + { + for (int x = 0; x < A.cols; x++) + { + for (int c = 0; c < cn; c++) + { + float v = A.ptr(y, x)[c]; + nans += (v == v) ? 0 : 1; + } + } + } + EXPECT_EQ(nans, cn * (sz.area() - 2)) << "A=" << A << std::endl << "mask=" << test_mask << std::endl; + } +} + #ifdef CV_CXX_STD_ARRAY TEST(Core_Mat_array, outputArray_create_getMat) {