gpt4 book ai didi

java - 生成的图像跳过像素

转载 作者:行者123 更新时间:2023-11-30 11:15:35 27 4
gpt4 key购买 nike

我正在编写一个程序来生成程序图像,但是当我运行它时,它会跳过像素。我的意思是生成的图像在像素级别看起来像棋盘。相反,我希望它是平滑的。

public class Test {
public static void main(String[] args) {
JFrame f = new JFrame();
BufferedImage bi = create(new BufferedImage(1024, 1024,
BufferedImage.TYPE_INT_ARGB));

f.getContentPane().add(new JLabel(new ImageIcon(bi)));
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);

save(bi, "name.png");
}

//creates the procedural image
private static BufferedImage create(BufferedImage bi) {

for (int i = 0; i < bi.getWidth(); i++) {
for (int j = 0; j < bi.getHeight(); j++) {
Context c = new Context(bi, i, j);

int red = red(i, j, c) + 128;
int green = green(i, j, c) + 128;
int blue = blue(i, j, c) + 128;

bi.setRGB(i, j, new Color(red, green, blue).getRGB());
}
}
return bi;
}

//saves image to file
private static void save(BufferedImage img, String filename) {
try {
File f = new File(filename);
System.out.println(f.getAbsolutePath());
ImageIO.write(img, "png", f);
} catch (IOException e) {
e.printStackTrace();
}
}

//PIXEL FUNCTIONS - returns the color for specified pixel

private static byte red(int i, int j, Context context) {
return (byte) (Context.average(context.north, context.west).getRed()
+ (Math.random() * 5.5) - 2);
}

private static byte green(int i, int j, Context context) {
return (byte) (Context.average(context.north, context.west).getGreen()
+ (Math.random() * 3.46) - 1);
}

private static byte blue(int i, int j, Context context) {
return (byte) (Context.average(context.north, context.west).getBlue()
+ (Math.random() * 17.5) - 8);
}

//Represents the surrounding pixels of the pixel passed into the constructor
static private class Context {

public Color north;
public Color northeast;
public Color east;
public Color southeast;
public Color south;
public Color southwest;
public Color west;
public Color northwest;

private static final Color DEFAULT = Color.GRAY;

//Returns the mean color
public static Color average(Color... colors) {
int redtot = 0;
int greentot = 0;
int bluetot = 0;

for (Color c : colors) {
redtot += c.getRed();
greentot += c.getGreen();
bluetot += c.getBlue();
}
int red = redtot / colors.length;
int green = greentot / colors.length;
int blue = bluetot / colors.length;

return new Color(red, green, blue);
}


public Context(BufferedImage image, int x, int y) {

north = DEFAULT;
northeast = DEFAULT;
east = DEFAULT;
southeast = DEFAULT;
south = DEFAULT;
southwest = DEFAULT;
west = DEFAULT;
northwest = DEFAULT;

try {
north = new Color(image.getRGB(x, y - 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
northeast = new Color(image.getRGB(x + 1, y - 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
east = new Color(image.getRGB(x + 1, y));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
southeast = new Color(image.getRGB(x + 1, y + 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
south = new Color(image.getRGB(x, y + 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
southwest = new Color(image.getRGB(x - 1, y + 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
west = new Color(image.getRGB(x - 1, y));
} catch (ArrayIndexOutOfBoundsException e) {
}
try {
northwest = new Color(image.getRGB(x - 1, y - 1));
} catch (ArrayIndexOutOfBoundsException e) {
}
}
}
}

这是生成的图像:generated image

这是放大的:zoom

是什么导致了这种行为?

最佳答案

我不熟悉您用于计算这些颜色的任何一种公式,但方格图案似乎来自您将 128 添加到每个颜色分量。可能一些溢出导致值的上下跳动......

int red = red(i, j, c) + 128;
int green = green(i, j, c) + 128;
int blue = blue(i, j, c) + 128;

至少,如果我删除 + 128,我会得到一个(对我来说)看起来正确的图像。尽管我不确定这对您尝试做的事情是否仍然有效。

这是我想到的快速修复方法,但可能还有其他更好的方法。使用 Math.max() 是因为至少第一个像素的无效值会引发异常。 (也许这就是您首先添加 128 的原因?)

int red = Math.max(0, red(i, j, c));
int green = Math.max(0, green(i, j, c));
int blue = Math.max(0, blue(i, j, c));

图像在放大时(对我而言)看起来很正常:

enter image description here

关于java - 生成的图像跳过像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25315197/

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