How to convert Mat (opencv) to INDArray (DL4J)?

Viewed 1152

I hope anyone can help me to fix this task. I'm working with some Image Classification and tried to combine OpenCv 3.2.0 and DL4J. I knew DL4J also included Opencv, but i think it's useless.

Can anyone help me, how to convert into INDArray ? I tried to read some issue here and Class NativeImageLoader from DL4J has provide the method, asMatrix(Mat image). But when i tried to runing it i got an error

java: no suitable method found for asMatrix(org.opencv.core.Mat)

I don't know how to fix it my code, or maybe there's something wrong with my code. Can anyone help me to find out the problem ?

Btw here's my code :

Mat imgMat = Imgcodecs.imread("C:\\Pictures\\image.jpg");
INDArray image = loader.asMatrix(imgMat);

Thank you for your kind attention.

2 Answers

DL4J does not include the OpenCV native library because the library must be located in the JAVA_HOME directory or in an other location where you can precise in your code. If you want to use the features of opencv included in DL4J, you must firstly download the version of opencv included in the version of DL4J you are using. After downloading the opencv library, The opencv native library is located in

  • opencv/build/java/x64/opencv_javaVERSION_OF_OPENCV.dll for 64 bits architectures or in
  • opencv/build/java/x86/opencv_javaVERSION_OF_OPENCV.dll for 32 bits architectures.
  1. You can copy the native library in the java home and do this in your code:
 System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
 Mat imgMat = Imgcodecs.imread("C:\\Pictures\\image.jpg");
 INDArray image = loader.asMatrix(imgMat);
  1. Or you can use this approach which is the best
System.load("PATH/opencv_javaOPENCV_VERSION.dll");
Mat imgMat = Imgcodecs.imread("C:\\Pictures\\image.jpg");
INDArray image = loader.asMatrix(imgMat);

PATH is the path where to locate the library and OPENCV_VERSION is the opencv version you use.

You can adopt the same approach in an other java project based on opencv

Related