繁体   English   中英

BufferedImage - 灰度

[英]BufferedImage - Gray Scale

给定灰度图像,我如何获得该位置的灰度像素值?

这一直输出temp为-16777216 (黑色)。

public void testMethod()
{
    int width = imgMazeImage.getWidth();
    int height = imgMazeImage.getHeight();

    //Assign class variable as a new image with RGB formatting
    imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    for(int i=0; i < width; i ++){
        for(int j=0; j < height; j++)
        {
            //Grab and set the colors one-by-one
            inttemp = imgMazeImage.getRGB(j, i);
            System.out.println(temp);
        }
    }
}

您正在创建一个新的空白图像并将其分配给您的类变量:

imgMazeImage = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);

使用默认值创建像素,并且在打印它们时,它们在舞台上都具有相同的颜色(黑色),因为您尚未操纵任何像素中的颜色。

另外,如果宽度不等于高度,您的代码可能会失败。 根据你的for循环,我沿着宽度运行,j沿着高度运行。 因此,你应该改变

int temp = imgMazeImage.getRGB(j, i);

int temp = imgMazeImage.getRGB(i, j);

暂无
暂无

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

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