I believe that modifying the features2d module (FeatureDetector class or any other classes from features2d_manual.hpp) to include methods from OpenCV Contrib modules is less attractive because it leads to a cyclical relationship between the OpenCV core and extensions (which may be free or experimental). There is another way to fix this problem without affecting feature2d classes. Making changes to xfeatures2d CMakeLists.txt, as described here , will generate java wrappers for SIFT and SURF - opencv-310.jar now has the org.opencv.xfeatures2d package. Some fixes were needed in / opencv / modules / java / generator / gen _java.py. Namely, insert 2 lines as shown below:
def addImports(self, ctype): if ctype.startswith('vector_vector'): self.imports.add("org.opencv.core.Mat") self.imports.add("org.opencv.utils.Converters") self.imports.add("java.util.List") self.imports.add("java.util.ArrayList") self.addImports(ctype.replace('vector_vector', 'vector')) elif ctype.startswith('Feature2D'): #added self.imports.add("org.opencv.features2d.Feature2D") #added elif ctype.startswith('vector'): self.imports.add("org.opencv.core.Mat") self.imports.add('java.util.ArrayList') if type_dict[ctype]['j_type'].startswith('MatOf'): self.imports.add("org.opencv.core." + type_dict[ctype]['j_type']) else: self.imports.add("java.util.List") self.imports.add("org.opencv.utils.Converters") self.addImports(ctype.replace('vector_', ''))
After these changes, wrappers are created successfully. However, the main problem remains how to use these wrappers from Java)). For example, SIFT.create () gives a pointer to a new SIFT class, but a call to any class method (for example, detect ()) will cause Java to crash. I also noticed that using MSER.create () directly from Java results in the same crash.
So, it seems the problem is isolated by how Feature2D.create () methods are wrapped in Java. The solution is as follows (again, by modifying / opencv / modules / java / generator / gen _java.py):
Find the line:
ret = "%(ctype)s* curval = new %(ctype)s(_retval_);return (jlong)curval->get();" % { 'ctype':fi.ctype }
Replace it with the following:
ret = "%(ctype)s* curval = new %(ctype)s(_retval_);return (jlong)curval;" % { 'ctype':fi.ctype }
Restore opencv. That is, all create () methods will work correctly for all children of the Feature2D class, including experimental and non-free methods. FeatureDescriptor / DescriptorExtractor wrappers may be deprecated. I find Feature2D much easier to use.
BUT! I am not sure if the proposed fix is ββsafe for other OpenCV modules. Is there a scenario where (jlong) curval needs to be dereferenced? It looks like the same fix has already been proposed here .
Alex b
source share