简体   繁体   中英

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?

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 (ie 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM