Breaking block into quadrants

百般思念 提交于 2019-12-24 15:22:20

问题


So lets say I have a block that is 10x10, my goal is the break this into quadrants and then once I have done that, go back to the first quadrant and break and into four quadrants, go to the second quadrants and break that into four quadrants and so on until they are all broken and then go back to the first and repeat it for the new set of blocks.

So essentially I want to break a block into 4 pieces and break each new block into four pieces and keep going with this pattern.

This is the code I have so far:

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

        int red = 0;
        int green = 0;
        int blue = 0;
        int rgb = 0;

        if(endHeight <= 1 || endWidth <= 1) {
            return;
        }

        // get average
        for(int i = startHeight; i < endHeight; i++) {
            for(int j = startWidth; j < endWidth; j++) {
                rgb = img.getRGB(j, i);
                red += (rgb >> 16) & 0xFF;
                green += (rgb >> 8) & 0xFF;
                blue += (rgb) & 0xFF;
            }
        }

        // get average color
        red = red /((startWidth - endWidth) * (startHeight - endHeight));
        green = green/((startWidth - endWidth) * (startHeight - endHeight));
        blue = blue/((startWidth - endWidth) * (startHeight - endHeight));
        Color color = new Color(red,green,blue);

        // apply 
        for(int i = startHeight; i < endHeight; i++) {
            for(int j = startWidth; j < endWidth; j++) {
                blockImg.setRGB(j, i, color.getRGB());
            }
        }

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


    }

So what I am doing is trying to recursively call this function which will continue to break each block into quadrants but I continue to get a stack overflow.

What my code does is takes an image, gets the average colour of that block and displays it. It is a relatively simple concept that I am going to tweek a bit to get some cool images but that is for later, right now I am trying to fix this issue.

Edit here are the results of System.out.println(startHeight + " " + endHeight + " "+ startWidth + " " + endWidth);

0 72 0 108
0 36 0 54
0 18 0 27
0 9 0 13
0 4 0 6
0 2 0 3
0 1 0 1
0 1 2 3
3 4 0 3
3 4 0 1
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3
3 4 2 3

and then 3 4 2 4 repeats until I get the stack overflow.


回答1:


Three features are important when doing recursion:

  1. a breaking condition
  2. the actual workload
  3. collating the results of recursions

Specifically:

private static void getBlockAverage(int startHeight, int endHeight, int startWidth, int endWidth, BufferedImage img, BufferedImage blockImg) {
    // break recursion on empty block
    if(endHeight <= startHeight || endWidth <= startWidth)
        return;

    if(startHeight + 1 == endHeight || startWidth + 1 == endWidth) {
        // work on single columns or rows of pixels
        // results are stored to blockImg...
    } else {
        // recurse
        getBlockAverage(startHeight, (startHeight + endHeight)/2, startWidth, (startWidth + endWidth)/2, img, blockImg);
        getBlockAverage((startHeight + endHeight)/2, endHeight, startWidth, (startWidth + endWidth)/2, img, blockImg);
        getBlockAverage(startHeight, (startHeight + endHeight)/2, (startWidth + endWidth)/2, endWidth, img, blockImg);
        getBlockAverage((startHeight + endHeight)/2, endHeight, (startWidth + endWidth)/2, endWidth, img, blockImg);
        // now collate the results in blockImg...
    }
}


来源:https://stackoverflow.com/questions/33828524/breaking-block-into-quadrants

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!