我想在8x8二維數組列表上進行子採樣。更確切地說,從8x8開始,我想通過逐個2x2塊變換爲4x4,並將2x2的算術平均值保存在4x4的正確位置。我這樣做後,輸出將是一個4x4二維數組列表。以下兩種方法都利用這個二維數組子採樣 - java
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;
}
護理現在,我做了二次取樣後,我不得不調整4×4二維數組回其初始大小,這意味着改造4×4二維數組的列表返回到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,即使看在調試器,他們似乎具有相同的值。我試圖使用這些方法來處理圖像,輸出也被搞亂了。難道我做錯了什麼?爲什麼列表不相同?
因爲正如我已經說過,它看起來在調試器好嗎,我看不出什麼毛病。 – Pred
明白了:當你做list.equals(otherList)...歸結爲array1.equals(array2)...並且不起作用 - 因爲這不會執行逐個元素的比較。你需要使用Arrays.equals() – GhostCat
你是對的,我改變它:'for(int i = 0; i
Pred