如何在JAVA的3个矩阵中分别显示由R G B值表示的图像

时间:2013-03-10 12:41:20

标签: java image image-processing rgb grayscale

我有3个相同大小的2D矩阵(比方说200行和300列)。每个矩阵代表三种“基本”颜色(红色,绿色和蓝色)之一的值。矩阵的值可以在0到255之间。现在我想组合这些矩阵以将它们显示为彩色图像(200乘300像素)。我怎么能在JAVA中做到这一点?

1 个答案:

答案 0 :(得分:3)

首先:您可以从以下值创建颜色,如:

<强> Color c = new Color(red, green, blue, alpha);

请注意:

  1. 红色是Matrics1的值
  2. 绿色是Matrics2的值
  3. 蓝色是Matrics3的值
  4. 然后创建新图片:

    BufferedImage image = new BufferedImage(200/*Width*/, 300/*height*/, BufferedImage.TYPE_INT_ARGB);

    然后在图像上设置值,如下所示:

    image.setRGB(x, y, c.getRGB());
    

    这是此步骤的代码,请尝试:

    public class Main {
    
        public static void main(String args[]) throws IOException {
            int red[][] = new int[200][300];
            int green[][] = new int[200][300];
            int blue[][] = new int[200][300];
            /////////////////set this matrices 
    
            BufferedImage image = new BufferedImage(200/*Width*/, 300/*height*/, BufferedImage.TYPE_INT_ARGB);
    
            for (int i = 0; i < 200; i++) {
                for (int j = 0; j < 300; j++) {
                    Color c = new Color(red[i][j], green[i][j], blue[i][j]);
                    image.setRGB(i, j, c.getRGB());
                }
            }
            ImageIO.write(image, "jpg", new File("/////////////image path.jpg"));
        }
    }