【问题标题】:Java RGB code to csv - table aggregationJava RGB 代码到 csv - 表聚合
【发布时间】:2015-12-03 07:32:45
【问题描述】:

我已经准备好代码,用于从我想要的每个像素(图片每 10 行中的每个像素)读取 R G 和 B 值。它返回像素的位置及其各自的 RGB 值,但仅在列中。现在我必须更深入地研究这个,我希望程序也给我一个包含表格聚合的 CSV 文件:第一列将是测试图片上的 y 位置,第一行是 x 位置(逐个像素) ,下一行是 R 值,然后是 G,然后是 B。问题是我已将其放入循环中以使其自动完成,但我再次堆叠 - ArrayIndexOutOfBoundsException 我认为我不应该有。这是代码:

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
}

我想得到这样的东西:

rowIndex0 ; redValue , greenValue, blueValue ; redValue , greenValue, blueValue ...
rowIndex1 ; redValue , greenValue, blueValue ; redValue , greenValue, blueValue ...

我用分号将像素分成一行,只是为了表明我的想法。每行的列都相同(例如,每行 0 个像素)。关于在 loadPixels() 之后分隔列 - 这只会设置第一列,不是吗?将引用行中像素的“i”列怎么样?如何做到这一点?

【问题讨论】:

    标签: java csv pixel indexoutofboundsexception


    【解决方案1】:

    鉴于您的评论,您应该更正 for 语句以跳转到 10 的倍数:例如

    for(int s=0; s < pixels.length; s=s+10*width) 
    

    警告:您正在使用以下行为每个像素创建 2 个新列!您应该在 loadPixels() 之后单独移动这些行。

    for(int j=0; j < width; j++) {
      Tabela.addColumn("Y");
      String i=Integer.toString(j);
      Tabela.addColumn(i);
    }
    

    至于原始问题,请明确您需要哪种格式。这是您想要的格式吗?

    rowIndex , columnIndex , redValue , greenValue , blueValue
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多