使用BufferedImage从Java中获取RGB

使用BufferedImage从Java中获取RGB

本文介绍了使用BufferedImage从Java中获取RGB colourspace的灰度像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

任何人都知道转换从< BufferedImage>返回的RGBint值的简单方法。 getRGB(i,j)到一个灰度值?

Anyone know of a simple way of converting the RGBint value returned from <BufferedImage> getRGB(i,j) into a greyscale value?

我只想通过使用它来分解它们来平均RGB值;

I was going to simply average the RGB values by breaking them up using this;

int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;

然后是红色,绿色,蓝色。

and then average red,green,blue.

但我觉得这样一个简单的操作我一定会错过一些东西...

But i feel like for such a simple operation I must be missing something...

在对一个不同的问题做出了很好的回答之后,我应该清楚一下想要。

After a great answer to a different question, I should clear up what i want.

我想从getRGB(i,j)返回RGB值,并将其转换为0-255范围内的白色值,表示那个像素的黑暗。

I want to take the RGB value returned from getRGB(i,j), and turn that into a white-value in the range 0-255 representing the "Darkness" of that pixel.

这可以通过平均等来实现,但我正在寻找一个OTS实现来节省几行。

This can be accomplished by averaging and such but I am looking for an OTS implementation to save me a few lines.

推荐答案

这并不像听起来那么简单,因为

this isn't as simple as it sounds because there is no 100% correct answer for how to map a colour to greyscale.

方法我用的是将RGB转换为HSL然后转换为S部分(并且可选地转换回RGB),但这可能不是您想要的。 (它相当于最高和最低rgb值的平均值,因此与所有3的平均值略有不同)

the method i'd use is to convert RGB to HSL then 0 the S portion (and optionally convert back to RGB) but this might not be exactly what you want. (it is equivalent to the average of the highest and lowest rgb value so a little different to the average of all 3)

这篇关于使用BufferedImage从Java中获取RGB colourspace的灰度像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 15:24