【发布时间】:2020-03-04 03:07:11
【问题描述】:
我刚开始学习在 Processing 中编程,现在我正在处理图像。我正在尝试加载图像,然后在按下键“a”时创建平均图像。我想要计算第二张图像,其中像素全部设置为原始图像的平均颜色,在图像中的所有像素上。这是到目前为止的代码,但每次我按“a”键时,都会收到一条错误消息。
PImage inputimg = loadImage("dog.jpg");
boolean loadimg = false;
void setup() {
size (400,400);
background(255);
}
void draw() {
image(inputimg,15,45);
}
void keyPressed() {
//for average color
if(key == 'a' || key == 'A'){
loadimg = true;
inputimg.loadPixels();
int red=0,green=0,blue=0;
for(int i=0;i<inputimg.pixels.length;i++){
color c = inputimg.pixels[i];
red = red + (c >> 16) & 0xFF;
green = green + (c >> 8) & 0xFF;
blue = blue + (c >> 0) & 0xFF;
}
red = red / inputimg.pixels.length;
green= green / inputimg.pixels.length;
blue = blue / inputimg.pixels.length;
inputimg.loadPixels();
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
int update = j + i*width;
pixels[update] = color(red,green,blue);
}
}
updatePixels();
}
}
【问题讨论】:
标签: javascript java image processing computer-science