我需要将给定的彩色图片分解为三张单独的图片,以便将每种颜色成分(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);
    }
}

最佳答案

听起来您想要做的是拍摄RGB图像,将其转换为YCbCr,然后将YCbCr中的三个通道分别显示为单独的RGB图像。

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

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

10-05 22:43
查看更多