gpt4 book ai didi

java - 获取图像的RGB-YCbCr分量

转载 作者:行者123 更新时间:2023-12-01 15:28:17 28 4
gpt4 key购买 nike

我想将 jpeg 图像 R_G_B channel 显示为 jlabel 和 Y-Cb-Cr channel 中的分离图像,我得到了数组,但不知道如何将它们转换为图像///

编辑:非常感谢,这就是我正在写的方法,现在它可以只显示图像的左上四分之一,并以蓝色显示,无论颜色 channel 是什么?

      public void getRGB_YCC(int width,int height,String inFileName) {
R=new int[height][width];G=new int[height][width];
B=new int[height][width];Y=new int[height][width];
Cb1=new int[height][width];Cr1=new int[height][width];
final int values[] = new int[width * height];
int r, g, b, Y_ch,Cb,Cr, y, x;

final PixelGrabber grabber = new PixelGrabber(image.getSource(), 0, 0,width,height, values, 0, width);

try {
if (grabber.grabPixels() != true) {
try {
throw new AWTException("Grabber returned false: " + grabber.getStatus());
} catch (final Exception e) {};
}
} catch (final InterruptedException e) {};
int index = 0;
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
r = values[index] >> 16 & 0xff;
g = values[index] >> 8 & 0xff;
b = values[index] & 0xff;

Y_ch= (int)(0.299 * r + 0.587 * g + 0.114 * b);
Cb= 128 + (int) (-0.16874 * r - 0.33126 * g + 0.5 * b);
Cr= 128 + (int)(0.5 * r - 0.41869 * g - 0.08131 * b);
R [y][x]=r;
G [y][x]=g;
B [y][x]=b;
Y [y][x]=Y_ch;
Cb1[y][x]=Cb;
Cr1[y][x]=Cr;
index++;
}
}
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
for( y=0;y<height;y++)
{
for( x=0;x<width;x++)
{
pixels[x + y*width] =R[y][x]<<16;
}
}

jLabel15.setIcon(new ImageIcon(img));

}

最佳答案

将像素数组放入图像中的一种简单快速的方法是使用 BufferedImage :

此示例创建一个灰度 8 位图像并为其检索“像素缓冲区”:

BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
byte[] pixels = ((DataBufferByte)img.getRaster().getDataBuffer()).getData();

这也适用于 RGB:

BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int[] pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();

现在,您只需写入 pixels 数组即可设置像素,例如 pixels[x + y * w] = value,结果立即可见。

关于java - 获取图像的RGB-YCbCr分量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9904816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com