标题翻译
Processing Programming Average Image
问题
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();
}
}
英文翻译
I just began learning to program within Processing and I am now working with images. I am trying to load an image, then create an average image when the key 'a' is pressed. I want a second image to be computed, where the pixels are all set to the average color, of the original image, over all the pixels in the image. Here's the code so far, but every time I press the 'a' key, I receive an error message.
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();
}
}
答案1
得分: 0
PImage.loadPixels
将像素加载到 PImage
对象的.pixel
属性中。更改图像的.pixel
并使用PImage.updatePixels()
将像素存储到图像中。例如:
PImage inputimg;
void setup() {
size (400,400);
inputimg = loadImage("dog.jpg");
}
void draw() {
background(255);
image(inputimg, 15, 45);
}
void keyPressed() {
if (key == 'a' || key == 'A'){
inputimg.loadPixels();
int red=0, green=0, blue=0;
for(int i=0; i<inputimg.pixels.length; i++){
color c = inputimg.pixels[i];
red += (c >> 16) & 0xFF;
green += (c >> 8) & 0xFF;
blue += (c >> 0) & 0xFF;
}
red /= inputimg.pixels.length;
green /= inputimg.pixels.length;
blue /= inputimg.pixels.length;
for(int i=0; i<inputimg.pixels.length; i++){
inputimg.pixels[i] = color(red, green, blue);
}
inputimg.updatePixels();
}
}
英文翻译
PImage.loadPixels
loads the pixels to the .pixel
attribute of the PImage
object. Change the .pixel
of the image and use PImage.updatePixels()
to store the pixel to the image. e.g:
PImage inputimg;
void setup() {
size (400,400);
inputimg = loadImage("dog.jpg");
}
void draw() {
background(255);
image(inputimg, 15, 45);
}
void keyPressed() {
if (key == 'a' || key == 'A'){
inputimg.loadPixels();
int red=0, green=0, blue=0;
for(int i=0; i<inputimg.pixels.length; i++){
color c = inputimg.pixels[i];
red += (c >> 16) & 0xFF;
green += (c >> 8) & 0xFF;
blue += (c >> 0) & 0xFF;
}
red /= inputimg.pixels.length;
green /= inputimg.pixels.length;
blue /= inputimg.pixels.length;
for(int i=0;i<inputimg.pixels.length;i++){
inputimg.pixels[i] = color(red,green,blue);
}
inputimg.updatePixels();
}
}
专注分享java语言的经验与见解,让所有开发者获益!
评论