无法从处理中的一维坐标更新像素
Unable to update pixels from 1D coordinates in Processing
我在 Processing 中编写了一个程序,它将图像中黑色像素的一维坐标写入文本文件。
我编写了第二个程序,它应该读取文本文件并在白色图像上重新创建黑色像素以验证脚本 #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 次所有像素并将内容输出到文件。
因此,要解决您的问题,您需要更改代码,这样您就不会再这样做了。
退一步说,您应该养成 debugging your code 的习惯,以准确理解它在做什么。这将帮助您处理此类情况,并帮助您了解您的代码应该如何运行。祝你好运!
已修复。这是解决方案:
将文本文件中的坐标转换回图像以验证原始转换器的脚本:
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");
}
}
我在 Processing 中编写了一个程序,它将图像中黑色像素的一维坐标写入文本文件。
我编写了第二个程序,它应该读取文本文件并在白色图像上重新创建黑色像素以验证脚本 #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 次所有像素并将内容输出到文件。
因此,要解决您的问题,您需要更改代码,这样您就不会再这样做了。
退一步说,您应该养成 debugging your code 的习惯,以准确理解它在做什么。这将帮助您处理此类情况,并帮助您了解您的代码应该如何运行。祝你好运!
已修复。这是解决方案:
将文本文件中的坐标转换回图像以验证原始转换器的脚本:
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");
}
}