Java 平均阵列中的像素数据

Java 平均阵列中的像素数据,java,rgb,pixels,Java,Rgb,Pixels,我正在尝试解决一个读取图像的问题。我需要排除像素中的蓝色通道,这样它将产生RG图像 这就是我到目前为止所做的,但我没有工作 我是否需要创建一个新的BuffereImage并将每个平均像素(没有蓝色通道)应用到它?不过我不知道该怎么做。对于初学者,请将第二行到最后一行代码更改为这一行,看看它是否有帮助: image.setRGB(i, j, (redData[i][j] << 16) | (greenData[i][j] << 8)); 以下是您的代码的更新版本(未经测试

我正在尝试解决一个读取图像的问题。我需要排除像素中的蓝色通道,这样它将产生RG图像

这就是我到目前为止所做的,但我没有工作


我是否需要创建一个新的BuffereImage并将每个平均像素(没有蓝色通道)应用到它?不过我不知道该怎么做。

对于初学者,请将第二行到最后一行代码更改为这一行,看看它是否有帮助:

image.setRGB(i, j, (redData[i][j] << 16) | (greenData[i][j] << 8));
以下是您的代码的更新版本(未经测试):

公共静态BuffereImage isolateBlueChannelAndResize(BuffereImage图像){
int imageWidth=image.getWidth();
int imageHeight=image.getHeight();
//奇数大小的图像向下舍入,以防止索引外部图像
如果((imageWidth&1)>0)imageWidth--;
如果((imageHeight&1)>0)imageHeight--;
BuffereImage newImage=新的BuffereImage(imageWidth/2,imageHeight/2,image.getType());
对于(int i=0;i>16)和0xff;
int r2=(image.getRGB(i+1,j)>>16)和0xff;
intr3=(image.getRGB(i,j+1)>>16)&0xff;
intr4=(image.getRGB(i+1,j+1)>>16)&0xff;
int red=(r1+r2+r3+r4+3)/4;//+3四舍五入(相当于ceil())
intg1=(image.getRGB(i,j)>>8)&0xff;
intg2=(image.getRGB(i+1,j)>>8)&0xff;
intg3=(image.getRGB(i,j+1)>>8)&0xff;
intg4=(image.getRGB(i+1,j+1)>>8)&0xff;
绿色整数=(g1+g2+g3+g4+3)/4;//+3四舍五入(相当于ceil())

setRGB(i/2,j/2,(红色我真的认为更简单的方法是使用
Color
类:

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AverageOutBlue {
    public static final float AVG_FACTOR = 1f / (4f * 255f);

    public static BufferedImage processImage(final BufferedImage src) {
        final int w = src.getWidth();
        final int h = src.getHeight();

        final BufferedImage out = new BufferedImage(w / 2, h / 2, src.getType());

        for (int i = 0; i < w; i += 2)
            for (int j = 0; j < h; j += 2) {
                final Color color = avgColor(src, i, j);
                out.setRGB(i / 2, j / 2, color.getRGB());
            }

        return out;
    }

    private static Color avgColor(final BufferedImage src, final int i,
        final int j) {

        final Color c1 = new Color(src.getRGB(i, j));
        final Color c2 = new Color(src.getRGB(i + 1, j));
        final Color c3 = new Color(src.getRGB(i + 1, j));
        final Color c4 = new Color(src.getRGB(i + 1, j + 1));

        final float r = (c1.getRed() + c2.getRed() + c3.getRed() + c4.getRed())
                            * AVG_FACTOR;
        final float g = (c1.getGreen() + c2.getGreen() + c3.getGreen() + 
                            c4.getGreen()) * AVG_FACTOR;

        return new Color(r, g, 0f);
    }
}
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
公共类平均值为蓝色{
公共静态最终浮动平均系数=1f/(4f*255f);
公共静态BuffereImage processImage(最终BuffereImage src){
final int w=src.getWidth();
final int h=src.getHeight();
final BufferedImage out=新的BufferedImage(w/2,h/2,src.getType());
对于(int i=0;i
因此,您希望拍摄一幅2mX2n像素的图像,并将其转换为一幅mXn图像,每个像素(最终)是原始图像的平均像素减去蓝色分量(即蓝色分量始终为零)?是的,基本上就是这样!但我似乎不知道怎么做。:SA注释:平均RGB值会略微恶化图像质量。RGB值与亮度没有线性关系(已应用伽马曲线)。结果比应该的暗。但正确操作需要花费大量时间,而且速度要慢得多。如果您感兴趣,请看一看:@ErwinBolwidt平均RGB值足以用图像(缩略图)重建图像,因为每个像素都相当忠实,所以我质疑“正确”操作的优点。谢谢,让我试着实现它;您是否将显示
返回图像;
的行更改为
返回新图像;
?;)@Jared同意了……我在80年代开始编程,当时编写可读性更强的代码和性能更高的代码的奢侈并不存在。因此,我仍然对抛出一堆不必要的对象和函数调用感到有点不安。特别是如果你试图处理一堆巨大的图像,速度确实开始起作用。而且,我只是卡住了使用OP在问题中使用的方法…@Jared不知道他们为什么不提供这些方法…但我肯定某个Apache Commons库确实提供了这些方法。;)我的猜测是:如果您开始使用这些函数(尤其是数组版本),这是因为您需要快速执行大量处理。然后,您最不想做的就是用方法调用替换简单的移位操作…事实上,我甚至不应该调用getRGB(…)对于我上面代码中的同一个像素,我希望Java有一个@Inline注释来强制编译器这样做。但不幸的是,它没有,而且你不能依赖编译器在那里做任何事情:。因此,根据你将要运行的JVM,任何事情都可能发生。不知道,例如,Android的Dalvik将如何处理如果你坚决反对使用
Color
类,那么你可以在尝试使用
int
RGB值时平均给定
int
RGB值的像素o,然后返回一个整数,而不是
颜色
对象。
public static BufferedImage isolateBlueChannelAndResize(BufferedImage image) {
    int imageWidth  = image.getWidth();
    int imageHeight = image.getHeight();
    // Round down for odd-sized images to prevent indexing outside image
    if ((imageWidth  & 1) > 0) imageWidth --;
    if ((imageHeight & 1) > 0) imageHeight--;

    BufferedImage newImage = new BufferedImage(imageWidth/2, imageHeight/2, image.getType());

    for (int i = 0; i < imageWidth; i += 2) {
        for (int j = 0; j < imageHeight; j += 2) {
            int r1 = (image.getRGB(i  , j  ) >> 16) & 0xff;
            int r2 = (image.getRGB(i+1, j  ) >> 16) & 0xff;
            int r3 = (image.getRGB(i  , j+1) >> 16) & 0xff;
            int r4 = (image.getRGB(i+1, j+1) >> 16) & 0xff;
            int red = (r1 + r2 + r3 + r4 + 3) / 4;   // +3 rounds up (equivalent to ceil())

            int g1 = (image.getRGB(i  , j  ) >>  8) & 0xff;
            int g2 = (image.getRGB(i+1, j  ) >>  8) & 0xff;
            int g3 = (image.getRGB(i  , j+1) >>  8) & 0xff;
            int g4 = (image.getRGB(i+1, j+1) >>  8) & 0xff;
            int green = (g1 + g2 + g3 + g4 + 3) / 4;   // +3 rounds up (equivalent to ceil()) 

            newImage.setRGB(i/2, j/2, (red << 16) | (green << 8));
        }
    }

    return newImage;
}
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class AverageOutBlue {
    public static final float AVG_FACTOR = 1f / (4f * 255f);

    public static BufferedImage processImage(final BufferedImage src) {
        final int w = src.getWidth();
        final int h = src.getHeight();

        final BufferedImage out = new BufferedImage(w / 2, h / 2, src.getType());

        for (int i = 0; i < w; i += 2)
            for (int j = 0; j < h; j += 2) {
                final Color color = avgColor(src, i, j);
                out.setRGB(i / 2, j / 2, color.getRGB());
            }

        return out;
    }

    private static Color avgColor(final BufferedImage src, final int i,
        final int j) {

        final Color c1 = new Color(src.getRGB(i, j));
        final Color c2 = new Color(src.getRGB(i + 1, j));
        final Color c3 = new Color(src.getRGB(i + 1, j));
        final Color c4 = new Color(src.getRGB(i + 1, j + 1));

        final float r = (c1.getRed() + c2.getRed() + c3.getRed() + c4.getRed())
                            * AVG_FACTOR;
        final float g = (c1.getGreen() + c2.getGreen() + c3.getGreen() + 
                            c4.getGreen()) * AVG_FACTOR;

        return new Color(r, g, 0f);
    }
}