batchDistance assert error
When I use batchDistance in OpenCV4Android, I get the following assertion error:
10-16 17:33:22.770: E/cv::error()(30470): OpenCV Error: Assertion failed (_nidx.needed() == (K > 0)) in void cv::batchDistance(const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, int, const cv::_OutputArray&, int, int, const cv::_InputArray&, int, bool), file X:\Dev\git\opencv-2.4\modules\core\src\stat.cpp, line 1771
10-16 17:33:22.775: E/AndroidRuntime(30470): FATAL EXCEPTION: main
10-16 17:33:22.775: E/AndroidRuntime(30470): CvException [org.opencv.core.CvException: X:\Dev\git\opencv-2.4\modules\core\src\stat.cpp:1771: error: (-215) _nidx.needed() == (K > 0) in function void cv::batchDistance(const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, int, const cv::_OutputArray&, int, int, const cv::_InputArray&, int, bool)
I'm using batchDistance as follows:
Mat distances = new Mat();
Mat nidx = new Mat();
assert(nidx.empty());
Core.batchDistance(queryDescriptors,
mTrainDescriptors,
distances,
Core.NORM_HAMMING2,
nidx
);
I've used nidx = new Mat()
because I saw that in C++ the default was noArray()
, which should be equal to an empty Mat. In Java, the method .needed()
doesn't exist, so I don't know what's actually the problem.
Please help me fix this or help me find another way to calculate the pairwise distance of the descriptors in two Mat
's.
Edit: noArray()
doesn't exist under OpenCV4Android and passing in null results in a NullPointerException.
Workaround
This doesn't really solve the issue at hand, but a workaround is to call it as follows:
Core.batchDistance(queryDescriptors,
mTrainDescriptors,
distances,
-1,
nidx,
Core.NORM_L1,
1
);
This circumvents the assertion _nidx.needed() == (K > 0)
because K is set to 1. I don't really know what K represents/means though, so this does not solve the question as I would like to call batchDistance
with as many default parameters as possible (= safest if you don't know the inner workings of a method yet, I think).
Can you pass noArray() instead of nidx?
I've thought of that, but noArray() doesn't seem to exist under Android.
Try to declare just
Mat nidx;
Not possible, Eclipse tells me that the variable has to be initialized. (Can't compile or run until I do that and the only options are null or new Mat(), both give an error.)