gpt4 book ai didi

java - 在java中将图像大小调整为2倍

转载 作者:行者123 更新时间:2023-11-30 02:34:59 25 4
gpt4 key购买 nike

这是我到目前为止所拥有的,但不知道现在该怎么办?它使图片变大,但图片中有很多空间。如何复制像素来填充孔?

public Picture enlarge()
{
Picture enlarged = new Picture(getWidth() * 2, getHeight() * 2);

for (int x = 0; x < getWidth(); x++)
{
for (int y = 0; y < getHeight(); y++)
{
Pixel orig = getPixel(x,y);
Pixel enlargedPix = enlarged.getPixel(x*2,y*2);
Pixel enlargedPix2 = enlarged. getPixel(x,y);
enlargedPix.setColor(orig.getColor());
enlargedPix2.setColor(orig.getColor());
}
}
return enlarged;
}

最佳答案

好吧,如果您将图像放大两倍,并且不使用插值。然后原始图像的像素 (x,y) 应该映射到像素 (2*x,2*y), (2*x, 2*y+1)(2*x+1,2*y)(2*x+1,2*y+1) 。所以算法应该是:

Picture enlarged = new Picture(getWidth() * 2, getHeight() * 2);
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Pixel orig = getPixel(x,y);
<b>for(int x2 = 2*x; x2 < 2*x+2; x2++) {</b>
<b>for(int y2 = 2*y; y2 < 2*y+2; y2++) {</b>
enlarged.getPixel(<b>x2,y2</b>).setColor(orig.getColor());
<b>}</b>
<b>}</b>
}
}

或者更通用,带有放大参数mag:

Picture enlarged = new Picture(getWidth() * <b>mag</b>, getHeight() * <b>mag</b>);
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Pixel orig = getPixel(x,y);
for(int x2 = <b>mag</b>*x; x2 < <b>mag</b>*x+<b>mag</b>; x2++) {
for(int y2 = <b>mag</b>*y; y2 < <b>mag</b>*y+<b>mag</b>; y2++) {
enlarged.getPixel(x2,y2).setColor(orig.getColor());
}
}
}
}

关于java - 在java中将图像大小调整为2倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43326214/

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