繁体   English   中英

获取图像的RGB-YCbCr分量

[英]get RGB-YCbCr components of image

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

编辑:thanX非常多,这是我正在编写的方法,现在它可以只显示图像的左上四分之一,并以蓝色显示它,而不管颜色通道是什么?

      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[x + y * w] = value类的pixels数组即可设置像素,结果立即可见。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM