Ask Your Question
1

How to use OpenCV 4 in Java on Raspberry PI (armv6 and armv7 Raspian)?

asked 2020-07-03 18:13:53 -0600

George Profenza gravatar image

updated 2020-07-04 12:45:06 -0600

Please note this is a cross-post also present on stackoverflow, but I would also like the expertise of this wonderful expert community.

This question is very similar to this one however it's on Linux ARM ( Raspberry Pi).

I've compiled OpenCV 4.4.0 from source along with the Java bindings and tried something like this hack (that worked on Windows):

import org.opencv.core.*;

public class CVTest {

        public static void main(String[] args) {
                System.out.println("setup");
//              loading the typical way fails :(
//              System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

                System.load("/home/pi/eclipse/CVTest/lib/libopencv_core.so");
////System.load("/home/pi/opencv/build/lib/libopencv_core.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_imgproc.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_imgcodecs.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_img_hash.so");
////System.load("/home/pi/eclipse/CVTest/lib/opencv_core.so");//videoio_ffmpeg440_64.dll
//System.load("/home/pi/eclipse/CVTest/lib/libopencv_videoio.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_photo.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_xphoto.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_flann.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_features2d.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_calib3d.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_phase_unwrapping.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_structured_light.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_xfeatures2d.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_video.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ximgproc.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_aruco.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_bgsegm.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_bioinspired.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_objdetect.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_face.so");

System.load("/home/pi/eclipse/CVTest/lib/libopencv_dnn.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_tracking.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_plot.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ml.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_ml.so");
System.load("/home/pi/eclipse/CVTest/lib/libopencv_text.so");
// crashes here!
System.load("/home/pi/eclipse/CVTest/lib/libopencv_java440.so");


Mat m = Mat.eye(new Size(3,3), CvType.CV_8UC1);
                System.out.println("done");
        }

}

However, depending on the Raspberry Pi, I get different crashes at the same line, loading libopencv_java440 (after the other dependant libraries have loaded):

On a Raspberry Pi 3B running Raspian stretch I get errors like this one:

#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGBUS (0x7) at pc=0x6360f644, pid=9730, tid=0x64eba470
#
# JRE version: Java(TM) SE Runtime Environment (8.0_202-b08) (build 1.8.0_202-b08)
# Java VM: Java HotSpot(TM) Client VM (25.202-b08 mixed mode linux-arm )
# Problematic frame:
# C  [libopencv_core.so+0x258644]  cv::Ptr<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >::~Ptr()+0x38
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java ...
(more)
edit retag flag offensive close merge delete

Comments

you could at least get rid of all the opencv_xxx.so runtime dependancies by using

cmake -DBUILD_SHARED_LIBS=OFF

(which will link all opencv modules statically into libopencv_java440.so)

berak gravatar imageberak ( 2020-07-04 02:19:15 -0600 )edit

@berak thank you for the suggestion. I did try that: unfortunately I still get segfault. I'll try disabling NEON/VFP3 optimizations first, and if that doesn't work I'll try upgrading g++ and see if that helps

George Profenza gravatar imageGeorge Profenza ( 2020-07-04 14:02:20 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-07-17 14:53:12 -0600

George Profenza gravatar image

The solution was to reinstall Raspian OS which came with an updated c++ compiler. Details here

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-07-03 18:13:53 -0600

Seen: 1,443 times

Last updated: Jul 17 '20