简体   繁体   中英

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:

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. 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. 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. 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. The following is the matlab script:

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. 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...

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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