处理:仅使一些随机像素改变颜色

时间:2015-12-04 13:13:27

标签: processing pixels noise

我创造了一些白噪声,我希望随着时间的推移减少(2秒后开始变化,10秒后变强等),慢慢趋向黑屏。

我无法弄清楚的是,我怎样才能使一些(比如所有像素的50%)随机像素改变颜色,而其余像素只是黑色,在同一帧内?

到目前为止,我只能随机更改所有这些内容,或者所有内容都保持黑色。非常感谢任何帮助,谢谢!!

void setup() {
  size(1000, 800);
}

void draw() {
  if (millis() < 2000) {
    loadPixels();
    for ( int i=0; i<pixels.length; i++)
      pixels[i] = color(random(255));
    updatePixels();
  }

  if (millis() > 2000) {
    loadPixels();
    if (random(1) >= 0.5) {
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color(random(255));
      updatePixels();
    } else {
      loadPixels();
      for ( int i=0; i<pixels.length; i++)
        pixels[i] = color(0);
      updatePixels();
    }
  }

  if (millis() > 10000) {
    loadPixels();
    for ( int i=0; i<pixels.length; i++)
      pixels[i] = color(random(255));
    updatePixels();
  }
}

2 个答案:

答案 0 :(得分:1)

一种简单的方法是考虑random()返回范围内的随机值。如果你给它一个低值,你将有一个低随机值。 如果您将该值用作颜色,则值越低,您就越接近黑色,这可能适用于您的情况。

如果您的随机性为255,则增加亮像素的变化,否则(随机值较低),像素将变暗:

/* ... */

为了解决这个问题,这里是一个稍微修改过的代码版本,它使用//noise image PImage noise; //amount of noise image image 0 = 0%, 255 = 100% int noiseAmt = 255; void setup(){ noise = createImage(width,height,RGB); } void draw(){ //decrease noise over time noiseAmt--; if(noiseAmt < 0) noiseAmt = 255; //apply noise based on noise amount noiseImage(); //render image image(noise,0,0); } void noiseImage(){ int numPixels = noise.pixels.length; for(int i = 0 ; i < numPixels; i++){ //random(noiseAmt) is the key - low values = darker pixels noise.pixels[i] = color(random(noiseAmt)); } noise.updatePixels(); } 箭头键来控制噪音:

UP/DOWN

回到时间问题,您可以查看this answer,其中包含//noise image PImage noise; //amount of noise image image 0 = 0%, 255 = 100% int noiseAmt = 127; void setup() { noise = createImage(width, height, RGB); } void draw() { //apply noise based on noise amount noiseImage(); //render image image(noise, 0, 0); } void noiseImage() { int numPixels = noise.pixels.length; for (int i = 0; i < numPixels; i++) { //random(noiseAmt) is the key - low values = darker pixels noise.pixels[i] = color(random(noiseAmt)); } noise.updatePixels(); } void keyPressed(){ if(keyCode == UP) noiseAmt += 5; if(keyCode == DOWN) noiseAmt -= 5; noiseAmt = constrain(noiseAmt,0,255); println("noiseAmt: " + noiseAmt); } 的跟踪时间。唯一的额外部分是将衰落时间映射到噪声量,这将是一些比率。如果我们将传递的时间映射为标准化值(从0.0到1.0)可能更容易,只需乘以255即可轻松扩展到0.0到255.0:

millis()

Processing有一些很好的功能来处理mappingconstraining个数字范围:

//noise image
PImage noise;
//amount of noise image image 0 = 0%, 255 = 100%
int noiseAmt = 255;

int timestamp,fadeTime = 10000;//fade to black in 10s

void setup(){
  noise = createImage(width,height,RGB);
  timestamp = millis();
}
void draw(){
  //decrease noise over time
  int now = millis();
  //if the difference between an initial timestamp and the current time is less than 10s
  if(now - timestamp <= fadeTime){
    //compute the ratio between the time difference and total fadeTime which will be from 0.0 to 1.0
    //subtract this difference from 1.0 to flip the ratio direction from 0.0 -> 1.0 to 1.0 -> 0.0 
    float fadeRatio = 1.0 - ((float)(now-timestamp)/fadeTime);
    //this ratio multiplied to 255 will be
    noiseAmt = (int)(fadeRatio * 255);
  } 

  //apply noise based on noise amount
  noiseImage();
  //render image
  image(noise,0,0);
}

void noiseImage(){
  int numPixels = noise.pixels.length;
  for(int i = 0 ; i < numPixels; i++){
    //random(noiseAmt) is the key - low values = darker pixels
    noise.pixels[i] = color(random(noiseAmt));
  }
  noise.updatePixels();
}

注意//noise image PImage noise; //amount of noise image image 0 = 0%, 255 = 100% int noiseAmt = 255; int timestamp,fadeTime = 10000;//fade to black in 10s void setup(){ noise = createImage(width,height,RGB); timestamp = millis(); } void draw(){ //decrease noise over time int now = millis(); //if the difference between an initial timestamp and the current time is less than 10s noiseAmt = (int)map(now - timestamp,0,fadeTime,255,0); noiseAmt = constrain(noiseAmt,0,255); //apply noise based on noise amount int numPixels = noise.pixels.length; for(int i = 0 ; i < numPixels; i++){ //random(noiseAmt) is the key - low values = darker pixels noise.pixels[i] = color(random(noiseAmt)); } noise.updatePixels(); //render image image(noise,0,0); } 设置为10s(10000毫秒)。随意修改fadeTime值。

答案 1 :(得分:0)

不要将所有代码发布到外部网站,而是将问题简化为MCVE并将其直接包含在您的问题中。

话虽如此,您有两种选择:

选项1 :将所有像素存储在某种数据结构中。您可能有一个MyPixel个对象的二维数组,其中MyPixel是您创建的一个类,其中包含您需要知道该数组中哪些实例更改颜色所需的所有信息。

选项2 :直接绘制到PImage。然后,您可以遍历PImage来查找非黑色像素并进行更改。

您采取哪种方法完全取决于您。我亲自选择第一个选项,但这仅仅是我个人的偏好。尝试使用其中一种方法,并在遇到问题时发布MCVE。请注意,这应该是尽可能少的线,同时仍然展示问题,而不是整个草图 - 例如,我们不需要查看您的时序逻辑。