Java BuffereImage设置RGB奇怪结果
我尝试使用Java BuffereImage设置RGB奇怪结果,java,image,image-processing,bufferedimage,Java,Image,Image Processing,Bufferedimage,我尝试使用setRGB和BufferedImage在Java中旋转图像,但得到了一个奇怪的结果。有人知道为什么吗 BufferedImage pic1 = ImageIO.read(new File("Images/Input-1.bmp")); int width = pic1.getWidth(null); int height = pic1.getHeight(null); double angle = Math.toRadians(90); doub
setRGB
和BufferedImage
在Java中旋转图像,但得到了一个奇怪的结果。有人知道为什么吗
BufferedImage pic1 = ImageIO.read(new File("Images/Input-1.bmp"));
int width = pic1.getWidth(null);
int height = pic1.getHeight(null);
double angle = Math.toRadians(90);
double sin = Math.sin(angle);
double cos = Math.cos(angle);
double x0 = 0.5 * (width - 1); // point to rotate about
double y0 = 0.5 * (height - 1); // center of image
BufferedImage pic2 = pic1;
// rotation
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
double a = x - x0;
double b = y - y0;
int xx = (int) (+a * cos - b * sin + x0);
int yy = (int) (+a * sin + b * cos + y0);
if (xx >= 0 && xx < width && yy >= 0 && yy < height) {
pic2.setRGB(x, y, pic1.getRGB(xx, yy));
}
}
}
ImageIO.write(pic2, "bmp", new File("Images/Output2.bmp"));
BufferedImage pic1=ImageIO.read(新文件(“Images/Input-1.bmp”);
int-width=pic1.getWidth(null);
int height=pic1.getHeight(null);
双角度=数学托拉迪安(90);
双正弦=数学正弦(角度);
双余弦=数学余弦(角度);
双x0=0.5*(宽度-1);//指向旋转
双y0=0.5*(高度-1);//图像中心
缓冲图像pic2=pic1;
//轮换
对于(int x=0;x=0&&xx=0&&yy
在左侧是原始图片,在右侧是我的结果。有人知道我怎么修吗?
谢谢您的帮助。问题在于您使用的图像与输入和输出的图像相同: 缓冲图像pic2=pic1 必须为pic2创建另一个图像,然后进行旋转,将像素从Image1复制到Image2
但是,请注意,使用getRGB和setRGB的速度非常慢。如果您直接操作像素,速度将提高100倍。您无法在适当的位置旋转图像。创建另一个单独的旋转图像。如果要将图像旋转90度,则不必进行旋转数学。只需将x坐标复制到新的y坐标,将y坐标复制到新的x坐标。谢谢,我没有注意到这个错误:D现在可以工作了。关于setRBG和getRGB。。。我知道他们太慢了。。。。但我不知道如何将图像转换成像素[]。我在互联网上搜索过每一个地方,我看到的都是如何从图像中获取一维数组,我指的是像素[]。但我需要一个像像素[][]这样的2D数组,我不知道如何获得它。。。。你能帮我做这个吗在这张照片中,您可以创建一个像素图像: