转换图像的阵列矩阵RGB Java来灰阶 [英] Convert Image to Grayscale with array matrix RGB java
本文介绍了转换图像的阵列矩阵RGB Java来灰阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建一个图像过滤程序,我想彩色图片转换为灰度图片使用数组矩阵的帮助。
I'm creating an Image filter program and I want to convert a coloured picture to a grayscale picture with the help of an array matrix.
这是我目前:
import java.awt.Color;
import se.lth.cs.ptdc.images.ImageFilter;
public class GrayScaleFilter extends ImageFilter {
public GrayScaleFilter(String name){
super(name);
}
public Color[][] apply(Color[][] inPixels, double paramValue){
int height = inPixels.length;
int width = inPixels[0].length;
Color[][] outPixels = new Color[height][width];
for (int i = 0; i < 256; i++) {
grayLevels[i] = new Color(i, i, i);
}
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
Color pixel = inPixels[i][j];
outPixels[i][j] = grayLevels[index];
}
}
return outPixels;
}
}
它看起来像我应该使用这个公式:((R + G + B)/ 3)
It looks like I'm supposed to use this formula: ((R+G+B)/3)
我要创建这样一个数组矩阵:
I want to create an array matrix like this:
Color[] grayLevels = new Color[256];
// creates the color (0,0,0) and puts it in grayLevels[0],
// (1,1,1) in grayLevels[1], ..., (255,255,255) in grayLevels[255]
这是我闯民宅太时,我想用grascale类:
This is the class I'm refering too when I want to use grascale:
public abstract Color[][] apply(Color[][] inPixels, double paramValue);
protected short[][] computeIntensity(Color[][] pixels) {
int height = pixels.length;
int width = pixels[0].length;
short[][] intensity = new short[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = pixels[i][j];
intensity[i][j] = (short) ((c.getRed() + c.getGreen() + c
.getBlue()) / 3);
}
}
return intensity;
}
我如何能做到这一点任何反馈?而不是使用 outPixels [I] [J] =新的色彩(强度,力度,强度);
推荐答案
构建 grayLevels
数组是这样的:
for (int i = 0; i < 256; i++) {
grayLevels[i] = new Color(i, i, i);
}
然后,当你需要某一种颜色,只需检索它为 grayLevels [指数]
。
这篇关于转换图像的阵列矩阵RGB Java来灰阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文