[英]Getting pixel RGB from a bufferedImage from the mouses X and Y position
[英]Getting Greyscale pixel value from RGB colourspace in Java using BufferedImage
有人知道將從<BufferedImage> getRGB(i,j)
返回的RGBint值轉換為灰度值的簡單方法嗎?
我只是通過使用它來分解它們來簡單地平均RGB值;
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
然后平均紅色,綠色,藍色。
但我覺得這樣一個簡單的操作我必須遺漏一些東西......
在對一個不同的問題做出了很好的回答后,我應該清楚自己想要什么。
我想從getRGB(i,j)返回RGB值,並將其轉換為0-255范圍內的白色值,表示該像素的“暗度”。
這可以通過平均等來實現,但我正在尋找一個OTS實現來節省幾行。
本教程介紹了3種方法:
通過改變ColorSpace
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(cs, null);
BufferedImage image = op.filter(bufferedImage, null);
通過繪制到灰度BufferedImage
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_GRAY);
Graphics g = image.getGraphics();
g.drawImage(colorImage, 0, 0, null);
g.dispose();
通過使用GrayFilter
ImageFilter filter = new GrayFilter(true, 50);
ImageProducer producer = new FilteredImageSource(colorImage.getSource(), filter);
Image image = this.createImage(producer);
這並不像聽起來那么簡單,因為對於如何將顏色映射到灰度,沒有100%正確的答案。
我使用的方法是將RGB轉換為HSL然后轉換為S部分(並且可選地轉換回RGB),但這可能不是您想要的。 (它相當於最高和最低rgb值的平均值,因此與所有3的平均值略有不同)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.