将rgb值存储在数组中

时间:2014-08-26 17:11:57

标签: arrays processing rgb

我无法将我的rgb图像值存储在二维数组中。如何在一个2d数组中存储3个值。最重要的是为什么返回这些值,因为我得到的错误不能从float [] []转换为float []。

PImage img;

void setup()
{
  img = loadImage("leaf.jpg");
  size(img.width,img.height); 
}

void draw(){

  background(0);
  float[] m = storedPixels();
  println(m);
}  

float[] storedPixels(){ 

  int w = img.width;
  int h = img.height;
  float [][] c = new float[h][w];

  img.loadPixels();
  for(int y = 0; y < img.height; y++){
    for(int x = 0; x < img.width; x++){
      int loc = x + y * img.width;

      float r = red(img.pixels[loc]);       //Finds the amount of red is at a specific pixel.
      float g = green(img.pixels[loc]);
      float b = blue(img.pixels[loc]);

      c[y][x] = {r,g,b};
    }
  }
  img.updatePixels();

  return c;

}

2 个答案:

答案 0 :(得分:1)

您可以创建一个类来将数据组合在一起,或者使用Processing的颜色几乎是数据类型(尽管颜色会再次组合组件),或者甚至使用PVector将它们存储在一起new PVector (r,g,b),并使用点表示法检索pv.x = r | pv.y = g | pv.z = b。 Processing中的color很简单,其中组件的存储方式类似于AAAARRRRGGGGBBBB。

这里有一个使用PVector的样本

PImage img;
PVector[][] m;
int i, j;

void setup()
{
  while (img==null) {
    img = loadImage("http://upload.wikimedia.org/wikipedia/en/b/b1/Indica_leaf.jpg");
  }
  size(img.width, img.height); 
  m = storedPixels();
}

void draw() {
  i =  (i + 1) % (width-1);
  j =  (j + 1) % (height-1);

  background(color(m[i][j].x, m[i][j].y, m[i][j].z));
}  

PVector[][] storedPixels() { 

  int w = img.width;
  int h = img.height;
  PVector [][] c = new PVector[w][h];

  img.loadPixels();
  for (int y = 0; y < img.height; y++) {
    for (int x = 0; x < img.width; x++) {
      int loc = x + y * img.width;

      float r = red(img.pixels[loc]);       //Finds the amount of red is at a specific pixel.
      float g = green(img.pixels[loc]);
      float b = blue(img.pixels[loc]);

      c[x][y] = new PVector(r, g, b);
    }
  }

  return c;
}

答案 1 :(得分:0)

rgb[][] = { r, g, b}, {r1, g2, b2}, ... }

我建议您访问文档或Multidimensional Arrays上的教程。

相关问题