使用setRGB后如何在java中显示灰度图像?

发布于 2024-12-27 12:05:19 字数 1377 浏览 3 评论 0原文

我在做一些图像处理程序时遇到了显示灰度图像的问题。我所做的是,我使用 getRGB 提取像素值,然后尝试更改这些像素值。但是在更改这些像素值后,我仍然得到之前缓冲的相同图像。我的代码部分如下: -

  int [] rgbArray=new int[width*height];
  BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  buffer = ImageIO.read(new File(file));


  //Before changing the pixel values
  buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
  int a = (0xff000000 & rgbArray[0]) >>> 24;
  int r = (0x00ff0000 & rgbArray[0]) >> 16;
  int g = (0x0000ff00 & rgbArray[0]) >> 8;
  int b = (0x000000ff & rgbArray[0]);
  System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
  System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
  for(int i = 0 ; i<width*height; i++)
   {
     rgbArray[0]=0x808080;  //Changing pixel values to grey
   }
  buffer.setRGB(0, 0, width, height, rgbArray , 0,width);


  //After changing the pixel values
  buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
  a = (0xff000000 & rgbArray[0]) >>> 24;
  r = (0x00ff0000 & rgbArray[0]) >> 16;
  g = (0x0000ff00 & rgbArray[0]) >> 8;
  b = (0x000000ff & rgbArray[0]);
  System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
  System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);

像素值的输出对于原始像素和新像素都是正确的,但我无法查看新图像,它显示旧图像,即原始图像显示而不是改变了图像。

M working on some image processing program and encountered a problem of displaying the greyscale image. What i did was, i extracted the pixel values using getRGB and then tried to change these pixel values. But after changing these pixel value i'm still getting the same image which was buffered before. My part of code is given below:-

  int [] rgbArray=new int[width*height];
  BufferedImage buffer = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  buffer = ImageIO.read(new File(file));


  //Before changing the pixel values
  buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
  int a = (0xff000000 & rgbArray[0]) >>> 24;
  int r = (0x00ff0000 & rgbArray[0]) >> 16;
  int g = (0x0000ff00 & rgbArray[0]) >> 8;
  int b = (0x000000ff & rgbArray[0]);
  System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
  System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);
  for(int i = 0 ; i<width*height; i++)
   {
     rgbArray[0]=0x808080;  //Changing pixel values to grey
   }
  buffer.setRGB(0, 0, width, height, rgbArray , 0,width);


  //After changing the pixel values
  buffer.getRGB(0, 0, width, height, rgbArray , 0,width );
  a = (0xff000000 & rgbArray[0]) >>> 24;
  r = (0x00ff0000 & rgbArray[0]) >> 16;
  g = (0x0000ff00 & rgbArray[0]) >> 8;
  b = (0x000000ff & rgbArray[0]);
  System.out.println("a " + a + " r " + r + " g " + g + " b " + b);
  System.out.println("rgbArray["+0+"] = "+ rgbArray[0]);

Output for the pixel value is coming out to be correct for both original and new pixels but i'm not able to view the new image, it is showing the old i.e original image is shown not the changed image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不再让梦枯萎 2025-01-03 12:05:19
rgbArray[0]=0x808080;  //Changing pixel values to grey

您只需将第一个像素更改为灰色即可。你可能想要做什么:

rgbArray[i]=0x808080;  //Changing pixel values to grey

这会将每个像素更改为灰色。

// 编辑:

当然,那么每个像素都是完全相同的(灰色),这可以用更少的代码来完成,例如:

    Graphics2D g2 = buff.createGraphics();
    g2.setColor(Color.grey);
    g2.fillRect(0, 0, buff.getWidth(), buff.getHeight());
    g2.dispose();

如果您想要一张黑白图片而不是彩色图片,请尝试:

    BufferedImageOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    buffer =  op.filter(buffer, null);
rgbArray[0]=0x808080;  //Changing pixel values to grey

there you are only changig the first pixel to gray. what you probably meant to do:

rgbArray[i]=0x808080;  //Changing pixel values to grey

which would change every pixel to gray.

// edit:

Of course, then every pixel is exactly the same (grey), which can be done with way less code, e.g.:

    Graphics2D g2 = buff.createGraphics();
    g2.setColor(Color.grey);
    g2.fillRect(0, 0, buff.getWidth(), buff.getHeight());
    g2.dispose();

If you want a black and white picture instead of a color one, try:

    BufferedImageOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
    buffer =  op.filter(buffer, null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文