我在处理中编写了一个程序,它将图像中黑色像素的一维坐标写入文本文件。
我编写了第二个程序,它应该读取文本文件并重新创建白色图像上的黑色像素,以验证脚本#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
循环:
for (int x = 0; x < width; x++ ) {
for (int y = 0; y < height; y++ ) {
这意味着每秒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");
}
}