gpt4 book ai didi

java - 将数组多次分成象限

转载 作者:行者123 更新时间:2023-12-01 10:50:01 26 4
gpt4 key购买 nike

我的目标是拥有一张可以吐出位图的图像。现在我想将图像的平均颜色显示为一个巨大的像素。这是一个相当简单的任务,只需使用 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 循环,但我不知道该怎么做。

最佳答案

我倾向于同意你的观点。我想我也会将此方法放入循环中,但也会使该方法将每个象限的平均颜色返回到一维数组中,并考虑每个数组索引是一个象限编号,并且该索引的实际元素包含颜色对于那个特定的象限。这样您就可以使用稍后获得的所有相关信息。我至少会让它继续运行,然后在它按照我想要的方式工作后对其进行优化。好吧,我就是这么做的:P

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

enter image description here

这是我要做的:

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

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。希望对您有一点帮助。

关于java - 将数组多次分成象限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970115/

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