[英]I need some help understanding arrays of pixels
I'm using Processing to divide a large image into a series of smaller, rectangular nodes. 我正在使用“ 处理”将大图像划分为一系列较小的矩形节点。
Processing stores the color value for the pixels of a PImage
in a pixels
array, which I am accessing to break up the image into smaller parts. 处理将
pixels
数组中的PImage
像素的颜色值存储在pixels
阵列中,我正在访问该图像以将图像分解为更小的部分。 For some reason, I am getting this output, when my intent was for the entire image to be displayed when the nodes are arranged in draw()
. 出于某种原因,我得到这个输出,当我的意图是在节点以
draw()
排列时显示整个图像。
Here is my main class: 这是我的主要课程:
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++;
}
}
}
And here is the Node class: 这是Node类:
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)];
}
}
Hopefully my code makes sense. 希望我的代码有意义。 I suspect the issue is in the
sendPixels()
method of the main class. 我怀疑问题出在主类的
sendPixels()
方法中。
I used this this page from the Processing reference as a guide for creating that function, and I'm not sure where my logic is wrong. 我用这个这个网页,处理基准的作为创建功能的指导,我不知道在我的逻辑是错误的。
Any help would be appreciated, and please let me know if I can clarify something. 任何帮助,我们将不胜感激,如果可以澄清,请告诉我。
According to getPixelColor()
, it seems that it uses rows. 根据
getPixelColor()
,它似乎使用了行。
So if you have a 5x5 square image then 2x2 would be 7. 因此,如果您有一个5x5的正方形图像,那么2x2就是7。
To get the index you use this formula: 要获取索引,请使用以下公式:
index = (y - 1) * width + x
Explained this way it's look pretty simple, doesn't it? 用这种方式解释它看起来很简单,不是吗?
Alternatively, you may be able to use getSubimage()
on the BufferedImage
returned by the getImage
method of PImage
. 或者,你可以使用
getSubimage()
上BufferedImage
通过返回getImage
方法PImage
。 There's a related example here . 有一个相关的例子在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.