Java 我需要一些理解像素阵列的帮助

Java 我需要一些理解像素阵列的帮助,java,image-processing,processing,Java,Image Processing,Processing,我用它将一个大图像分割成一系列较小的矩形节点 Processing将PImage的像素的颜色值存储在像素数组中,我正在访问该数组以将图像分割成更小的部分。出于某种原因,我正在获取输出,而我的目的是在节点排列在draw()中时显示整个图像 这是我的主要课程: ArrayList node = new ArrayList(); PImage grid; PVector nodeDimensions = new PVector(210, 185); PVector gridDimensions = n

我用它将一个大图像分割成一系列较小的矩形节点

Processing将
PImage
的像素的颜色值存储在
像素
数组中,我正在访问该数组以将图像分割成更小的部分。出于某种原因,我正在获取输出,而我的目的是在节点排列在
draw()
中时显示整个图像

这是我的主要课程:

ArrayList node = new ArrayList();
PImage grid;
PVector nodeDimensions = new PVector(210, 185);
PVector gridDimensions = new PVector(2549, 3300);
String name = "gridscan.jpeg";

void setup() {
  size(500, 500); 
  grid = loadImage(name);
  grid.loadPixels(); 
  fillPixels();
  noLoop();
}

void fillPixels() {
  int nodeNum = 0;
  for (int startX = 0; startX < 2549 - nodeDimensions.x; startX += nodeDimensions.x) {
    for (int startY = 0; startY < 3300 - nodeDimensions.y; startY += nodeDimensions.y) {
      node.add(new Node()); 
      sendPixels(new PVector(startX, startY), nodeNum);
      nodeNum++;
    }
  }
}

void sendPixels(PVector start, int nodeNum) {
  for (int x = int(start.x); x < start.x + nodeDimensions.x; x++) {
    for (int y = int(start.y); y < start.x + nodeDimensions.y; y++) {
      Node _node = (Node) node.get(node.size() - 1);
      _node.fillPixel(new PVector(x, y), grid.pixels[int(y*gridDimensions.x+x)]);
    }
  }
}

void draw() {
  drawNodes();
}

void drawNodes() {
  int nodeNum = 0;
  for (int x = 0; x < width; x += nodeDimensions.x) {
    for (int y = 0; y < height; y += nodeDimensions.y) {
      Node _node = (Node) node.get(nodeNum);
      _node.drawMe(new PVector(x - (nodeDimensions.x/2), y - (nodeDimensions.y/2))); 
      nodeNum++;
    }
  }
}
ArrayList节点=新建ArrayList();
PImage网格;
PVector节点尺寸=新PVector(210185);
PVector网格尺寸=新PVector(25493300);
String name=“gridscan.jpeg”;
无效设置(){
大小(500500);
grid=loadImage(名称);
grid.loadPixels();
填充像素();
noLoop();
}
void fillPixels(){
int nodeNum=0;
对于(int-startX=0;startX<2549-nodeDimensions.x;startX+=nodeDimensions.x){
对于(int startY=0;startY<3300-nodeDimensions.y;startY+=nodeDimensions.y){
添加(新节点());
sendPixels(新PVector(startX、startY)、nodeNum);
nodeNum++;
}
}
}
void sendPixels(PVector开始,int nodeNum){
对于(int x=int(start.x);x
下面是节点类:

class Node {

color[] pixel;

Node() {
  pixel = new color[int(nodeDimensions.x * nodeDimensions.y)];
}

void fillPixel(PVector pos, color pixelValue) {
  if(int(pos.y * nodeDimensions.y + pos.x) < 38850) pixel[int(pos.y * nodeDimensions.y + pos.x)] = pixelValue;
}

void drawMe(PVector centerPos) {
  pushMatrix();
  translate(centerPos.x, centerPos.y);
  for(int x = 0; x < nodeDimensions.x; x++) {
   for(int y = 0; y < nodeDimensions.y; y++) {
    stroke(getPixelColor(new PVector(x, y)));
    point(x,y);
   } 
  }
  popMatrix();
}


color getPixelColor(PVector pos) {
 return pixel[int(pos.y * nodeDimensions.x + pos.x)];
}

}
类节点{
彩色[]像素;
节点(){
像素=新颜色[int(nodeDimensions.x*nodeDimensions.y)];
}
空心填充像素(PVector位置、颜色像素值){
如果(int(pos.y*nodeDimensions.y+pos.x)<38850)像素[int(pos.y*nodeDimensions.y+pos.x)]=像素值;
}
作废图纸(PVector centerPos){
pushMatrix();
翻译(中心位置x、中心位置y);
对于(int x=0;x
希望我的代码有意义。我怀疑问题出在main类的
sendPixels()
方法中

我使用Processing参考中的这一页作为创建该函数的指南,我不确定我的逻辑哪里错了


非常感谢您的帮助,如果我能澄清一些问题,请告诉我。

根据
getPixelColor()
,它似乎使用了行。
所以如果你有一个5x5的正方形图像,那么2x2就是7。
要获取索引,请使用以下公式:
索引=(y-1)*宽度+x


通过这种方式解释,它看起来非常简单,不是吗?

或者,您可以在通过方法返回的
缓冲图像上使用。有一个相关的例子