将块分成象限 [英] Breaking block into quadrants

查看:57
本文介绍了将块分成象限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以假设我有一个 10x10 的块,我的目标是将其分成多个象限,然后一旦完成,返回第一象限并分成四个象限,转到第二象限并打破分成四个象限,依此类推,直到它们都被破坏,然后回到第一个象限,对新的一组块重复此操作.

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.

所以基本上我想把一个块分成 4 块,然后把每个新块分成四块,然后继续这个模式.

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.

这是我目前的代码:

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.

这里编辑的结果是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

然后重复 3 4 2 4 直到堆栈溢出.

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

推荐答案

进行递归时三个特性很重要:

Three features are important when doing recursion:

  1. 破坏条件
  2. 实际工作量
  3. 整理递归的结果

特别是:

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...
    }
}

这篇关于将块分成象限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆