Loops 无法在处理过程中从1D坐标更新像素

Loops 无法在处理过程中从1D坐标更新像素,loops,file-io,processing,pixels,Loops,File Io,Processing,Pixels,我在Processing中编写了一个程序,将图像中黑色像素的1D坐标写入文本文件 我编写了第二个程序,该程序应该读取文本文件并在白色图像上重新创建黑色像素,以验证脚本1的功能 问题在于,第二个应该在白色图像上重新创建黑色像素的程序没有这样做 对于第一个脚本,用户打开gui,点击2打开文件浏览器,然后加载文件。代码调整文件大小并将其更改为黑白。然后,代码扫描黑色像素,并将黑色像素坐标写入文本文件,每行一个坐标 第二个脚本只需将这些坐标中的黑色像素添加到白色图像中,即可重新创建原始图像。这是为了验证

我在Processing中编写了一个程序,将图像中黑色像素的1D坐标写入文本文件

我编写了第二个程序,该程序应该读取文本文件并在白色图像上重新创建黑色像素,以验证脚本1的功能

问题在于,第二个应该在白色图像上重新创建黑色像素的程序没有这样做

对于第一个脚本,用户打开gui,点击2打开文件浏览器,然后加载文件。代码调整文件大小并将其更改为黑白。然后,代码扫描黑色像素,并将黑色像素坐标写入文本文件,每行一个坐标

第二个脚本只需将这些坐标中的黑色像素添加到白色图像中,即可重新创建原始图像。这是为了验证脚本1的功能

下面是脚本1的代码:

boolean stopt = false;
boolean writin = false;
PrintWriter output;
PImage input;
int limiter = 0;
void setup() {
  size(350, 500);
// Create a new file in the sketch directory
 output = createWriter("DIRECTIONS.TXT");
}
void draw(){
  background(0, 25, 51);
  //show processed image here.
  text("Click and type '1' to stop", 10, 440);
   text("Click and type '2' to convert image to command file", 10, 460);
    text("Copy DIRECTIONS.TXT to the printer's memory card after", 10, 480);
    if (stopt) {
      writin = false;
       text("finished or stopped.", 10, 420);
    }
    if (!stopt) {
      text("                          ", 10, 420);
    }
    if (writin) {
      stopt = false;
      text("translating image to printer commands", 10, 420);
    }
    if (!writin) {
      text("                          ", 10, 420);
    }
  if(input!= null){
    input.resize(300, 300);
    input.filter(THRESHOLD, 0.5);
    image(input, 10, 10, 330, 330);

     input.loadPixels();
  //Loop through each pixel (use image size instead of pixels.length!)
  if (limiter <= 90000) {
for (int x = 0; x < width; x++ ) {
  for (int y = 0; y < height; y++ ) { 
    color black = color(0, 0, 0);
    color c = get(x, y);
    if (c == black) {
    int loc = x + y * width;
     output.println(loc);
     writin = true;
  }
}
limiter = limiter + 1;
}

  }
  else if (limiter > 90000) {
    stopt = true;
  }
}
}

void imageSelected(File selection) {
  if (selection == null) {
    println("Window was closed or the user hit cancel.");
  } else {
    input = loadImage (selection.getAbsolutePath());
  }
}
void keyPressed() {
  if (key == '1') {
  output.flush(); // Writes the remaining data to the file
  output.close(); // Finishes the file
  stopt = true;
  //exit(); // Stops the program
}
if (key == '2') {
  selectInput("Select an image to process", "imageSelected");
  }
}
下面是脚本2的代码,它应该从脚本1生成的文本文件中重新创建加载到脚本1中的图像,但不是

PImage input;
int tex = 0;
void setup() {
  input = loadImage("white.png"); //mostly blank image in sketch folder 300 x 300 px.
  size (300, 300);
}
void draw() {
  loadPixels();
  input.loadPixels();
  String[] lines = loadStrings("direct.TXT"); //text file with each line being a 1D pixel coordinate in linear order.
  for (int i = 0; i < lines.length; i++) {
    tex = Integer.parseInt(lines[i]);
    for (int x = 0; x < width; x++ ) {
      for (int y = 0; y < height; y++ ) { 
        color black = color(0, 0, 0);
        //color c = get(x, y);
        int loc = x + y * width; 
        if (loc == tex) {
          pixels[loc] = black;
        }
      }
    }
  }
  updatePixels();
}
请记住,draw函数每秒调用60次

然后在draw函数中,有以下嵌套for循环:

这意味着每秒60次,你在所有像素上循环并将内容输出到一个文件

所以要解决问题,您需要更改代码,这样您就不会再这样做了


退一步,你应该养成正确理解它在做什么的习惯。这将帮助您处理类似的情况,并帮助您理解代码应该如何工作。祝你好运

它是固定的。以下是解决方案:

将文本文件中的坐标转换回图像以验证原始转换器的脚本:

String[] input;// input file

void setup(){
  size(300, 300);
  input = loadStrings("DIRECTIONS.TXT");// load DIRECTIONS.TXT

  background(255);// clear image

  loadPixels();
  for(int i = 0; i < input.length; i ++){
    pixels[int(input[i])] = color(0);// for each line in the input file, set that pixel to black
  }
  updatePixels();
  println("done");
}
要处理并将图像转换为黑色像素坐标文本文件的脚本:

PImage input;// The selected image
String[] output;// The output file

void setup(){
  size(300, 300);
  output = new String[1];
  output[0] = "";// Avoid "null" at beginning
  selectInput("Select an image to process", "select");
}

void select(File selection){
  if(selection == null){
    exit();// exit if the input is null
  }else{
    input = loadImage(selection.getAbsolutePath());// load selected image
    input.resize(width, height);// reszie
    input.filter(THRESHOLD, 0.5);// make everything black and white

    input.loadPixels();
    for(int i = 0; i < input.pixels.length; i ++){// loop through all pixels
      if(input.pixels[i] == color(0)){
        output[0] += i + "\n";// if the pixel is black, write the position to the file
      }
    }
    saveStrings("DIRECTIONS.TXT", output);// save everything
    println("done");
  }
}

我根据您的想法修改了代码,使其仅循环90000倍于图像中最大像素数,但文件输出仍然是195032 KB。有没有办法减少这个?你是不是每帧循环90000次?还是全部?你在输出什么?您希望文件有多大?您能否尝试使用具有已知输出的简单图像?正如我提到的,解决此类问题的关键是调试。祝你好运嗨,凯文,我根据新的更新更新了前提。
PImage input;// The selected image
String[] output;// The output file

void setup(){
  size(300, 300);
  output = new String[1];
  output[0] = "";// Avoid "null" at beginning
  selectInput("Select an image to process", "select");
}

void select(File selection){
  if(selection == null){
    exit();// exit if the input is null
  }else{
    input = loadImage(selection.getAbsolutePath());// load selected image
    input.resize(width, height);// reszie
    input.filter(THRESHOLD, 0.5);// make everything black and white

    input.loadPixels();
    for(int i = 0; i < input.pixels.length; i ++){// loop through all pixels
      if(input.pixels[i] == color(0)){
        output[0] += i + "\n";// if the pixel is black, write the position to the file
      }
    }
    saveStrings("DIRECTIONS.TXT", output);// save everything
    println("done");
  }
}