【发布时间】:2014-08-26 16:32:31
【问题描述】:
我正在编写 Java 代码,将过滤器应用于图像(由数组(300 x 300 值)组成)。我从数组中获取许多相邻值,对它们进行平均并将它们添加到 ArrayList 中,以生成新值,并最终生成过滤图像。我很欣赏 ArrayList 很大(87616 个对象),但我不知道如何进一步分解这个过程。
如您所见,我对此并不陌生。感谢所有帮助!
import java.util.ArrayList;
public class Analyst {
/**
* Creates a storage object of class Storage within which array data is held
*/
public Analyst () {
Storage store;
store = new Storage();
//Create new DataReader class, in which original data can be read from, initialise it with object file
DataReader file = new DataReader();
//Create neighbours ArrayList to hold neighbour objects within Moore radius
ArrayList<Double> neighbours = new ArrayList<Double>();
//Create newValues ArrayList to hold mean value calculated from neighbours
ArrayList<Double> newValues = new ArrayList<Double>();
//Create loop to run sequence for subsequent lines
for (int y = 0; y < 296; y++){
//Create loop to run average and newValue methods for first line (note that the first value averaged is in [2][2], last is [2][297]
for (int x = 0; x < store.image.length - 4; x++){
// Establish size of Moore Radius = 2r+1
// Run loop to extract first 25 values to new neighbours array
for (int i = 0 + x; i < 5 + x; i++) {
for (int j = 0 + y; j < 5 + y; j++){
neighbours.add(store.image[i][j]);
//System.out.println("Normal store values " + store.image[i][j]);
}
}
//Instantiate new variable to hold total value of objects in neighbour array list
double total = 0;
for (int k = 0; k < neighbours.size(); k++){
total += neighbours.get(k);
}
//Instantiate new variable to hold Mean Filter Moor neighbourhood value
double neighbourhoodAverage = (total / neighbours.size());
//System.out.println("Total = " + total);
//System.out.println("Average = " + neighbourhoodAverage);
//add new average value to newValues ArrayList
newValues.add(neighbourhoodAverage);
}
}
//Print to check correct number of values in row have been added to newValues ArrayList
System.out.println("newValues ArrayList is made up of " + newValues.size() + " averaged values");
}
public static void main (String args[]) {
new Analyst();
}
}
【问题讨论】:
-
你说它需要“很长时间”。那是多长时间? (我们在这里说的是毫秒还是小时......?)
-
大约需要 15 分钟。
标签: java performance loops arraylist