Quantcast
Channel: Arduino - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 747

get color average from picture parts and save values in an array to send via serial

$
0
0

Hi! I am using part of a code I've found here in the forum ( Get the average RGB from pixels from bourdonprince and GoToLoop) that gets the color average values and display squares like big pixels on screen.

Now I want to use this values to set the pixels colors of a RGB LED strip.

The problem is that when I separate and read (at the processing console) de values of the color for each pixel, I get almost all my pixels with 0 for red... But the squares drawn in the screen seems OK, only the values I try to save to the matrix for latter use that seems odd.

Below is my code ( with thanks to bourdonprince and GoToLoop):

import processing.video.*;
Capture webcam;

import processing.serial.*;
Serial myPort;

float[][] rx;
float[][] gx;
float[][] bx;

void setup() {
  size(440, 320);

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  rx = new float[16][15];
  gx = new float[16][15];
  bx = new float[16][15];

  webcam = new Capture(this, 320, 240);
  //String[] devices = Capture.list();
  //println(devices);
  webcam.start();
}

void draw() {

  if (webcam.available() == true) {
    webcam.read();
    image(webcam, 0, 0, width, height);
    saveFrame("/data/image.jpg");
  }

  delay(100);

  PImage img = loadImage("image.jpg");
  int detail = 30; // tamanho do quadrado

  int count_i = 0;
  int count_j = 0;


  for (int j=0; j< height; j+=detail) {

    count_i = 0;

    for (int i=0; i < width; i+=detail) {
      PImage newImg = img.get(i, j, detail, detail);
      noStroke();
      color cor = extractColorFromImage(newImg);
      fill(cor);
      rx[count_i][count_i]= red(cor);
      gx[count_i][count_j]= green(cor);
      bx[count_i][count_j]= blue(cor);

      count_i++;
    }
    count_j++;
  }


  for (int i=0; i<15; i++)
  {
    for (int j=0; j<11; j++)
    {
      println((i) + "," + (j) + "," + int( rx[i][j] )+ "," + int (gx[i][j]) + "," + int (bx[i][j]));
      //delay(100);
      myPort.write((i) + "," + (j) + "," + int( rx[i][j] )+ "," + int (gx[i][j]) + "," + int (bx[i][j])  + "\n");
      delay(50);
    }
  }

  delay(2000);
}



//função para extrair a cor média em um recorte
color extractColorFromImage(PImage img) {
  img.loadPixels();
  color r = 0, g = 0, b = 0;

  for (final color c : img.pixels) {
    r += c >> 020 & 0xFF;
    g += c >> 010 & 0xFF;
    b += c        & 0xFF;
  }

  r /= img.pixels.length;
  g /= img.pixels.length;
  b /= img.pixels.length;

  return color(r, g, b);
}

Viewing all articles
Browse latest Browse all 747

Latest Images

Trending Articles



Latest Images