gpt4 book ai didi

java - 二维数组子采样 - java

转载 作者:行者123 更新时间:2023-12-02 13:19:04 25 4
gpt4 key购买 nike

我正在尝试对 8x8 二维数组列表进行子采样。更准确地说,我想从 8x8 转换为 4x4,方法是逐一取出 2x2 block ,并将 2x2 的算术平均值保存在 4x4 中的正确位置。执行此操作后,输出将是 4x4 二维数组的列表。以下两个方法正在处理这个问题

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 与相同值的 2x2 block 。执行此操作的两种方法如下:

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,即使通过在调试器中查看它们似乎具有相同的值。我尝试使用这些方法来处理图像,但输出也很困惑。难道我做错了什么?为什么列表不相等?

最佳答案

对于 Java 数组 equals() 的测试类似于 array1==array2。它检查是否是同一个对象。它不检查是否具有相同的内容。

你必须使用

Arrays.equals(array1, array2);

这是关于 Java 数组的另一个解释:equals vs Arrays.equals in Java

关于java - 二维数组子采样 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43658314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com