This is what I have so far, but don't know what to do now? It makes the picture bigger but there is a lot of spaces in the picture. How do you copy pixels to fill in the holes?
这是我到目前为止,但不知道现在该怎么办?它使图片更大,但图片中有很多空格。如何复制像素以填充孔?
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;
}
2 个解决方案
#1
1
Well if you enlarge an image times two, and you don't use interpolation. Then a pixel (x,y)
of the original image, should be mapped to pixels (2*x,2*y)
, (2*x,2*y+1)
, (2*x+1,2*y)
and (2*x+1,2*y+1)
. So the algorithm should be:
好吧,如果你将图像放大两倍,并且不使用插值。那么原始图像的像素(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);
for(int x2 = 2*x; x2 < 2*x+2; x2++) {
for(int y2 = 2*y; y2 < 2*y+2; y2++) {
enlarged.getPixel(x2,y2).setColor(orig.getColor());
}
}
}
}
Or more generic, with a magnification parameter mag
:
或者更通用,使用放大参数mag:
Picture enlarged = new Picture(getWidth() * mag, getHeight() * mag);
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Pixel orig = getPixel(x,y);
for(int x2 = mag*x; x2 < mag*x+mag; x2++) {
for(int y2 = mag*y; y2 < mag*y+mag; y2++) {
enlarged.getPixel(x2,y2).setColor(orig.getColor());
}
}
}
}
#2
1
The reason there are gaps in your new picture is because you are only setting the pixel once for each original pixel, whereas you have to set 4 pixels (i.e. 2x2) for each pixel of the original image, since it's twice as big.
新图片中存在间隙的原因是因为您只为每个原始像素设置一次像素,而您必须为原始图像的每个像素设置4个像素(即2x2),因为它是两倍大。
#1
1
Well if you enlarge an image times two, and you don't use interpolation. Then a pixel (x,y)
of the original image, should be mapped to pixels (2*x,2*y)
, (2*x,2*y+1)
, (2*x+1,2*y)
and (2*x+1,2*y+1)
. So the algorithm should be:
好吧,如果你将图像放大两倍,并且不使用插值。那么原始图像的像素(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);
for(int x2 = 2*x; x2 < 2*x+2; x2++) {
for(int y2 = 2*y; y2 < 2*y+2; y2++) {
enlarged.getPixel(x2,y2).setColor(orig.getColor());
}
}
}
}
Or more generic, with a magnification parameter mag
:
或者更通用,使用放大参数mag:
Picture enlarged = new Picture(getWidth() * mag, getHeight() * mag);
for (int x = 0; x < getWidth(); x++) {
for (int y = 0; y < getHeight(); y++) {
Pixel orig = getPixel(x,y);
for(int x2 = mag*x; x2 < mag*x+mag; x2++) {
for(int y2 = mag*y; y2 < mag*y+mag; y2++) {
enlarged.getPixel(x2,y2).setColor(orig.getColor());
}
}
}
}
#2
1
The reason there are gaps in your new picture is because you are only setting the pixel once for each original pixel, whereas you have to set 4 pixels (i.e. 2x2) for each pixel of the original image, since it's twice as big.
新图片中存在间隙的原因是因为您只为每个原始像素设置一次像素,而您必须为原始图像的每个像素设置4个像素(即2x2),因为它是两倍大。