简体   繁体   English

如何以矩阵形式(xy平面)使像素在java的[1-255]范围内?

[英]How to make pixel be in range [1-255] in java in matrix form (x-y plane)?

I have an image of (512 pixels * 512 pixels) of type PNG. 我有一个PNG类型的图像(512像素* 512像素)。 I knew that each pixel is of type 8 bits. 我知道每个像素都是8位类型。 So, we have range [0-255]. 因此,我们的范围是[0-255]。 the question is I want the row and column of this image such that the each cell has a number in between [0-255]. 问题是我要此图像的行和列,以使每个单元格的数字都在[0-255]之间。

Unfortunately, my program works fine without error, but the bad news is that it doesn't come with what I want. 不幸的是,我的程序运行正常,没有错误,但坏消息是它没有我想要的。 the output for each cell there is 7 numbers ie 2894893 -2829100 -2829100 -2894893 -2894893 -2960686 -2960686 -2960686 -3092272 -3223858 -3289651 -3421237 -4144960 -3684409 -3552823 -4144960 -4802890 -5263441 etc. 每个单元的输出有7个数字,即2894893 -2829100 -2829100 -2894893 -2894893 -2960686 -2960686 -2960686 -3092272 -3223858 -3289651 -3421237 -4144960 -3684409 -3552823 -4144960 -4802890 -5263441等

what I want is only a range between [1-255]? 我想要的只是[1-255]之间的范围? ie instead of the above output, we should have something like 23 182 33 250 etc. 即代替上面的输出,我们应该有类似23 182 33 250之类的东西。

remember that I need this using 2-dimensional instead of 1-dimensional (means array[row] [column] instead of array [index]). 请记住,我需要使用2维而不是1维(意味着array [row] [column]而不是array [index])。 Here is the code: 这是代码:

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

    }

This method is taken from the member @davenpcj from getting pixel data from an image using java . 此方法来自成员@davenpcj,该成员使用java从图像中获取像素数据

Thank you 谢谢

What you are getting are RGB values, where each of the three components is stored as a byte. 您得到的是RGB值,三个分量中的每个分量都存储为一个字节。 That is, the integer consists of 4 bytes, each one for the R, G, B and alpha components of the pixel. 也就是说,整数由4个字节组成,每个像素分别代表像素的R,G,B和alpha分量。 For example, the pixel value 2894893 looks like this as binary: 例如,像素值2894893如下所示:

 00000000 00101100 00101100 00101101

You can get the individual channels by masking the integer pixel value: 您可以通过屏蔽整数像素值来获得各个通道:

int red = (pix[count] & 0xFF);
int green = (pix[count] >> 8) & 0xFF;    
int blue = (pix[count] >> 16) & 0xFF;
int alpha = (pix[count] >> 24) & 0xFF;

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

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