我正在尝试实现具有N级量化的非均匀量化器。 我已经完成了一些工作并且它有效,问题是当N(级别数)超过“4”时它会进入无限循环。
如果有人能指出任何提示,知道错误的位置,我会很感激。
public static Vector<Integer> split(Vector <Integer> image,float average,int n)
{
float lowerAverage = average - 1;
float upperAverage = average + 1;
Vector<Float> averages = new Vector<Float>();
Vector <Integer> leftData = new Vector <Integer>();
Vector <Integer> rightData = new Vector <Integer>();
averages.add(lowerAverage);
averages.add(upperAverage);
//FIND ALL AVERAGES
while (averages.size()<n)
{
//I THINK THE PROBLEM HAPPENS HERE AS IT KEEP PRINTING "loop"
System.out.println("loop");
for (int i = 0; i < image.size(); i++)
{
if (Math.abs(image.get(i)-lowerAverage) <= Math.abs(image.get(i)-upperAverage))
{
leftData.add(image.get(i));
}
else
{
rightData.add(image.get(i));
}
}
lowerAverage = average(leftData) - 1;
upperAverage = average (leftData) + 1;
averages.removeAllElements();
averages.add(lowerAverage);
averages.add(upperAverage);
lowerAverage = average(rightData) - 1;
upperAverage = average (rightData) + 1;
averages.add(lowerAverage);
averages.add(upperAverage);
}
//*****************************************************************************************
//CREATE DATASETS WITH NUMBER == AVERAGES.SIZE()
Vector <DataSet> v = new Vector <DataSet>();
for (int i = 0; i < averages.size(); i++) {
DataSet temp = new DataSet();
temp.setName(averages.get(i));
v.add(temp);
}
//*****************************************************************************************
//SPLIT ORIGINAL DATA ACCORDING TO AVERAGES
float name;
for (int i = 0; i < image.size(); i++)
{
float min =Math.abs(image.get(i)-averages.get(0));
name = averages.get(0);
for (int j = 1; j < averages.size(); j++)
{
if (Math.abs(image.get(i)-averages.get(j)) < min)
{
min = Math.abs(image.get(i)-averages.get(j));
name = averages.get(j);
}
}
getDataset(v, name).addData(image.get(i));
}
//*****************************************************************************************
//CALCULATE EACH DATASET AVERAGE
for (int i = 0; i < v.size(); i++)
{
v.get(i).UpdateAverage();
}
//*****************************************************************************************
//THIS IS JUST FOR TESTING
Vector<Integer> Qinv = new Vector<Integer>();
for (int i = 0; i < v.size(); i++)
{
Qinv.add(v.get(i).getAverage());
}
return Qinv;
}