繁体   English   中英

Java:将彩色图像表示为灰度强度值的数组

[英]Java: represent a color image as an array of gray scale intensity values

是否有内置的工具将颜色(或灰度图像)表示为灰度强度值的数组?

如果没有,假设我有一个BufferedImage img ,如何将这个图像表示为这样的数组:

int[][] intensity = toIntensity(img);

toIntensity()方法应如何显示?

有一种方法可以不做您的toIntensity()事情:

public static BufferedImage convertToType(BufferedImage sourceImage, int targetType) {
        BufferedImage image;
        if (sourceImage.getType() == targetType) {
            image = sourceImage;
        }else {
            image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), targetType);
            image.getGraphics().drawImage(sourceImage, 0, 0, null);
        }
        return image;
    }

使用targetType调用此方法: BufferedImage.TYPE_BYTE_GRAY如果需要其他类型的方法,则可以将其转换回原样,并保持其灰色。

toIntensity方法可能是这样的

public int[][] toIntensity(BufferedImage img){
        int[][] cols = new int[img.getWidth()][img.getHeight()];
            for(int z = 0;z < img.getWidth();z++){
                for(int a = 0;a < img.getHeight();a++){
                    int color = img.getRGB(z, a);
                    cols[z][a] = color;
                }
            }
            return cols;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM