我正在尝试对8x8 2d阵列列表进行子采样。更确切地说,从8x8开始,我想通过逐个采用2x2块将2x2的算术平均值保存在4x4的正确位置,从而转换为4x4。执行此操作后,输出将是4x4 2d阵列的列表。以下两种方法正在处理这个
public static long[][] subSample420(long[][] originalBlocks){
long[][] downsampledBlocks = new long[4][4];
long[] mediePixel = new long[16];
int nr = 0;
for (int i = 0; i < 8; i += 2){
for (int j = 0; j < 8; j += 2){
mediePixel[nr] = Math.round((double)(originalBlocks[i][j] + originalBlocks[i][j+1] + originalBlocks[i+1][j] + originalBlocks[i+1][j+1]) / 4);
nr++;
}
}
nr = 0;
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
downsampledBlocks[i][j] = mediePixel[nr];
nr++;
}
}
return downsampledBlocks;
}
public static List<long[][]> subSampleBlocks(List<long[][]> originalBlockList){
List<long[][]> downsampledBlockList = new ArrayList<long[][]>();
for (long[][] anOriginalBlockList : originalBlockList) {
downsampledBlockList.add(subSample420(anOriginalBlockList));
}
return downsampledBlockList;
}
现在我完成子采样之后,我必须将4x4 2d阵列的大小调整回原始大小,这意味着通过替换单个值将4x4 2d阵列列表转换回8x8 2d阵列列表从4x4开始,具有相同值的2x2块。执行此操作的两种方法如下:
public static long[][] resizeSubsampledBlocks(long[][] subBlocks){
long[][] resizedBlocks = new long[8][8];
int nr = 0;
long[] pixel = new long[16];
for (int i = 0; i < 4; i++){
for (int j = 0; j < 4; j++){
pixel[nr] = subBlocks[i][j];
nr++;
}
}
nr = 0;
for (int i = 0; i < 8; i += 2){
for (int j = 0; j < 8; j += 2){
resizedBlocks[i][j] = pixel[nr];
resizedBlocks[i][j+1] = pixel[nr];
resizedBlocks[i+1][j] = pixel[nr];
resizedBlocks[i+1][j+1] = pixel[nr];
nr++;
}
}
return resizedBlocks;
}
public static List<long[][]> reziseSubsampledBlockList(List<long[][]> subBlocks){
List<long[][]> rezisedBlocks = new ArrayList<long[][]>();
for (long[][] subBlock : subBlocks) {
rezisedBlocks.add(resizeSubsampledBlocks(subBlock));
}
return rezisedBlocks;
}
但是当我尝试使用以下方法测试所有这些时:
private static void testOrderSub(int nr){
List<long[][]> testList = new ArrayList<long[][]>();
for (int i = 0; i < nr; i++){
long[][] matrix = {
{i, i, i+1, i+1, i+2, i+2, i+3, i+3},
{i, i, i+1, i+1, i+2, i+2, i+3, i+3},
{i+4, i+4, i+5, i+5, i+6, i+6, i+7, i+7},
{i+4, i+4, i+5, i+5, i+6, i+6, i+7, i+7},
{i+8, i+8, i+9, i+9, i+10, i+10, i+11, i+11},
{i+8, i+8, i+9, i+9, i+10, i+10, i+11, i+11},
{i+12, i+12, i+13, i+13, i+14, i+14, i+15, i+15},
{i+12, i+12, i+13, i+13, i+14, i+14, i+15, i+15}};
testList.add(matrix);
}
List<long[][]> subTest = FirstLevelEncoder.subSampleBlocks(testList);
List<long[][]> sizeTest = FirstLevelDecoder.reziseSubsampledBlockList(subTest);
for (int i = 0; i < nr; i++) {
if (!(Arrays.equals(testList.get(i), sizeTest.get(i)))){
System.out.println("false");
}
}
}
sizeTest.equals(testList)
返回false,即使通过查看调试器它们似乎具有相同的值。我尝试使用这些方法处理图像,输出也搞砸了。难道我做错了什么?为什么列表不相等?
答案 0 :(得分:1)
对于Java-Array,equals()的测试类似于array1 == array2。它检查它是否是同一个对象。它不会检查它是否具有相同的内容。
你必须使用
Arrays.equals(array1, array2);
以下是关于Java-Arrays的另一种解释:equals vs Arrays.equals in Java