【问题标题】:How to extract Y, Cb and Cr color components?如何提取 Y、Cb 和 Cr 颜色分量?
【发布时间】:2016-08-10 21:37:28
【问题描述】:

我需要将给定的彩色图片分解为三张单独的图片,以便将每个颜色分量(Y、Cb、Cr)存储在一张图片中,如here。 也许有一个想法,我怎样才能得到这三张照片
分别是 Y、Cb 还是 Cr 颜色分量?通过以下代码,我可以读出文件并将颜色模型从 RGB 转换为 YCbCr。

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SpaceConverter {

    static int [] colorComponentsYCbCr = new int[3];
    static int [] colorComponentsRGB = new int[3];


    public static void getRGBComponents (int color)
    {
        colorComponentsRGB [0] = (color & 0xff);
        colorComponentsRGB [1] = (color & 0x00ff) >> 8;
        colorComponentsRGB [2] = (color & 0x0000ff) >> 16;
    }

    public static void convertYCbCr2RGB(int [] componentsYCbCrToConvert)
    {
        int Y = componentsYCbCrToConvert [0];
        int Cb = componentsYCbCrToConvert [1];
        int Cr = componentsYCbCrToConvert [2];

        colorComponentsRGB = new int [3];
        colorComponentsRGB [0] = (int) (Y                        +   1.402 * (Cr - 128));
        colorComponentsRGB [1] = (int) (Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128));
        colorComponentsRGB [2] = (int) (Y + 1.772   * (Cb - 128));
    }

    public static void convertRGB2YCbCr(int [] componentsRGB)
    {
        int blue = componentsRGB [0];
        int green = componentsRGB [1];
        int red = componentsRGB [2];

        colorComponentsYCbCr [0] = (int) (0.299     *   red + 0.587 * green + 0.114 * blue);
        colorComponentsYCbCr [1] = (int) (128-0.169 *   red-0.331   * green + 0.500 * blue);
        colorComponentsYCbCr [2] = (int) (128+0.500 *   red - 0.419 * green - 0.081 * blue);
    }

    public static void getColoredCrPicture(BufferedImage image)
    {
        File f = null;
        // get width and height
        int width = image.getWidth();
        int height = image.getHeight();     

        for (int y = 0; y<height; y++)
        {
            for (int x = 0; x<width; x++)
            {
                int color = image.getRGB(x, y);
                getRGBComponents(color);
                convertRGB2YCbCr(colorComponentsRGB);

                int Y = colorComponentsYCbCr[0];
                int Cb = colorComponentsYCbCr[1];
                int Cr = colorComponentsYCbCr[2];   
                Y = 0;
                Cb = 0;

                int p = (Y << 24) | (Cb << 16) | (Cr<<8);           
                image.setRGB(x, y, p);
            }
        }
        try
        {
            f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/outputX.jpg");
            ImageIO.write(image, "jpg", f);
        }
        catch(IOException e)
        {
            System.out.println(e);
        }
    }

    public static void getColoredCbPicture(BufferedImage image)
    {
       File f = null;
       // get width and height
       int width = image.getWidth();
       int height = image.getHeight();

       for (int y = 0; y<height; y++)
       {
           for (int x = 0; x<width; x++)
           {
                int color = image.getRGB(x, y);
                getRGBComponents(color);
                convertRGB2YCbCr(colorComponentsRGB);

                int Y = colorComponentsYCbCr[0];
                int Cb = colorComponentsYCbCr[1];
                int Cr = colorComponentsYCbCr[2];   

                Y = 0;
                Cr = 0;
                int p = (Y << 24) | (Cb<< 16) | (Cr <<8);
                image.setRGB(x, y, p);
           }
        }
        try
        {
           f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/outputCb.jpg");
           ImageIO.write(image, "jpg", f);
           System.out.println("WRITE Status: OK");
        }
        catch(IOException e)
        {
           System.out.println(e);
        }

    }

    public static BufferedImage loadPicture()
    {
        File f = null;
        BufferedImage img = null;

        // read Image
        try
        {
            f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/VILLA.JPG");
            img = ImageIO.read(f);
            System.out.println("READ Status: OK");
            getColoredCbPicture(img);
        }   
        catch(IOException e)
        {
            System.out.println(e);
        }
        return img;
    }

    public static void main(String[] args)
    {
        BufferedImage image = null;
        loadPicture();
        getColoredCbPicture(image);
    }
}

【问题讨论】:

  • 您的问题并不完全清楚。如果我理解正确,在计算每个像素的 Y、Cb 和 Cr 数字之后,您必须弄清楚如何将每个通道转换回 RGB 表示,然后您可以将其写成 JPEG。

标签: java colors rgb format-conversion


【解决方案1】:

听起来您要做的是获取 RGB 图像,将其转换为 YCbCr 并将 YCbCr 中的三个通道中的每一个显示为单独的 RGB 图像。

您已经有了从 RGB 转换为 YCbCr 的代码。您还需要执行反向转换的代码,以便您可以从 YCbCr 转换为 RGB。

您将希望使用相同的逻辑,但实际上创建了三个 Y'CrCb 图像:(Y, 0, 0)、(0, Cb, 0) 和 (0, 0, Cr)。然后将这三个图像中的每一个都转换为 RGB。这三个图像将是三个 YCbCr 通道中每个通道的 RGB 表示。

【讨论】:

  • 我已经编辑了我的代码,所以现在它还可以选择将 YCbCr 转换为 RGB。当我启动程序时,它会为“Cb-picture”创建一个红色图片。我认为这是因为int p = (Y &lt;&lt; 24) | (Cb&lt;&lt; 16) | (Cr &lt;&lt;8); image.setRGB(x, y, p); 有什么方法可以与YCbCr 组件一起使用吗?例如。当我在进行 RGB2YCbCr 对话后,我 Y=0; Cb=0; Cr=123; and then set p = (Y &lt;&lt; 24) | (Cb&lt;&lt; 16) | (Cr &lt;&lt;8); image.setRGB(x, y, p); 并不意味着我只是设置 R=0; G=0;和蓝色分量到 123?
  • 请帮忙!程序对我来说并不重要。我只需要这三张图片的 Y、Cb 和 Cr 来在我的 PowerPoint 演示文稿中谈论 JPEG-Standard,直到今天晚上:(
猜你喜欢
  • 2013-05-08
  • 1970-01-01
  • 2014-08-15
  • 2020-06-09
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多