更改直方图中的bin值

时间:2015-05-05 04:25:36

标签: java arrays indexing bounds

我之前发布了此代码,但我解决了我遇到的问题。但是,我遇到了最后一个问题:如何将此代码中的bin值从64更改为8和512?对于64个bin,它会运行得很好,但是其他任何东西都会返回一个超出范围的数组索引。这是“stackoverflow”的完美问题,因为这正是这里发生的事情。 :)

   import java.io.File;
   import weka.core.Attribute;
   import weka.core.FastVector;
   import weka.core.Instance;
   import weka.core.Instances;
   import weka.core.converters.ArffSaver;
   import weka.filters.Filter;
   import weka.filters.unsupervised.instance.NonSparseToSparse;
   import java.awt.image.BufferedImage;
   import java.io.File;
   import java.io.IOException;
   import java.io.PrintWriter;
   import javax.imageio.ImageIO;
   import javax.swing.JPanel;

public class AttTest {

 public static void main(String[] args) throws Exception 
{


    int i;
    FastVector attributes;
    Instances dataSet;
    attributes = new FastVector();
    for (i = 0; i < 64; i++) 
    attributes.addElement(new Attribute("bin" +(i+1)))  ; 


        dataSet = new Instances("NormalizedHistogram_64bins", attributes, 0);

    NonSparseToSparse nonSparseToSparseInstance = new NonSparseToSparse(); 
    nonSparseToSparseInstance.setInputFormat(dataSet); 
    Instances sparseDataset = Filter.useFilter(dataSet, nonSparseToSparseInstance);

    System.out.println(sparseDataset);

    ArffSaver arffSaverInstance = new ArffSaver(); 
    arffSaverInstance.setInstances(sparseDataset); 
    arffSaverInstance.setFile(new File("ESDN.arff")); 
    arffSaverInstance.writeBatch();

    Histogram();


 }

private static void Histogram() throws IOException {
    int[][][] ch = new int[4][4][4];
    BufferedImage image = ImageIO.read(new File("airplane_training3.jpg"));
    for(int x = 0; x < image.getWidth(); x++)
        for(int y = 0; y < image.getHeight(); y++) {
            int color = image.getRGB(x, y);
            int alpha = (color & 0xff000000) >> 24;
            int red = (color & 0x00ff0000) >> 16;
            int green = (color & 0x0000ff00) >> 8;
            int blue = color & 0x000000ff;
            ch[red / 64][green / 64][blue / 64]++;
        }
    for(int i = 0; i < ch.length; i++)
        for(int j = 0; j < ch[i].length; j++)
            for(int p = 0; p < ch[i][j].length; p++)
                System.out.print("0." + (ch[i][j][p])/64 + ",");
    PrintWriter writer = new PrintWriter("C:\\Users\\Someguy\\Desktop\\TrainingData_64.arff");
    writer.println("");
    writer.close();
}

}

0 个答案:

没有答案