3

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

这始终将 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);
        }
    }
}
4

1 回答 1

2

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

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

像素是使用默认值创建的,并且在您打印它们的阶段它们都具有相同的颜色(黑色),因为您还没有操作任何像素中的颜色,因此它们是合乎逻辑的。

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

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

int temp = imgMazeImage.getRGB(i, j);
于 2011-09-14T00:55:58.893 回答