如何加快从文本文件中读取和处理数据?

时间:2015-11-02 20:23:05

标签: java android opencv

我一直在尝试用Java中的大型(~270 MB)文本文件读取数据并将其存储在Mats和3D点阵列的ArrayList中,以便进行基于Flann的匹配。虽然这些方法有效,但处理和存储数据需要花费太多时间(大约20-30分钟)。有没有办法加快将值添加到ArrayList?

我的文本文件格式如下: -1.78894 -0.260657 -5.01526 54 15 2899.69 817.542 14 0 0 1 ...(重复124次以上)

这是我使用BufferedReader的代码,其中mDescriptors是Mat对象的ArrayList,mPoints3d是3D点的2D ArrayList。

public void loadData() {

    // initialize mDescriptors and mPoints3d by iterating through it;
    for(int i = 0; i < 256; i++)
    {
        mDescriptors.add(new Mat());
        mPoints3d.add(new ArrayList<Point3>());
    }

    try {
        // open the database data file and read from it
        Log.i(TAG, "Opening table.txt");
        File fTable = new File(Environment.getExternalStorageDirectory().getPath() + "/GlassProject/table.txt");
        BufferedReader br = new BufferedReader(new FileReader(fTable));

        String str;
        // keep reading data until there is no more data to read
        while((str = br.readLine()) != null)
        {
            String[] sa = str.split(" ");
            Point3 xyz = new Point3(Float.valueOf(sa[0]), Float.valueOf(sa[1]), Float.valueOf(sa[2]));
            int imgidx = Integer.valueOf(sa[3]);
            //int featidx = Integer.valueOf(sa[4]);
            // Iterate through the rest of the line to get the descriptor data (128 values)
            for(int i = 7; i < sa.length; i++)
            {
                mDes.put(0,i - 7,Integer.valueOf(sa[i]));
            }
            mDescriptors.get(imgidx - 1).push_back(mDes); // Index issues happen here
            mPoints3d.get(imgidx - 1).add(xyz);
        }
        Log.v(TAG, "Loaded the Database data!");
    }
    catch(IOException ex) {
        Log.e(TAG, "Error: could not open table");
        ex.printStackTrace();
    }
}

0 个答案:

没有答案