BufferedImage - 灰度
给定一个灰度图像,我如何获得该位置灰度的像素值?
这始终将温度输出为 -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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在创建一个新的空白图像并将其分配给您的类变量:
像素是使用默认值创建的,并且逻辑上它们在打印它们时都具有相同的颜色(黑色),因为您没有操纵尚未在任何像素中着色。
另外,如果宽度不等于高度,您的代码可能会失败。根据您的 for 循环,i 沿宽度运行,j 沿高度运行。因此,你应该更改
为
you are creating a new blank image and assigning it to your class variable:
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
to