Using PROCESSING


OK, I’m not expert but seems this may help some people, so here we go…

Bellow is a short program using PROCESSING to capture a frame over serial port from CTS and display it. One can add some real image processing once data captured or can just use it the way it is.

In this example, image is in YUV format and converted to RGB before displayed on-screen. In case RGB is received, there is no need for conversion.

import processing.serial.*;

int H = 240;
int W = 360;

PImage img = createImage(W, H, RGB);
Serial myPort;

void setup() {
  // change the device details according to your setup
  myPort = new Serial(this, "/dev/cu.SLAB_USBtoUART", 230400);
  size(W, H);
}

void draw() {
  // send frame request
  myPort.write("FY4");
  while (myPort.available () > 0) {
    // prepare a buffer to receive the image 
    // size will be: W * H * bytes/pixel + start header + end header
    byte[] inBuffer = new byte[H*W*4+5];
    // receive data 
    myPort.readBytes(inBuffer);
    // check start frame header (0x46 0x00)
    if ((inBuffer != null) && (inBuffer[0] == 0x46) && (inBuffer[1] == 0)) {
      // load pixels for existing image
      img.loadPixels();
      // process image receive, pixel by pixel
      for (int y=0;y<H;y++) {
        for (int x=0;x<W/2;x++) {
          // retrieve YUV data
          int Y  = inBuffer[y*2*W+x*4+2]   & 0xff;
          int V  = inBuffer[y*2*W+x*4+2+1] & 0xff;
          int Y1 = inBuffer[y*2*W+x*4+2+2] & 0xff;
          int U  = inBuffer[y*2*W+x*4+2+3] & 0xff;

          // convert to RGB for 1st pixel
          int B = 14*(Y - 16) + 20*(U - 128);
          int G = 11*(Y - 16) - 8*(V - 128) - 4*(U - 128);
          int R = 12*(Y - 16) + 16*(V - 128);

          // update pixel
          img.pixels[W*y + x*2] = color (R/10, G/10, B/10);

          // convert to RGB for 2nd pixel
          B = 14*(Y1 - 16) + 20*(U - 128);
          G = 11*(Y1 - 16) - 8*(V - 128) - 4*(U - 128);
          R = 12*(Y1 - 16) + 16*(V - 128);

          // update pixel
          img.pixels[W*y + x*2 + 1] = color (R/10, G/10, B/10);
        }
      }
      // update image on screen
      img.updatePixels();
      image(img, 0, 0);
    }
    // flush remaining data, discard EOF (0x00 0xFF)
    myPort.clear();
  }
}
Advertisements

11 thoughts on “Using PROCESSING

  1. Thanks! I have been trying to do a code in order to generate a random frame and display it, but only the first row of the screen chage their colors. Here my arduino code.


    #define H 120
    #define W 160
    #define COMMAND_W 3

    byte frame[H*W*4+5];
    char inCommand[3];

    void setup()
    {
    Serial.begin(115200);
    for(int k=0;k<(H*W*4+5);k++) // Frame with random pixels
    frame[k]=random(256);
    }

    void loop()
    {
    while (Serial.available()<COMMAND_W); // Wait until the command
    for(int n=0; n0) Serial.read();
    }

    I don’t can’t get what’s wrong! :(

    • Sorry I have paste wrongly the code

      #define H 120
      #define W 160
      #define COMMAND_W 3

      byte frame[H*W*4+5];
      char inCommand[3];

      void setup()
      {
      Serial.begin(115200);
      for(int k=0;k<(H*W*4+5);k++) // Frame with random pixels
      frame[k]=random(256);
      }

      void loop()
      {
      while (Serial.available()<COMMAND_W); // Wait until the command
      for(int n=0; n0) Serial.read();
      }

      • Not sure what you want to achieve, but if you want to generate a frame with random pixels every 100 ms I’ll do it like:


        PImage img = createImage(W, H, RGB);

        void setup() {
        size(W, H);
        }

        void draw(){
        // load the image
        img.loadPixels();
        for (y=0; y<H; y++){
        for (x=0; x<W; x++){
        // define pixel by pixel
        img.pixels[W*y + x] = color (randomR, ramdomG, randomB);
        }
        }
        // to display it
        img.updatePixels();
        image(img, 0, 0);
        delay(100);
        }

        Well, is just the idea, need to define vars, etc.

      • Sorry I have no explain myself properly.
        My goal is to test your processing code.
        So, in order to do it I have write the code of the photo on my Arduino in oder to send through serial random pixels.

      • Sorry you lost me :-) My Arduino “skills” are very rusty but guess this may work. So here we go:


        void setup(){
        Serial.begin(115200);
        }

        void loop(){
        while (Serial.availabe() > 0){
        char c = serial.read();
        // check for proper frame request, let's say 'F'
        if (c != 'F'){
        // if is not what we are looking for just drop it and cycle again
        return 0;
        }
        }

        // send start of frame
        serial.write(0x46);
        serial.write(0x00);
        // generate 160 x 120 x 2 bytes with random values
        for(i=0; i<W*H*2; i++){
        serial.write(random(256));
        }
        // send end of frame
        serial.write(0x00);
        serial.write(0xFF);
        }

      • Yes, your code do the same I was trying to do, and the result is the same weird think.

        The pixels which change their color are the ones on the first row, the others keep green all the time.

    • I don’t know waht my code doesn’t paste properly!
      Here a photo of the code, sorry por all the posts, I can’t erase the others!

  2. And I also have changed in Processing my details.

    myPort = new Serial(this, “COM5”, 115200);

    and

    int H = 120;
    int W = 160;

    All the other Processing code is the same

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s