简体   繁体   English

如何将 Mat (opencv) 转换为 INDArray (DL4J)?

[英]How to convert Mat (opencv) to INDArray (DL4J)?

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.我正在使用一些图像分类并尝试结合 OpenCv 3.2.0 和 DL4J。 I knew DL4J also included Opencv, but i think it's useless.我知道 DL4J 也包括 Opencv,但我认为它没用。

Can anyone help me, how to convert into INDArray ?任何人都可以帮助我,如何转换为 INDArray ? I tried to read some issue here and Class NativeImageLoader from DL4J has provide the method, asMatrix(Mat image).我试图在这里阅读一些问题并且来自 DL4J 的 Class NativeImageLoader 提供了方法 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) java:没有找到适合 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.感谢您的关注。

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. DL4J不包括OpenCV本机库,因为该库必须位于JAVA_HOME目录或您可以在代码中精确定位的其他位置。 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.如果你想使用DL4J中包含的opencv的特性,你必须首先下载你使用的DL4J版本中包含的opencv版本。 After downloading the opencv library, The opencv native library is located in下载opencv库后,opencv原生库位于

  • opencv/build/java/x64/opencv_javaVERSION_OF_OPENCV.dll for 64 bits architectures or in opencv/build/java/x64/opencv_javaVERSION_OF_OPENCV.dll用于 64 位架构或在
  • opencv/build/java/x86/opencv_javaVERSION_OF_OPENCV.dll for 32 bits architectures. opencv/build/java/x86/opencv_javaVERSION_OF_OPENCV.dll用于 32 位架构。
  1. You can copy the native library in the java home and do this in your code:您可以在 java home 中复制本机库并在您的代码中执行此操作:
 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. PATH是定位库的路径, OPENCV_VERSION是您使用的 opencv 版本。

You can adopt the same approach in an other java project based on opencv您可以在其他基于opencv 的java 项目中采用相同的方法

You can try org.datavec.image.loader.NativeImageLoader 您可以尝试org.datavec.image.loader.NativeImageLoader

public INDArray asMatrix(Mat image) throws IOException public INDArray asMatrix(Mat image)抛出IOException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM