2017-04-27 40 views
0

我想在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,即使看在調試器,他們似乎具有相同的值。我試圖使用這些方法來處理圖像,輸出也被搞亂了。難道我做錯了什麼?爲什麼列表不相同?

+0

因爲正如我已經說過,它看起來在調試器好嗎,我看不出什麼毛病。 – Pred

+0

明白了:當你做list.equals(otherList)...歸結爲array1.equals(array2)...並且不起作用 - 因爲這不會執行逐個元素的比較。你需要使用Arrays.equals() – GhostCat

+0

你是對的,我改變它:'for(int i = 0; i Pred

回答

1

對於Java-Array equals()正在測試像array1 == array2。它檢查它是否是同一個對象。它不檢查它是否具有相同的內容。

你必須使用

Arrays.equals(array1, array2); 

這裏是關於Java的數組另一種解釋:equals vs Arrays.equals in Java

+0

謝謝,我也試過你的方法,在原帖中查看編輯過的測試方法。它說陣列不相同。我認爲我在二次採樣/調整大小的方法中做了錯誤的事情,但我看不出什麼。 – Pred

+0

再次感謝,我發現問題,它在代碼中的其他地方。 – Pred