#76 RGB information from camera

closed-accepted
nobody
None
5
2011-05-24
2011-02-07
Klaus
No

Greetings!
I was playing with Example3.java, from java swig for yarp, it deals with getting images from iCub simulator's camera.
Right after I use the grabber:

grabber.getImage(img);
int w = img.width();
int h = img.height();
System.out.println("Got a " + w + "x" + h + " image");

I would like to have access to the RGB values from each pixel. So, after taking a look at the FindLocation.cpp example, I tried this:

System.out.println("Pixel RGB: "+img.pixel(w-1,h-1));

Which produced:
Pixel RGB: yarp.PixelRgb@1f1fba0

And then this:
System.out.println("Pixel: R: "+img.pixel(w-1,h-1).getR()+" G: "+img.pixel(w-1,h-1).getG()+" B: "+img.pixel(w-1,h-1).getB());

Which produced:
Pixel: R: 0 G: 0 B: 0

Am I doing it wrong?

My intention is not only having access to this information but also saving it as an image file in disk (bmp, png, etc)

Best regards,

Klaus

Discussion

  • Klaus
    Klaus
    2011-03-01

    Just closing the track. The following code (Thanks paul) seems to work:
    Notice how it uses "ImageRgb img = port.read();" instead of "grabber.getImage(img);" to acquire images.

    //---------
    import yarp.Network;
    import yarp.ImageRgb;
    import yarp.PixelRgb;
    import yarp.BufferedPortImageRgb;

    class ICubCameraDemo {
    public static void main(String[] args) {
    System.loadLibrary("jyarp");
    Network net = new Network();
    BufferedPortImageRgb port = new BufferedPortImageRgb();
    if (!port.open("...")) {
    System.out.println("Could not start reader");
    System.exit(1);
    }
    if (!net.connect("/icubSim/cam/left",port.getName().c_str())) {
    System.out.println("Could not find camera");
    System.exit(1);
    }
    // grab 30 images
    for (int i=0; i<30; i++) {
    ImageRgb img = port.read();
    if (img == null) break;
    int w = img.width();
    int h = img.height();
    System.out.println("Got a " + w + "x" + h + " image");
    PixelRgb pix = img.pixel(w/2,h/2);
    System.out.println("Pixel -- R: "+pix.getR()+" G:
    "+pix.getG()+" B: "+pix.getB());
    }
    }
    }
    //--------

     
  • Klaus
    Klaus
    2011-03-01

    • status: open --> open-accepted
     
  • Klaus
    Klaus
    2011-05-24

    Just to close this thread, I got this answer by email and it really helped. :)
    cheers

    -- Email ----------------
    Your code looks fine. If you are following Example3.java, and haven't
    switched from using the "test_grabber" source, that might explain why
    you see a black pixel. The "test_grabber" test image is mostly black,
    with RGB=(0,0,0).

    The following works for me for reading from one of the simulator's cameras:

    ----

    import yarp.Network;
    import yarp.ImageRgb;
    import yarp.PixelRgb;
    import yarp.BufferedPortImageRgb;

    class ICubCameraDemo {
    public static void main(String[] args) {
    System.loadLibrary("jyarp");
    Network net = new Network();
    BufferedPortImageRgb port = new BufferedPortImageRgb();
    if (!port.open("...")) {
    System.out.println("Could not start reader");
    System.exit(1);
    }
    if (!net.connect("/icubSim/cam/left",port.getName().c_str())) {
    System.out.println("Could not find camera");
    System.exit(1);
    }
    // grab 30 images
    for (int i=0; i<30; i++) {
    ImageRgb img = port.read();
    if (img == null) break;
    int w = img.width();
    int h = img.height();
    System.out.println("Got a " + w + "x" + h + " image");
    PixelRgb pix = img.pixel(w/2,h/2);
    System.out.println("Pixel -- R: "+pix.getR()+" G:
    "+pix.getG()+" B: "+pix.getB());
    }
    }
    }

     
  • Klaus
    Klaus
    2011-05-24

    • status: open-accepted --> closed-accepted