如何使矩阵形式(x-y平面)中的像素在java范围内[1-255]?
How to make pixel be in range [1-255] in java in matrix form (x-y plane)?
我有一张 PNG 类型的(512 像素 * 512 像素)图像。我知道每个像素都是 8 位类型的。所以,我们的范围是 [0-255]。问题是我想要这张图片的行和列,这样每个单元格都有一个介于 [0-255].
之间的数字
不幸的是,我的程序运行良好,没有错误,但坏消息是它没有我想要的。 the output for each cell there is 7 numbers i.e. 2894893 -2829100 -2829100 -2894893 -2894893 -2960686 -2960686 -2960686 -3092272 -3223858 -3289651 -3421237 -4144960 -3684409 -3552823 -4144960 -4802890 -5263441 etc.
我要的只是[1-255]之间的范围?
即代替上面的输出,我们应该有类似的东西
23 182 33 250 等等
记住我需要使用二维而不是一维(意味着数组[行] [列]而不是数组[索引])。
这是代码:
ima = ImageIO.read(new File("how.png"));
int [] pix = ima.getRGB(0, 0, ima.getWidth(), ima.getHeight(), null, 0, ima.getWidth());
int count=0;
for (int i=0; i < ima.getHeight() ; i++)
{
for (int j=0; j < ima.getWidth() ; j++){
System.out.print(pix[count]+" ");
count++;
}
System.out.println();
}
此方法取自getting pixel data from an image using java.
的成员@davenpcj
谢谢
您得到的是 RGB 值,其中三个分量中的每一个都存储为一个字节。也就是说,整数由 4 个字节组成,每个字节用于像素的 R、G、B 和 alpha 分量。例如,像素值 2894893 看起来像这样的二进制:
00000000 00101100 00101100 00101101
您可以通过屏蔽整数像素值来获得各个通道:
int red = (pix[count] & 0xFF);
int green = (pix[count] >> 8) & 0xFF;
int blue = (pix[count] >> 16) & 0xFF;
int alpha = (pix[count] >> 24) & 0xFF;
我有一张 PNG 类型的(512 像素 * 512 像素)图像。我知道每个像素都是 8 位类型的。所以,我们的范围是 [0-255]。问题是我想要这张图片的行和列,这样每个单元格都有一个介于 [0-255].
之间的数字不幸的是,我的程序运行良好,没有错误,但坏消息是它没有我想要的。 the output for each cell there is 7 numbers i.e. 2894893 -2829100 -2829100 -2894893 -2894893 -2960686 -2960686 -2960686 -3092272 -3223858 -3289651 -3421237 -4144960 -3684409 -3552823 -4144960 -4802890 -5263441 etc.
我要的只是[1-255]之间的范围? 即代替上面的输出,我们应该有类似的东西 23 182 33 250 等等
记住我需要使用二维而不是一维(意味着数组[行] [列]而不是数组[索引])。 这是代码:
ima = ImageIO.read(new File("how.png"));
int [] pix = ima.getRGB(0, 0, ima.getWidth(), ima.getHeight(), null, 0, ima.getWidth());
int count=0;
for (int i=0; i < ima.getHeight() ; i++)
{
for (int j=0; j < ima.getWidth() ; j++){
System.out.print(pix[count]+" ");
count++;
}
System.out.println();
}
此方法取自getting pixel data from an image using java.
的成员@davenpcj谢谢
您得到的是 RGB 值,其中三个分量中的每一个都存储为一个字节。也就是说,整数由 4 个字节组成,每个字节用于像素的 R、G、B 和 alpha 分量。例如,像素值 2894893 看起来像这样的二进制:
00000000 00101100 00101100 00101101
您可以通过屏蔽整数像素值来获得各个通道:
int red = (pix[count] & 0xFF);
int green = (pix[count] >> 8) & 0xFF;
int blue = (pix[count] >> 16) & 0xFF;
int alpha = (pix[count] >> 24) & 0xFF;