I am attempting to test some ideas for java robot code using simulation. The code in question uses opencv. The code compiles without error, but when simulation starts a run-time error occurs:
Exception in thread "Thread-1" java.lang.UnsatisfiedLinkError: 'long org.opencv.core.Mat.n_Mat()'
at org.opencv.core.Mat.n_Mat(Native Method)
at org.opencv.core.Mat.<init>(Mat.java:23)
at frc.robot.subsystems.CameraSubsystem.lambda$0(CameraSubsystem.java:26)
at java.base/java.lang.Thread.run(Thread.java:833)
The error occurs at the point where a new Mat() is being performed. I’ve tried this on both Windows and Linux (Ubuntu 22.04).
Interestingly enough, there was a CD post in 2018 (VSCode Testing Classes Using Opencv - #3 by Bart_Kerfeld) dealing with the same error; the workarounds then are likely no longer applicable. Indeed the thread ended with a remark from @Thad_House that I interpret as saying that in the 2019 wpilib things were set up so opencv would work in simulation.
I haven’t been able to try this code on a RoboRio yet – I’ll update tonight as to whether the problem also occurs there.
in the IntermediateVision Robot.java from it’s original location to just after the line with camera.setResolution it then gets the unsatisfied link error, just as in my code. And if in my code I move
Mat mat = new Mat();
to after the line with CvSink cvSink = CameraServer.getVideo(); it no longer crashes.
This does not appear to be strictly a timing issue as a Thread.sleep(1) before the new Mat doesn’t fix it.
It’s much too late to continue tonight. @Joe_Ross, I really appreciate the pointer to the IntermediateVision project.
[This was on Ubuntu 22.04. I’ll try Windows tomorrow but I don’t expect different.]
Creating a CvSink pulls in CameraServerCvJNI, which loads the opencv native libraries. To maximize the amount of memory available to robot programs that do not use OpenCV, we don’t automatically load the OpenCV native libraries at the start of every robot program.