1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-31 08:13:04 +04:00

Merge pull request #23098 from savuor:nanMask

finiteMask() and doubles for patchNaNs() #23098

Related to #22826
Connected PR in extra: [#1037@extra](https://github.com/opencv/opencv_extra/pull/1037)

### TODOs:
- [ ] Vectorize `finiteMask()` for 64FC3 and 64FC4

### Changes

This PR:
* adds a new function `finiteMask()`
* extends `patchNaNs()` by CV_64F support
* moves `patchNaNs()` and `finiteMask()` to a separate file

**NOTE:** now the function is called `finiteMask()` as discussed with the OpenCV core team

### Pull Request Readiness Checklist

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

- [x] I agree to contribute to the project under Apache 2 License.
- [x] 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
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Rostislav Vasilikhin
2023-11-09 08:32:47 +01:00
committed by GitHub
parent 34f34f6227
commit 53aad98a1a
15 changed files with 1190 additions and 138 deletions
+12 -5
View File
@@ -302,6 +302,9 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Mat_<Vec4b> img = image.getMat();
Mat goods;
finiteMask(points, goods);
Range range(0, sz.height);
const int nstripes = -1;
parallel_for_(range, [&](const Range&)
@@ -311,6 +314,7 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Vec4b* imgRow = img[y];
const ptype* ptsRow = points[y];
const ptype* nrmRow = normals[y];
const uchar* goodRow = goods.ptr<uchar>(y);
for (int x = 0; x < sz.width; x++)
{
@@ -319,7 +323,7 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Vec4b color;
if (cvIsNaN(p.x) || cvIsNaN(p.y) || cvIsNaN(p.z) )
if ( !goodRow[x] )
{
color = Vec4b(0, 32, 0, 0);
}
@@ -357,6 +361,11 @@ void renderPointsNormalsColors(InputArray _points, InputArray, InputArray _color
Points points = _points.getMat();
Colors colors = _colors.getMat();
Mat goods, goodp, goodc;
finiteMask(points, goodp);
finiteMask(colors, goodc);
goods = goodp & goodc;
Mat_<Vec4b> img = image.getMat();
Range range(0, sz.height);
@@ -366,18 +375,16 @@ void renderPointsNormalsColors(InputArray _points, InputArray, InputArray _color
for (int y = range.start; y < range.end; y++)
{
Vec4b* imgRow = img[y];
const ptype* ptsRow = points[y];
const ptype* clrRow = colors[y];
const uchar* goodRow = goods.ptr<uchar>(y);
for (int x = 0; x < sz.width; x++)
{
Point3f p = fromPtype(ptsRow[x]);
Point3f c = fromPtype(clrRow[x]);
Vec4b color;
if (cvIsNaN(p.x) || cvIsNaN(p.y) || cvIsNaN(p.z)
|| cvIsNaN(c.x) || cvIsNaN(c.y) || cvIsNaN(c.z))
if ( !goodRow[x] )
{
color = Vec4b(0, 32, 0, 0);
}
+3 -11
View File
@@ -102,16 +102,8 @@ static void extendPyrMaskByPyrNormals(const std::vector<UMat>& pyramidNormals,
UMat maski = pyramidMask[i];
UMat normali = pyramidNormals[i];
UMat validNormalMask;
// NaN check
cv::compare(normali, normali, validNormalMask, CMP_EQ);
CV_Assert(validNormalMask.type() == CV_8UC4);
std::vector<UMat> channelMasks;
split(validNormalMask, channelMasks);
UMat tmpChMask;
cv::bitwise_and(channelMasks[0], channelMasks[1], tmpChMask);
cv::bitwise_and(channelMasks[2], tmpChMask, tmpChMask);
cv::bitwise_and(maski, tmpChMask, maski);
finiteMask(normali, validNormalMask);
cv::bitwise_and(maski, validNormalMask, maski);
}
}
}
@@ -727,7 +719,7 @@ void computeCorresps(const Matx33f& _K, const Mat& rt,
{
float ddst = depthDst_row[udst];
if (maskDst_row[udst] && !cvIsNaN(ddst))
if (maskDst_row[udst])
{
float transformed_ddst = static_cast<float>(ddst * (KRK_inv6_u1[udst] + KRK_inv7_v1_plus_KRK_inv8[vdst]) + ktinv.z);
+4 -6
View File
@@ -16,11 +16,8 @@ void dilateFrame(Mat& image, Mat& depth)
CV_Assert(depth.type() == CV_32FC1);
CV_Assert(depth.size() == image.size());
Mat mask(image.size(), CV_8UC1, Scalar(255));
for(int y = 0; y < depth.rows; y++)
for(int x = 0; x < depth.cols; x++)
if(cvIsNaN(depth.at<float>(y,x)) || depth.at<float>(y,x) > 10 || depth.at<float>(y,x) <= FLT_EPSILON)
mask.at<uchar>(y,x) = 0;
Mat mask;
inRange(depth, FLT_EPSILON, 10, mask);
image.setTo(255, ~mask);
Mat minImage;
@@ -726,7 +723,8 @@ TEST(RGBD_Odometry_WarpFrame, nansAreMasked)
ASSERT_EQ(0, rgbDiff);
Mat goodVals = (w.warpedDepth == w.warpedDepth);
Mat goodVals;
finiteMask(w.warpedDepth, goodVals);
double l2diff = cv::norm(w.dstDepth, w.warpedDepth, NORM_L2, goodVals);
double lidiff = cv::norm(w.dstDepth, w.warpedDepth, NORM_INF, goodVals);
+15 -34
View File
@@ -295,6 +295,9 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Points points = _points.getMat();
Normals normals = _normals.getMat();
Mat goods;
finiteMask(points, goods);
Mat_<Vec4b> img = image.getMat();
Range range(0, sz.height);
@@ -306,6 +309,7 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Vec4b* imgRow = img[y];
const ptype* ptsRow = points[y];
const ptype* nrmRow = normals[y];
const uchar* goodRow = goods.ptr<uchar>(y);
for (int x = 0; x < sz.width; x++)
{
@@ -314,7 +318,7 @@ void renderPointsNormals(InputArray _points, InputArray _normals, OutputArray im
Vec4b color;
if (cvIsNaN(p.x) || cvIsNaN(p.y) || cvIsNaN(p.z))
if (!goodRow[x])
{
color = Vec4b(0, 32, 0, 0);
}
@@ -352,6 +356,11 @@ void renderPointsNormalsColors(InputArray _points, InputArray, InputArray _color
Points points = _points.getMat();
Colors colors = _colors.getMat();
Mat goods, goodc, goodp;
finiteMask(points, goodp);
finiteMask(colors, goodc);
goods = goodp & goodc;
Mat_<Vec4b> img = image.getMat();
Range range(0, sz.height);
@@ -361,18 +370,16 @@ void renderPointsNormalsColors(InputArray _points, InputArray, InputArray _color
for (int y = range.start; y < range.end; y++)
{
Vec4b* imgRow = img[y];
const ptype* ptsRow = points[y];
const ptype* clrRow = colors[y];
const uchar* goodRow = goods.ptr<uchar>(y);
for (int x = 0; x < sz.width; x++)
{
Point3f p = fromPtype(ptsRow[x]);
Point3f c = fromPtype(clrRow[x]);
Vec4b color;
if (cvIsNaN(p.x) || cvIsNaN(p.y) || cvIsNaN(p.z)
|| cvIsNaN(c.x) || cvIsNaN(c.y) || cvIsNaN(c.z))
if (!goodRow[x])
{
color = Vec4b(0, 32, 0, 0);
}
@@ -587,33 +594,6 @@ void boundingBoxGrowthTest(bool enableGrowth)
}
static Mat nanMask(Mat img)
{
int depth = img.depth();
Mat mask(img.size(), CV_8U);
for (int y = 0; y < img.rows; y++)
{
uchar *maskRow = mask.ptr<uchar>(y);
if (depth == CV_32F)
{
Vec4f *imgrow = img.ptr<Vec4f>(y);
for (int x = 0; x < img.cols; x++)
{
maskRow[x] = (imgrow[x] == imgrow[x]) * 255;
}
}
else if (depth == CV_64F)
{
Vec4d *imgrow = img.ptr<Vec4d>(y);
for (int x = 0; x < img.cols; x++)
{
maskRow[x] = (imgrow[x] == imgrow[x]) * 255;
}
}
}
return mask;
}
template <typename VT>
static Mat_<typename VT::value_type> normalsErrorT(Mat_<VT> srcNormals, Mat_<VT> dstNormals)
{
@@ -725,8 +705,9 @@ void regressionVolPoseRot()
split(uptsRot, ptsRotCh);
Mat maskPts0 = ptsCh[2] > 0;
Mat maskPtsRot = ptsRotCh[2] > 0;
Mat maskNrm0 = nanMask(mnrm);
Mat maskNrmRot = nanMask(mnrmRot);
Mat maskNrm0, maskNrmRot;
finiteMask(mnrm, maskNrm0);
finiteMask(mnrmRot, maskNrmRot);
Mat maskPtsDiff, maskNrmDiff;
cv::bitwise_xor(maskPts0, maskPtsRot, maskPtsDiff);
cv::bitwise_xor(maskNrm0, maskNrmRot, maskNrmDiff);