【问题标题】:Processing Programming Average Image处理编程平均图像
【发布时间】: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


    【解决方案1】:

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      • 2017-06-11
      • 1970-01-01
      • 2011-09-13
      • 2018-06-08
      • 1970-01-01
      相关资源
      最近更新 更多