0

It's been a long time since I'v used Java, and I've never used arguments before. So I am using OpenCV Template Matching. I am following this example. You can look at the link if you want to. I do not think it is necessary to copy and past the whole thing.

When I run the project, it prints;

Not enough parameters
Program arguments:
<image_name> <template_name> [<mask_name>]

So the program needs least 2 parameters: Source and template image files. But I don't know how pass files to the program. Does it need the file-path or the file itself?

This is the method:

public void run(String[] args) {
        if (args.length < 2) {
            System.out.println("Not enough parameters");
            System.out.println("Program arguments:\n<image_name> <template_name> [<mask_name>]");
            System.exit(-1);
        }
        img = Imgcodecs.imread(args[0], Imgcodecs.IMREAD_COLOR);
        templ = Imgcodecs.imread(args[1], Imgcodecs.IMREAD_COLOR);
        if (args.length > 2) {
            use_mask = true;
            mask = Imgcodecs.imread(args[2], Imgcodecs.IMREAD_COLOR);
        }
        if (img.empty() || templ.empty() || (use_mask && mask.empty())) {
            System.out.println("Can't read one of the images");
            System.exit(-1);
        }
        matchingMethod();
        createJFrame();
}

This is the main method:

public class MatchTemplateDemo {
    public static void main(String[] args) {
        // load the native OpenCV library
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        // run code
        new MatchTemplateDemoRun().run(args);
    }
}
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
Lacrymae
  • 157
  • 1
  • 17

2 Answers2

1

Yes, you need to pass the file path. If you run it from your command prompt, it will look something like this:

java MatchTemplateDemo "C:/.../Image.png" "C:/.../Template.png"

If you are using an IDE, then you can use the following links to run your program with arguments:

  1. Eclipse: Eclipse command line arguments
  2. IntelliJ IDEA: How do you input commandline argument in IntelliJ IDEA?
  3. NetBeans: Netbeans how to set command line arguments in Java
Cardinal System
  • 2,749
  • 3
  • 21
  • 42
1

Imgcodecs.imread accepts a string which contains the full path or a relative path to an existing image. For example, under Windows, the full path on a image would be C:\\myfolder\\myimage.jpg. The relative path can be only myimage.jpg, but this assumes that the program was executed from the same folder where image can be found.

If you are trying to run your code from an IDE, there can be also ways of giving arguments to it. You should research this depending on what IDE are you using.

Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40