Trying to split video frames into RGB component images

edited January 2016 in Library Questions

I'm trying to take a video frame and break out the RGB component information into three separate images. The problem I seem to be having is that when I load more than one set of pixel information, some strange things start to happen. You can see in the code below that when I simply reference the information in the original capture it will actually seem to change that information. I'm not quite getting the loadPixels and updatePixels functions it seems. Here's a simplified version of my sketch using one of the video examples:

import processing.video.*;

Capture cam;

PImage camR;
PImage camG;
PImage camB;

void setup() {
  size(640, 480);
  colorMode(RGB,255);
  frameRate(1);
  String[] cameras = Capture.list();

  if (cameras == null) {
    println("Failed to retrieve the list of available cameras, will try the default...");
    cam = new Capture(this, 640, 480);
  } if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } else {
    println("Available cameras:");
    for (int i = 0; i < cameras.length; i++) {
      println(cameras[i]);
    }

    // The camera can be initialized directly using an element
    // from the array returned by list():
    cam = new Capture(this, cameras[0]);
    // Or, the settings can be defined based on the text in the list
    //cam = new Capture(this, 640, 480, "Built-in iSight", 30);

    // Start capturing the images from the camera
    cam.start();
  }
}

void draw() {
  if (cam.available() == true) {
    cam.read();
  }
  camR = cam;
  camG = cam;
  camB = cam;
  cam.loadPixels();
  camR.loadPixels();
  camG.loadPixels();
  camB.loadPixels();

  for (int i = 0; i < cam.width*cam.height; i++){

    camR.pixels[i] = color(red(cam.pixels[i]));
    camG.pixels[i] = color(green(cam.pixels[i]));
    camB.pixels[i] = color(blue(cam.pixels[i]));
  }
  cam.updatePixels();
  camR.updatePixels();
  camG.updatePixels();
  camB.updatePixels();
  image(cam,0,0,200,150);
  image(camR,200,0,200,150);
  image(camG,200,150,200,150);
  image(camB,200,300,200,150);

}

I'm expecting to see the full RGB image when I run image(cam...), but I end up just getting the greyscale RED channel. The "camG" and "camB" are also displaying the red channel.

Answers

  • edited January 2016 Answer ✓
    • camR, camG , camB & cam are all aliases for the very same Capture object! @-)
    • That is, all of those 4 variables hold the same memory address value (a.K.a. reference or pointer) of the Capture object which was instantiated @ line #29 within setup().
    • Given that a Capture object is also a PImage 1, we can use its get() method in order to grab a cloned copy of it: https://processing.org/reference/PImage_get_.html
    • We can even get() a section outta the original if we pass 4 parameters instead of none! :>
  • Answer ✓

    Lines 42-44 are pointing 3 references at the same copy of the cam object. You need to physically copy the contents to the other three pixel Arrays.

  • Perfect! I forgot about good old get()! Thanks folks!

Sign In or Register to comment.