比较不断重新填充的2d阵列

时间:2012-12-11 19:31:26

标签: java android arrays math multidimensional-array

我正在为Android创建一个动作检测应用程序。虽然我的检测算法存在问题:

public boolean compareBitmaps()
{

    /*I'm creating 2 x 2D arrays which will continually be repopulated 
    every 2 frames with the pixel data of that frame based on even or odd
    (frames are collected in an ArrayList 'BIT') 
    BIT(0) will be stored in compare1
    BIT(1) will be stored in compare2
    BIT(2) will be stored in compare1 and so on...*/

    int [][] compare1 = new int[width][height];
    int [][] compare2 = new int[width][height];
    int bmpCount = BIT.size();

    boolean noMotion = true;

    //This is where I determine wheter even or odd using the modulus %
    for (int x=0; x<bmpCount; x++)
        if(x%2!=0)
        {

                System.out.println("Odd");
                getPixels1(compare1, x);

        }
        else
        {

                System.out.println("Even");
                getPixels2(compare2, x);

        }

        //Here I'm looking to continually compare the returned pixel colours
        // of the 2D arrays
        if(!Arrays.deepEquals(compare1, compare2))
        {
            System.out.println("No Motion");
            return noMotion = false;

        }
        else
        {
        return noMotion = true;
        }
}

private void getPixels1(int[][] compare1, int x) 
{
    for(int i = 0; i<width; i++)
    {
        for(int j=0; j<height; j++)
        {

            compare1[j][i] = BIT.get(x).getPixel(j, i);

        }
    }
}

private void getPixels2(int[][] compare2, int x) 
{
    for(int i = 0; i<width; i++)
    {
        for(int j=0; j<height; j++)
        {
            compare2[j][i] = BIT.get(x).getPixel(j, i);
        }
    }
}

我正在使用println()来帮助我调试, - 目前控制台打印“Odd”(如果我错了,请纠正我)对于元素(0)是错误的?当我下一步申请休息时。

任何人都可以看到我做错了什么,任何帮助都会受到赞赏

非常感谢,

1 个答案:

答案 0 :(得分:3)

你的逻辑是颠倒过来的。

如果x % 2返回0,则表示x可以被2整除,没有余数,即偶数

4 % 2 = 0 // even
5 % 2 = 1 // odd