微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

随机处理后从图像中重绘像素

如何解决随机处理后从图像中重绘像素

假设我有一个像素化的图像……我想使用 Processing 重绘它。

首先应该有一个随机彩色像素网格 - 一段时间后,像素化图像将通过这个像素化网格出现。

我想我可以用这样的系统来管理这个:如果一个特定的随机彩色像素与像素化图像的颜色匹配 - 应该绘制一个新像素。

为了更好地理解,请看一下这段代码

PImage img;

float rows;
float cols;
float pixel;

color gridcolor;
color imagecolor;    

color[][] grid = new color[500][500];
color[] colors = new color[4];

void setup() {
  size(600,600);
  img = loadImage("bw.jpg");    
  cols = grid.length;
  rows = grid[0].length;    
  pixel = width/cols;    
  nostroke();    
  colors[0] = color(0,0);
  colors[1] = color(219,219,219);
  colors[2] = color(218,218,218);
  colors[3] = color(255,255,255);
}


void draw() {
  background(255);

  for (int x = 0; x < rows; x++) {
    for (int y = 0; y < cols; y++) {    
      int selector = int(random(colors.length));
      grid[x][y] = colors[selector];

      gridcolor = grid[x][y];
      fill(gridcolor);  
      rect(x*pixel,y*pixel,pixel,pixel);
    
      imagecolor = img.get(int(x*pixel),int(y*pixel));
      if (imagecolor == gridcolor) {
        fill(imagecolor);
        rect(x*pixel,pixel);
      }
    }
  }
}

所以我能够在颜色匹配的情况下绘制像素——但是因为有色噪声是使用随机绑定的——它们再次消失了...... 如何将矩形像素从 if 语句中移出? 如何让他们留下来?我试着把它连接到一个布尔语句……但这只是纠结……

顺便说一句,这是我的 PImage img:

enter image description here

感谢您的任何帮助!我真的坚持这个...

解决方法

这是我使用 PGraphics 找到的解决方案……使用 PGraphics,所有内容都在第二层上绘制 - 然后使用 image 函数在草图窗口上重新绘制:

PGraphics pg;
PImage img;

float rows;
float cols;
float pixel;

color gridcolor;
color imagecolor;


color[][] grid = new color[100][100];
color[] colors = new color[9];



void setup() {
  size(600,600);
  pg = createGraphics(600,600);
  img = loadImage("1.bmp");

  cols = grid.length;
  rows = grid[0].length;

  pixel = width/cols;

  noStroke();

  colors[0] = color(0,0);
  colors[1] = color(255,255,0);
  colors[2] = color(66,94,171);
  colors[3] = color(240,85,90);
  colors[4] = color(161,205,12);
  colors[5] = color(93,76,132); 
  colors[6] = color(202,132,184);
  colors[7] = color(16,208,208);
  colors[8] = color(255,255);
}


void draw() {
  background(255);

  for (int x = 0; x < rows; x++) {
    for (int y = 0; y < cols; y++) {

      int selector = int(random(colors.length));
      grid[x][y] = colors[selector];

      gridcolor = grid[x][y];
      fill(gridcolor);  
      rect(x*pixel,y*pixel,pixel,pixel);

      pg.beginDraw(); 
      imagecolor = img.get(int(x*pixel),int(y*pixel));
      if (imagecolor == gridcolor) {
        pg.noStroke();
        pg.fill(imagecolor);
        pg.rect(x*pixel,pixel);
        pg.endDraw();
      }
    }
  }
  image(pg,0);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。