多次将阵列分成象限

时间:2015-11-28 10:11:33

标签: java arrays bitmap

我的目标是我有一个吐出位图的图像。现在我希望将图像的平均颜色显示为一个巨大的像素。这是一个相当简单的任务,只需使用bufferImage并获取我获取每个红色,绿色和蓝色值的位图,将其全部加起来然后除以图片的分辨率。

事情是这样做之后,我想将图像分成四个象限并取每个象限的平均颜色并显示它。再次打破四个象限中的每一个并做同样的事情。我面临的问题是我使用的是递归语句,它执行以下操作:

private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {
        if(endHeight <= startHeight || endWidth <= startWidth) {
           counter++;
           return;
        }
        // get quadrant pixel average and display, I deleted this portion of the code just to keep things compact

        getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
        getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg, color);
        getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
        getBlockAverage((startHeight+endHeight)/2, endHeight, (startWidth+endWidth)/2, endWidth, img, blockImg, color);
    }

很容易看出这不是我想要的,因为递归语句将一直执行getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg, color);直到它完成然后移动到下一个。这不是我想要的。我希望将图像分解为4个象限,然后将每个象限分解,直到所有象限都被分解并继续。

例如:

从1个象限开始,分为4.现在为象限1,将其分解为4,现在为象限2,将其分解为4,现在为象限3,将其分解为4,现在为象限4,将其分解为4。

现在我正在思考它,我觉得我应该使用某种for循环,并且迭代次数上限,但我不知道该怎么做。

1 个答案:

答案 0 :(得分:1)

我倾向于同意你的观点。我想我也会把这个方法放到循环中,但也要让方法将每个象限的平均颜色返回到一个单维数组中,并考虑每个数组索引是一个象限数,并且该索引的实际元素包含颜色对于那个特定的象限。这样,您就可以处理稍后获取的所有相关信息。我至少会接受它,然后一旦它以我想要的方式工作就进行优化。嗯,这就是我如何做到的:P

当然,我在整个过程中假设象限解剖流程类似于我在下图中显示的内容:

enter image description here

以下是我要做的事情:

更改 getBlockAverage()方法,使其返回颜色...

private static Color getBlockAverage(int startHeight, int endHeight, int startWidth, 
            int endWidth, BufferedImage img, BufferedImage blockImg, Color oldAvg) {    

    // get quadrant pixel average color and return it
    // with whatever code you've been using....

    return theQuadrantAverageColor;
}

然后我会创建另一个方法,其中包含我们的循环,图像象限解剖维度,并调用 getBlockAverage()方法,而循环很好...循环,并为每个循环周期放置从 getBlockAverage()方法返回到每个建立的颜色数组中的颜色:

private static void getQuadrantsColorAverages(Color[] quadrantColors, BufferedImage img) {
    // Decalre and Initialize required variables.
    BufferedImage wrkImg = img;
    BufferedImage blockImg = null; //?????
    int imgWidth = wrkImg.getWidth();
    int imgHeight = wrkImg.getHeight();
    int startHeight = 0;
    int endHeight = 0;
    int startWidth = 0;
    int endWidth = 0;
    Color oldAvg = null;
    int quadCount = 1;

    // Start our loop and and continue it until our counter 
    // variable named quadCount goes over 20....
    while (quadCount <= 20) {
        // Handle dissectional dimensions (in pixels)
        // for quadrants 1 to 20 as layed out within
        // the supplied image to this forum post.
        switch (quadCount) {
            // Quadrant 1
            case 1:
                startHeight = 0; endHeight = (imgHeight / 2);
                startWidth = 0; endWidth = (imgWidth / 2);
            // Quadrant 2
            case 2: 
                startWidth = (endWidth + 1); endWidth = imgWidth; 
                break;
            // Quadrant 3
            case 3: 
                startHeight = (endHeight + 1); endHeight = imgHeight;
                startWidth = 0; endWidth = (imgWidth / 2);
                break;
            // Quadrant 4
            case 4:
                startWidth = (endWidth + 1); endWidth = imgWidth; 
                break;
            // Quadrant 5
            case 5:
                startHeight = 0; endHeight = (imgHeight / 4);
                startWidth = 0; endWidth = (imgWidth / 4);
                break;
            // Quadrant 6
            case 6:
                startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
                break;
            // Quadrant 7
            case 7:
                startHeight = (endHeight + 1); endHeight = (imgHeight / 2);
                startWidth = 0; endWidth = (imgWidth / 4);
                break;
            // Quadrant 8
            case 8:
                startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
                break;
            // Quadrant 9
            case 9:
                startHeight = 0; endHeight = (imgHeight / 4);
                startWidth = (endWidth + 1); endWidth = ((imgWidth / 4) * 3);
                break;
            // Quadrant 10
            case 10:
                startWidth = (endWidth + 1); endWidth = imgWidth;
                break;
            // Quadrant 11
            case 11:
                startHeight = (imgHeight / 4); endHeight = (imgHeight / 2);
                startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
                break;
            // Quadrant 12
            case 12:
                startWidth = (endWidth + 1); endWidth = imgWidth;
                break;
            // Quadrant 13
            case 13:
                startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
                startWidth = 0; endWidth = (imgWidth / 4);
                break;
            // Quadrant 14
            case 14:
                startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
                break;
            // Quadrant 15
            case 15:
                startHeight = (endHeight + 1); endHeight = imgHeight;
                startWidth = 0; endWidth = (imgWidth / 4);
                break;
            // Quadrant 16
            case 16:
                startWidth = (endWidth + 1); endWidth = (imgWidth / 2);
                break;
            // Quadrant 17
            case 17:
                startHeight = (imgHeight / 2); endHeight = ((imgHeight / 4) * 3);
                startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
                break;
            // Quadrant 18
            case 18:
                startWidth = (endWidth + 1); endWidth = imgWidth;
                break;
            // Quadrant 19
            case 19:
                startHeight = (endHeight + 1); endHeight = imgHeight;
                startWidth = (imgWidth / 2); endWidth = ((imgWidth / 4) * 3);
                break;
            // Quadrant 20
            case 20:
                startWidth = (endWidth + 1); endWidth = imgWidth;
                break;
        }

        // Maintain the oldAvg Color variable
        oldAvg = getBlockAverage(startHeight, endHeight, startWidth, 
                                 endWidth, img, blockImg, oldAvg);
        // We subtract 1 from quadCount below to accomodate
        // our Array indexing which must start at 0.
        quadrantColors[quadCount - 1] = oldAvg;
        // increment our quadrant counter by 1.
        quadCount++;
    }
}

然后从你的应用程序的某个地方开始我会这样做:

// We declare our array to handle 20 elements since
// the image will be dissected into 20 quadrants.
Color[] quadrantColors = new Color[20];

BufferedImage img = null;

// Fill our Color Array...
getQuadrantsColorAverages(quadrantColors, img);

// Let's see what we managed to get....
for (int i = 0; i < quadrantColors.length; i++) {
    Color clr =  quadrantColors[i];
    int red = clr.getRed();
    int green = clr.getGreen();
    int blue = clr.getBlue();

    System.out.println("The average color for Quadrant #" + 
       (i + 1) + " is: RGB[" + red + "," + green + "," + blue + "]");
}

嗯......这就是QQCompi。我希望它能帮到你一点点。