JavaRGB代码到csv-表聚合
我已经准备好从我想要的每个像素(图片每10行中的每个像素)读取RG和B值的代码。它返回像素的位置及其各自的RGB值,但仅以列形式返回。现在,我必须更深入地了解这一点,我希望程序也给我一个CSV文件,其中包含表格的聚合:第一列是测试图片上的y位置,第一行是x位置(逐像素),下一行是R值,然后是G,然后B.问题是,我已经将它放入循环中,以便自动完成,但我已经再次堆叠了-ArrayIndexOutofBounds感知我认为不应该有的东西。代码如下:JavaRGB代码到csv-表聚合,java,csv,pixel,indexoutofboundsexception,Java,Csv,Pixel,Indexoutofboundsexception,我已经准备好从我想要的每个像素(图片每10行中的每个像素)读取RG和B值的代码。它返回像素的位置及其各自的RGB值,但仅以列形式返回。现在,我必须更深入地了解这一点,我希望程序也给我一个CSV文件,其中包含表格的聚合:第一列是测试图片上的y位置,第一行是x位置(逐像素),下一行是R值,然后是G,然后B.问题是,我已经将它放入循环中,以便自动完成,但我已经再次堆叠了-ArrayIndexOutofBounds感知我认为不应该有的东西。代码如下: PrintWriter output; Table
PrintWriter output;
Table Tabela;
String i;
void setup()
{
output = createWriter("positions.txt"); //writedown tool enter
Tabela=new Table();
size(251,201); // screen size setup
PImage img = loadImage("sharp.jpg"); // implementing image Sketch->Add File
image(img,0,0); // placeing the img on the screen
loadPixels(); // work on pixels
for(int s=0; s < pixels.length; s=s+9*width) // go through selected rows of pixels with intervals of 10 rows (0 to 0+9width)
{
for(int j=0; j < width; j++) // go through each pixel in selected row
{
float r=red(pixels[s+j]); // read the red value from each chceked pixel
float b=blue(pixels[s+j]); // read the blue value from each chceked pixel
float g=green(pixels[j+s]); // read the green value from each checked pixel
Tabela.addColumn("Y");
String i=Integer.toString(j);
Tabela.addColumn(i);
TableRow newRow=Tabela.addRow();
newRow.setInt("Y", Tabela.lastRowIndex());
newRow.setFloat(s,r);
newRow.setFloat(s,g);
newRow.setFloat(s,b);
output.println(s + " , "+ j +" , "+ r + " , " + g + " , " + b); //write the data down to the file (selected data)
color black = color(0, 0, 0); // checking, set a black color
pixels[j+s]=black; // checking, change the color of current pixel
println(s + " , "+ j +" ; "+ r + " , " + g + " , " + b); // print the read data
}
}
output.flush();
saveTable(Tabela,"data/tabela.csv");
updatePixels(); // set changes
}
void keyPressed()
{
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
exit(); // Stops the program
}
我用分号将像素分成一行,只是为了说明我的想法。每行的列将相同(例如,每行0个像素)。关于在loadPixels()之后分离列-这将只设置第一列,不是吗?那么将引用行中像素的“i”列呢?如何做到这一点?根据您的评论,您应该将for语句改为10的倍数:例如
for(int s=0; s < pixels.length; s=s+10*width)
for(int s=0; s < pixels.length; s=s+10*width)
for(int j=0; j < width; j++) {
Tabela.addColumn("Y");
String i=Integer.toString(j);
Tabela.addColumn(i);
}
rowIndex , columnIndex , redValue , greenValue , blueValue