【问题标题】:BufferedImage - getting the value of a pixel in grayscale color model pictureBufferedImage - 获取灰度彩色模型图片中某个像素的值
【发布时间】:2013-04-05 01:08:27
【问题描述】:

我有一个BufferedImage 使用此代码转换为灰度。我通常通过BufferedImage.getRGB(i,j) 和 gor 得到 R、G 和 B 的每个值的像素值。但是如何获取灰度图像中的像素值?

编辑:抱歉,忘记了转换。

static BufferedImage toGray(BufferedImage origPic) {
    BufferedImage pic = new BufferedImage(origPic.getWidth(), origPic.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    Graphics g = pic.getGraphics();
    g.drawImage(origPic, 0, 0, null);
    g.dispose();
    return pic;
}

【问题讨论】:

  • 你能贴出你用来转换它的代码吗?
  • "使用此代码。"代码在哪里?

标签: java pixel grayscale


【解决方案1】:

如果你有 RGB 图像,那么你可以得到这样的 (Red , green , blue , Gray) 值:

BufferedImage img;//////read the image
int rgb = img.getRGB(x, y);
int r = (rgb >> 16) & 0xFF;
int g = (rgb >> 8) & 0xFF;
int b = (rgb & 0xFF);

灰色是 (r , g , b) 的平均值,如下所示:

int gray = (r + g + b) / 3;

但如果将 RGB 图像(24 位)转换为灰度图像(8 位):

int gray= img.getRGB(x, y)& 0xFF;/////////will be the gray value

【讨论】:

  • 有兴趣了解如何获取像素的 alpha 值
  • @SriHarshaChilakapati alpha 为 (rgb >> 24) & 0xFF
  • 嘿,Alya,现在我有了这个,如何创建具有新灰度值的 RGB?
  • 从灰度图像中获取 RGB 是不可能的,因为你没有信息去做它,得到灰色,所以你将使用 3 个值来获取它(R,G,B)你得到灰色,您将丢失值信息
  • 阅读此答案stackoverflow.com/questions/11226074/…,并在谷歌上搜索以了解为什么您无法进行此转换,或者至少返回您拥有的彩色图像
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 2015-11-20
  • 2015-08-31
  • 2013-09-16
  • 2013-10-28
  • 1970-01-01
  • 2015-01-31
  • 2013-07-28
相关资源
最近更新 更多