我在Processing中编写了一个算法来执行以下操作:
1. Instantiate a 94 x 2 int array
2. Load a jpg image of dimensions 500 x 500 pixels
3. Iterate over every pixel in the image and determine whether it is black or white then change a variable related to the array
4. Print the contents of the array
由于某种原因,此算法立即冻结。我把打印声明放在那里告诉我它甚至在尝试加载图像之前就冻结了。鉴于我已经编写了另一个非常类似的算法而没有出现并发症,这对我来说尤其令人困惑。另一种算法读取图像,平均每个图块的颜色,无论指定大小,然后在平均颜色平均的区域上打印矩形,有效地使图像像素化。两种算法都加载图像并检查每个像素。这个问题大多不同,因为它没有任何吸引力。我要说的是,有一个数组是不同的,但像素化算法保存了一个颜色数组中的所有颜色,它应占用比int数组更多的空间。
从我的mac的console.app中查看我发现最初有这个错误:“java.lang.OutOfMemoryError:超出GC开销限制”。从网上的其他建议/来源我尝试将内存分配从256mb提高到4000mb(这样做感觉毫无意义,因为我对算法的分析表明它们应该是相同的复杂性,但我还是尝试过)。这并没有停止冻结,而是将错误更改为“获取Java异常描述时出现JavaNativeFoundation错误”和“java.lang.OutOfMemoryError:Java堆空间”的组合。 然后我尝试将处理指向我的本地jdk,希望利用64位jdk而不是处理内置的32位jdk。在Processing.app/Contents中,我执行了以下命令: mv Java java-old ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_79.jdk Java 在尝试此操作后,处理将无法启动,并填充以下错误: “com.apple.xpc.launchd [1] :( org.processing.app.160672 [13559])服务退出时出现异常代码:1”
以下是我的代码: 首先是不合规算法
int squareSize=50;
int numRows = 10;
int numCols = 10;
PFont myFont;
PImage img;
//33-126
void setup(){
size(500,500);
count();
}
void count(){
ellipseMode(RADIUS);
int[][] asciiArea = new int[94][2];
println("hello?");
img=loadImage("countingPicture.jpg");
println("image loaded");
for(int i=0; i<(500/squareSize); i++){
for(int j=0; j<(500/squareSize); j++){
int currentValue=i+j*numCols;
if(currentValue+33>126){
break;
}
println(i+", "+j);
asciiArea[currentValue][0]=currentValue+33;
asciiArea[currentValue][1]=determineTextArea(i,j,squareSize);
//fill(color(255,0,0));
//ellipse(i*squareSize,j*squareSize,3,3);
}
}
println("done calculating");
displayArrayContents(asciiArea);
}
int determineTextArea(int i, int j, int squareSize){
int textArea = 0;
double n=0.0;
while(n < squareSize*squareSize){
n+=1.0;
int xOffset = (int)(n%((double)squareSize));
int yOffset = (int)(n/((double)squareSize));
color c = img.get(i*squareSize+xOffset, j*squareSize+yOffset);
if(red(c)!=255 || green(c)!=255 || blue(c)!=255){
println(red(c)+" "+green(c)+" "+blue(c));
textArea++;
}
}
return textArea;
}
void displayArrayContents(int[][] arr){
int i=0;
println("\n now arrays");
while(i<94){
println(arr[i][0]+" "+arr[i][1]);
}
}
有效的像素化算法:
PImage img;
int direction = 1;
float signal;
int squareSize = 5;
int wideness = 500;
int highness = 420;
int xDimension = wideness/squareSize;
int yDimension= highness/squareSize;
void setup() {
size(1500, 420);
noFill();
stroke(255);
frameRate(30);
img = loadImage("imageIn.jpg");
color[][] colors = new color[xDimension][yDimension];
for(int drawingNo=0; drawingNo < 3; drawingNo++){
for(int i=0; i<xDimension; i++){
for(int j=0; j<yDimension; j++){
double average = 0;
double n=0.0;
while(n < squareSize*squareSize){
n+=1.0;
int xOffset = (int)(n%((double)squareSize));
int yOffset = (int)(n/((double)squareSize));
color c = img.get(i*squareSize+xOffset, j*squareSize+yOffset);
float cube = red(c)*red(c) + green(c)*green(c) + blue(c)*blue(c);
double grayValue = (int)(sqrt(cube)*(255.0/441.0));
double nAsDouble = (double)n;
average=(grayValue + (n-1.0)*average)/n;
average=(grayValue/n)+((n-1.0)/(n))*average;
}
//average=discretize(average);
println(i+" "+j+" "+average);
colors[i][j]=color((int)average);
fill(colors[i][j]);
if(drawingNo==0){ //stroke(colors[i][j]); }
stroke(210);}
if(drawingNo==1){ stroke(150); }
if(drawingNo==2){ stroke(90); }
//stroke(colors[i][j]);
rect(drawingNo*wideness+i*squareSize,j*squareSize,squareSize,squareSize);
}
}
}
save("imageOut.jpg");
}
答案 0 :(得分:0)
您正在进入无限循环,这使得println()
语句不可靠。修复无限循环,打印语句将再次起作用。
看看这个while
循环:
while(i<94){
println(arr[i][0]+" "+arr[i][1]);
}
i
什么时候会成为>=
94?
您永远不会增加i
,因此其值始终为0
。您可以通过在println()
循环中添加while
语句来证明这一点:
while(i<94){
println("i: " + i);
println(arr[i][0]+" "+arr[i][1]);
}
您可能希望在i
循环内增加while
。或者只使用for
循环。