curiouscupcake
curiouscupcake

Reputation: 1277

OpenCV C++ read image and patch NaN Error: Assertion failed (_a.depth() == CV_32F) in patchNaNs

I have the following code which read an RGB JPEG image, however, the image sometimes has NaN value so I want to patch it with 0:

cv::Mat model = cv::imread(path);
cv::patchNaNs(model, 0.0);

I keep getting the following error message:

OpenCV(3.4.5) Error: Assertion failed (_a.depth() == CV_32F) in patchNaNs, file 
..../core/src/mathfuncs.cpp, line 1597
terminate called after throwing an instance of 'cv::Exception'

What does this error message mean?

Upvotes: 0

Views: 626

Answers (1)

rayryeng
rayryeng

Reputation: 104545

The function requires that the input be 32-bit floating-point. You can use cv::convertTo to convert your image to 32-bit:

cv::Mat model = cv::imread(path);
cv::Mat output;
model.convertTo(output, CV_32F);
cv::patchNaNs(model, 0.0);

Depending on your use case, you may want to normalise the input values so that they are in the range of [0-1] for your uses. Because of the way you named your image (i.e. model), I'm assuming you want to create some classification model, so you can specify an additional scale factor to cv::convertTo to scale the values after conversion. Usually images are 8-bit unsigned integer so dividing by 255 can be done here:

cv::Mat model = cv::imread(path);
cv::Mat output;
model.convertTo(output, CV_32F, 1.0f / 255.0f);
cv::patchNaNs(model, 0.0);

Upvotes: 2

Related Questions