处理编程平均图像

huangapple 未分类评论72阅读模式
标题翻译

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(&quot;dog.jpg&quot;);
boolean loadimg = false;

void setup() {
    size (400,400);
    background(255);
}

void draw() {
    image(inputimg,15,45);
}  

void keyPressed() {
    //for average color
    if(key == &#39;a&#39; || key == &#39;A&#39;){
        loadimg = true;
        inputimg.loadPixels();
        int red=0,green=0,blue=0;
        for(int i=0;i&lt;inputimg.pixels.length;i++){
            color c = inputimg.pixels[i];
            red = red + (c &gt;&gt; 16) &amp; 0xFF;
            green = green + (c &gt;&gt; 8) &amp; 0xFF;
            blue = blue + (c &gt;&gt; 0) &amp; 0xFF;
        }
        red = red / inputimg.pixels.length;
        green= green / inputimg.pixels.length;
        blue = blue / inputimg.pixels.length;

        inputimg.loadPixels();
        for(int i=0;i&lt;height;i++){
            for(int j=0;j&lt;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(&quot;dog.jpg&quot;);
}

void draw() {
    background(255);
    image(inputimg, 15, 45);
}  

void keyPressed() {
    
    if (key == &#39;a&#39; || key == &#39;A&#39;){
        inputimg.loadPixels();
        int red=0, green=0, blue=0;
        for(int i=0; i&lt;inputimg.pixels.length; i++){
            color c = inputimg.pixels[i];
            red += (c &gt;&gt; 16) &amp; 0xFF;
            green += (c &gt;&gt; 8) &amp; 0xFF;
            blue += (c &gt;&gt; 0) &amp; 0xFF;
        }
        red /= inputimg.pixels.length;
        green /= inputimg.pixels.length;
        blue /= inputimg.pixels.length;

        for(int i=0;i&lt;inputimg.pixels.length;i++){
            inputimg.pixels[i] = color(red,green,blue);
        }
        inputimg.updatePixels();
    }
}

huangapple
  • 本文由 发表于 2020年3月4日 11:07:11
  • 转载请务必保留本文链接:https://java.coder-hub.com/60518505.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定