[英]How to convert pixels to gray scale?
Ok, I am using Processing
which allows me to access pixels of any image as int[]
. 好吧,我正在使用
Processing
,它允许我以int[]
访问任何图像的像素。 What I now want to do is to convert the image to gray-scale. 我现在想要做的是将图像转换为灰度。 Each pixel has a structure as shown below:
每个像素的结构如下所示:
...........PIXEL............
[red | green | blue | alpha]
<-8--><--8---><--8--><--8-->
Now, what transformations do I need to apply to individual RGB values to make the image gray-scale ?? 现在,我需要将哪些转换应用于单个RGB值以使图像成为灰度?
What I mean is, how much do I add / subtract to make the image gray-scale ? 我的意思是,我添加/减少多少使图像灰度化?
I found a few methods here: http://www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/ 我在这里找到了一些方法: http : //www.johndcook.com/blog/2009/08/24/algorithms-convert-color-grayscale/
For each pixel, the value for the red, green and blue channels should be their averages. 对于每个像素,红色,绿色和蓝色通道的值应为其平均值。 Like this:
像这样:
int red = pixel.R;
int green = pixel.G;
int blue = pixel.B;
pixel.R = pixel.G = pixel.B = (red + green + blue) / 3;
Since in your case the pixel colors seem to be stored in an array rather than in properties, your code could end up looking like: 因为在你的情况下像素颜色似乎存储在一个数组而不是属性中,你的代码最终可能看起来像:
int red = pixel[0];
int green = pixel[1];
int blue = pixel[2];
pixel[0] = pixel[1] = pixel[2] = (red + green + blue) / 3;
The general idea is that when you have a gray scale image, each pixel's color measures only the intensity of light at that point - and the way we perceive that is the average of the intensity for each color channel. 一般的想法是,当你有一个灰度图像时,每个像素的颜色仅测量该点的光强度 - 我们感知的方式是每个颜色通道的强度平均值。
The following code loads an image and cycle through its pixels , changing the saturation to zero and keeping the same hue and brightness values. 以下代码加载图像并循环显示其像素 ,将饱和度更改为零并保持相同的色调和亮度值。
PImage img;
void setup () {
colorMode(HSB, 100);
img = loadImage ("img.png");
size(img.width,img.height);
color sat = color (0,0,0);
img.loadPixels();
for (int i = 0; i < width * height; i++) {
img.pixels[i]=color (hue(img.pixels[i]), sat, brightness(img.pixels[i]));
}
img.updatePixels();
image(img,0,0);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.