简体   繁体   English

如何将 Java 中的 Matlab 脚本与 arguments 一起使用?

[英]How do I use a Matlab script in Java with arguments?

To call a matlab method in java, I made a code sketch as follows:为了在 java 中调用 matlab 方法,我做了如下代码草图:

Process proc;
    try {
        // Run the process
        proc = Runtime.getRuntime().exec(
            new String[]{
                "matlab -nosplash -nodesktop -r run("C:\Users\felip\Desktop\Projetos\...\teste.m"),
                caminho.concat("sem_reflexo.jpg"),
                caminho.concat("edge.jpg"),
                caminho.concat("labels.pgm"),
                caminho.concat("borders.pgm")"
            }
        );

        proc.waitFor();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

For the above code, I intend to pass as a parameter to a Matlab method, inputs from a Java program that are parameters of image segmentation methods.对于上述代码,我打算将来自 Java 程序的输入作为图像分割方法的参数作为参数传递给 Matlab 方法。 These parameters are dynamic, as the program reads the image directories, that is, the images change one by one until all of them are read.这些参数是动态的,在程序读取图像目录时,图像会一张一张地变化,直到全部读取完毕。

The caminho.concat has the function of concatenating the string to pass the parameter on the same line, according to the image changes in each folder and so on. caminho.concat有串联字符串的function 在同一行传递参数,根据每个文件夹中的图像变化等等。 However, I do not know if it is the best way to do this, considering the construction of the test.m string and passing its parameters.但是,考虑到 test.m 字符串的构造并传递其参数,我不知道这是否是最好的方法。 In short, I am confused about how to do the integration.简而言之,我对如何进行整合感到困惑。

I made a code sketch in Matlab, so that I could call the script to pass the necessary parameters, but I'm not sure of that since I don't have much experience with the language.我在 Matlab 中做了一个代码草图,这样我就可以调用脚本来传递必要的参数,但我不确定,因为我没有太多的语言经验。 The following is the matlab script:以下是 matlab 脚本:

function sh = SuperpixelHierarchy(image, edge, edge_weight, compactness, labels, borders)
    arguments
        image 
        edge
        edge_weight
        compactness
        labels
        borders
    end
    sh = SuperpixelHierarchyMex(image, edge, edge_weight, compactness);
    imgdir =  dir(fullfile(image,'*.jpg'));
    imgname = {imgdir.name};
    imwrite(sh.label, borders);
    color1 = MeanColor(double(image), sh.label);
    imwrite(sh.label, labels);
    fusion = imfuse(color1, sh.label,'ColoChannels', [R G B]);
    imwrite(fusion, imgname+"SUPERPIXELS_LABELS.jpg");
end

I want to make an extração of superpixels from a method called Superpixels Hierarchy (SH) that is written in Matlab.我想通过一种名为 Superpixels Hierarchy (SH) 的方法制作额外的超像素,该方法用 Matlab 编写。 So, I need to use the input parameters to calculate these superpixels and use the outputs from this method as input for the next stages of the Java program...因此,我需要使用输入参数来计算这些超像素,并将此方法的输出用作 Java 程序下一阶段的输入...

Could you help me with the construction of the String and understanding of this integration for the extraction of superpixels as my program?您能否帮助我构建字符串并理解这种用于提取超像素作为我的程序的集成? Since I thank you.既然我谢谢你。

As far as I know, you cannot do what you've suggested directly.据我所知,你不能直接做你建议的事情。 You can instantiate Java objects in Matlab directly though.您可以直接在 Matlab 中实例化 Java 对象。 In that case they share the same JVM and you'd have more options for this back-and-forth using standard programming techniques that are harder or lost by starting a separate Matlab process via the shell.在这种情况下,它们共享相同的 JVM,并且您可以使用标准编程技术来回选择更多选项,这些技术通过 Z2591C98B70119FE6248198B1E 启动单独的 Matlab 进程会更难或丢失。

This would lead to a flow where you wrap you current Java in one or more Java classes, start Matlab (GUI or headless as you prefer), and insatiate one of the wrapper Java objects inside of Matlab. This would lead to a flow where you wrap you current Java in one or more Java classes, start Matlab (GUI or headless as you prefer), and insatiate one of the wrapper Java objects inside of Matlab. These objects can be passed into Matlab functions where the logic in the Matlab code could call methods on the Java objects to get the information that you described as "dynamic" as needed.这些对象可以传递到 Matlab 函数中,其中 Matlab 代码中的逻辑可以根据需要调用 Java 对象上的方法来获取您描述为“动态”的信息。

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

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