如何分别获取图像的rgb矩阵?

Glt*_*Btn 4 java

我有一个图像数据库,我想将这些图像的RGB矩阵分别存储在mysql数据库中(例如:redMatrix_column,greenMatrix_column,blueMatrix_column).在matlab中,我可以使用imread()函数分别获取RBG矩阵.如何在java中执行此操作?谢谢你的帮助.

Raf*_*ele 6

这是你获得颜色组件的方法:

public class GetImageColorComponents {
  public static void main(String... args) throws Exception {
    BufferedImage img = ImageIO.read(GetImageColorComponents.class
                                     .getResourceAsStream("/image.png"));
    int[] colors = new int[img.getWidth() * img.getHeight()];
    img.getRGB(0, 0, img.getWidth(), img.getHeight(), colors, 0, img.getWidth());

    int[] red = new int[colors.length];
    int[] green = new int[colors.length];
    int[] blue = new int[colors.length];

    for (int i = 0; i < colors.length; i++) {
      Color color = new Color(colors[i]);
      red[i] = color.getRed();
      green[i] = color.getGreen();
      blue[i] = color.getBlue();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有关保存和检索MySQL数据库中字节的完整示例,请参阅此要点.