How to display part of a captured video frame

edited May 2016 in How To...

What's the best way to capture a frame of video from an attached camera and then display an area of that frame? I assume that when capturing a frame from video, the whole frame is captured and placed in the sketch's "data" directory.

Say the sketch's size is 640 x 480 pixels, so the capture frame is that size as well. Now I want to display, say, the portion of that image (100, 100) upper left to (360, 245) lower right to the sketch window at location (150, 150) upper left?

Thanks for any suggestions!

George Roland

Using Processing v.2.0.3 MacBookPro 15" OS X 10.8.5, Fire-i camera



  • I think the get() function will do this. You need to set up a PImage variable to hold the cropped part of the video frame (PImage cropPic) then call:

    cropPic = sourceImage.get(srcX, srcY, srcWidth, srcHeight);
    image(cropPic, destX, destY, destWidth, destHeight);

    George Roland

  • Hold it right there!

    get() is a rather in-efficient algorithm, so I dug around in the docs for you, and i found a better way of doing this. I suggest you use the following code instead:

    import java.awt.image.*;
    PImage load = loadImage("Galaxy.jpg"); //"Galaxy.jpg" was my sample image.
    PImage display;
    BufferedImage in = (BufferedImage) load.getImage();
    BufferedImage out = in.getSubimage(100, 100, 260, 145);
    display = new PImage(out);
    size(640, 480);
    image(display, 100, 100);

    To test, just insert the above code into a blank Processing sketch. If you need an explanation of what is happening, fell free to ask.

    -- MenteCode

  • By "in-efficient", I mean that it'll take up more space. The code you would've written using the get() method would probably be 3x the size of this one.

Sign In or Register to comment.