Pass multiple images to OpenCV

edited February 2014 in Library Questions

Hi, I have OpenCV from https://github.com/atduskgreg/opencv-processing up and running fine. I wondered what the method is to pass multiple images to it?

So, I have a few source images I want to run the face detect on, and I guess the best way to do this is to create one OpenCV object and pass it different sources throughout the draw loop?

For instance, it works likes this:

opencv = new OpenCV(this, "myimage.png");
faces = opencv.detect();

which makes an array of faces (within that image) that I can loop through and do my stuff.

I don't know how to pass it another image, I would guess it's something like this:

opencv.load("anotherImage.png");

But I have no idea. And I'm not really sure where to look for documentation. Any guidance would be really appreciated!

Thanks.

Answers

  • edited February 2014 Answer ✓
    OpenCV[] opencv = OpenCV[12];
    
    void draw(){
    
      for(int i =0; i< opencv.length ; i++){
        String str  = i + ".jpg";
         opencv[i] =  new OpenCV(this , str);
         faces[i] = opencv[i].detect();
       }
    }
    
  • Hi, thanks for the reply. I didn't get notified, so that's why I'm late back!

    That was a real help. There were a couple of bits that caught me out, so I'm posting my version of the code:

    String[] imgs = {"...", "...", "..."};
    int len = imgs.length;
    OpenCV[] opencv = new OpenCV[len];
    Rectangle[] faces = new Rectangle[len];
    
    void draw(){
    for (int i=0; i<opencv.length; i++)
    {
        String str = imgs[i];
        opencv[i] = new OpenCV(this, str);
    
        // This next part was needed as it was returning a NullPointer Exception
        opencv[i].loadCascade(OpenCV.CASCADE_FRONTALFACE);
    
        // Detect the face
        faces = opencv[i].detect();
    
        // Display the image
        image(opencv[i].getInput(), 0, 0);
    }
    
  • Okay try to print the image path in the consol and see if you are getting the correct path. If you are not getting desired path then there would be error in while creating the path. If the error in path creating then there must be some bug in any of these line 1,2, & 9 but it seems correct to me so th eproblem is in image address that you have saved in the String array in line 1. What is it pointing?

    Some points

    1. How would the progam know if it is Jpeg or gif or png image?
    2. If u are trying to retrive the images from the data folder then you just rename them with numbers. It would be eaiser to call.
  • edited March 2014

    Hi, sorry I didn't say, but the code works fine and does what I want it to now.

    I am getting the images from the internet, so the array of images actually looks more like this:

    String[] imgs = { "example.com/image_1.png", "example.com/image_2.png" };
    

    (I had to remove the http part as it got formatted strangely in this forum)

    And it all works as I expect it to.

    Thanks again!

  • edited March 2014

    (I had to remove the http part as it got formatted strangely in this forum)

    Indeed it's a forum bug! :-L But you can create a constant like:

    final String HTTP = "http://";
    

    in order to avoid the bug. Then concatenate it to the rest of the URL when loading images:

    final PImage[] pics = new PImage[imgs.length];
    for (int i = 0; i != imgs.length; ++i)   pics[i] = loadImage(HTTP + imgs[i]);
    

    Since all images come from the same host, I'd even go as far as creating this more complete URL constant: :ar!

    final String HTTP = "http://", URL = HTTP + "example.com/";
    
    final String[] imgs = { "image_1.png", "image_2.png" };
    final PImage[] pics = new PImage[imgs.length];
    
    for (int i = 0; i != imgs.length; ++i)   pics[i] = loadImage(URL + imgs[i]);
    
  • Ah, thanks for the advice. Nice and tidy code.

    The next step is to get the list of images from JSON from the server!

Sign In or Register to comment.