gpt4 book ai didi

java - 如何提取Y、Cb、Cr颜色分量?

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

我需要将给定的彩色图片分解为三张单独的图片,以便每个颜色分量(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 中的三个 channel 中的每一个显示为单独的 RGB 图像。

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

您将想要使用相同的逻辑,但实际上创建三个 Y'CrCb 图像:(Y, 0, 0)、(0, Cb, 0) 和 (0, 0, Cr)。然后将这三个图像分别转换为 RGB。这三个图像将分别代表三个 YCbCr channel 。

关于java - 如何提取Y、Cb、Cr颜色分量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38883996/

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