BufferedImage - 灰度

发布于 2024-12-04 10:57:47 字数 622 浏览 0 评论 0原文

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

这始终将温度输出为 -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);
        }
    }
}

Given an image that is in grayscale, how would I get the pixel values of the grayscale at that location?

This outputs temp as -16777216 (black) all the time.

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);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮光之海 2024-12-11 10:57:47

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

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

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

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

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

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

you are creating a new blank image and assigning it to your class variable:

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

the pixels are created with a default value and its logical that they all have the same color (black) at the stage when you are printing them since you did not manipulate the color in any pixel yet.

also, your code might fail if the width is not equal to the height. according to your for loops, i runs along width and j runs along height. therefore, you should change

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

to

int temp = imgMazeImage.getRGB(i, j);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文