简体   繁体   English

java-将像素值转换为0到255

[英]java - Converting Pixel values to 0 to 255

Okay I have extracted RGB values from a file using the follwing code-: 好的,我已经使用以下代码从文件中提取了RGB值:

                    int r = cBuffer[1024*0+32*(j/32)+(j%32)] & 0xFF;
                    int g = cBuffer[1024*1+32*(j/32)+(j%32)] & 0xFF;
                    int b = cBuffer[1024*2+32*(j/32)+(j%32)] & 0xFF;

The "anding" with 0xFF is done because I want to convert the byte value to an unsigned representation. 完成0xFF的“与”运算是因为我想将字节值转换为无符号表示形式。

Now I form the actual pixel using the following code-: 现在,我使用以下代码形成实际像素:

int pixel = (r << 16) | (g << 8) | b;

Now the problem is when I print( using SOP(pixel) ) the value of the pixel I get numbers like the following-: 现在的问题是当我打印( 使用SOP(pixel) )时,像素的值会得到如下数字:

Computed Pixel = -11119787
Computed Pixel = -13884637
Computed Pixel = -14081500
Computed Pixel = -12436417

I know the Pixel calculation is right since the images that I get after using these values are valid. 我知道像素计算是正确的,因为使用这些值后得到的图像是有效的。

Now my questions are-: 现在我的问题是:

  • Why are there such arbitrary values of the pixel ? 为什么会有像素的任意值? Shouldn't a pixel value range from 0 to 255 ? 像素值应该在0到255之间吗?
  • Is there any way I can represent a pixel value between 0to 255 given the rgb values ? 给定rgb值,有什么办法可以表示0到255之间的像素值? Would that even be a correct representation ? 那会是正确的表示吗?

Shouldn't a pixel value range from 0 to 255? 像素值是否应该在0到255之间?

No and you should know that already since you're working with shifting bytes etc. 0-255 would be the value range for an unsigned byte and since pixel values typically consist of 3 8-bit (ie 3 byte) values the resulting int will have a much higher value in absolute terms. 否,您应该已经知道,因为您正在处理字节移位等问题,所以0-255将是无符号字节的值范围,并且由于像素值通常由3个8位(即3字节)值组成,因此结果int绝对价值更高。

Is there any way I can represent a pixel value between 0to 255 given the rgb values ? 给定rgb值,有什么办法可以表示0到255之间的像素值? Would that even be a correct representation ? 那会是正确的表示吗?

No, as said above a 24-bit pixel value can't be represented in the range 0 - 255. You'd typically represent each component (r,g,b) in that range, ie a pixel would consist of 3 values in the range 0-255. 不,如上所述,不能在0到255的范围内表示24位像素值。通常应表示该范围内的每个分量(r,g,b),即,一个像素将由3个值组成范围0-255。

There are representations that would allow you to map a pixel to 0-255 but you'd lose a lot of information since you'd either have to convert it to grayscale or "compress" the rgb values to 2 or 3-bit values which clearly can't hold as much information as 8-bit values. 有一些表示可以将像素映射到0-255,但是您会丢失很多信息,因为您必须将其转换为灰度或将rgb值“压缩”为2或3位值,显然不能容纳8位值那么多的信息。

Btw, I'm not sure your rgb-extraction routine is correct or at least it seems to be very complicated (did you check the values are correct?). 顺便说一句,我不确定您的rgb-extraction例程是否正确或至少看起来很复杂(您是否检查了值是否正确?)。 Assuming cBuffer is a byte array that represents uncompressed 32-bit color information you'd probably just get the rgb values like this: 假设cBuffer是一个字节数组,表示未压缩的32位颜色信息,则您可能会得到如下的rgb值:

//assuming the buffer to be in rgba format, i.e. 32-bit pixels with alpha information in the last byte
for( int i = 0; i < cBuffer.length; i+=4) {
  int r = cBuffer[i];
  int b = cBuffer[i + 1];
  int g = cBuffer[i + 2];
  int a = cBuffer[i + 3];

  //do whatever you want with the pixel
}

Edit: 编辑:

According to your comment you want to get the color value in the range [0.0,1.0]. 根据您的评论,您希望获得[0.0,1.0]范围内的颜色值。 In that case you'd get the integer color value and divide that to the maximum color value (white). 在这种情况下,您将获得整数颜色值并将其除以最大颜色值(白色)。 Assuming you have 24-bit color (or 32-bit argb and ignore the alpha component) you'd do something like this: 假设您具有24位颜色(或32位argb并忽略了alpha分量),则可以执行以下操作:

int w = 255 << 16 | 255 << 8 | 255; //white = 0x00FFFFFF
int r = 255 << 16;  //red   = 0x00FF0000
int g = 255 << 8;   //green = 0x0000FF00
int b = 255;        //blue  = 0x000000FF

System.out.format( "white > int: %8d (real: %1.5f )\n", w, (double)w/w);
System.out.format( "red   > int: %8d (real: %1.5f )\n", r, (double)r/w);
System.out.format( "green > int: %8d (real: %1.5f )\n", g, (double)g/w);
System.out.format( "blue  > int: %8d (real: %1.5f )\n", b, (double)b/w);

Output: 输出:

white > int: 16777215 (real: 1,00000 )
red   > int: 16711680 (real: 0,99609 )
green > int:    65280 (real: 0,00389 )
blue  > int:      255 (real: 0,00002 )

Why are there such arbitrary values of the pixel ? 为什么会有像素的任意值? Shouldn't a pixel value range from 0 to 255 ? 像素值应该在0到255之间吗?

  • A pixel in a grayscale image is represented by 1 channel and hence 1 byte and therefore takes a value between 0 and 255. 灰度图像中的像素由1个通道表示,因此由1个字节表示,因此取值介于0到255之间。
  • A pixel in a color image is represented by 3 channels - R, G, B (and sometimes a 4th channel called alpha channel) and hence 3 consecutive bytes across the width of the image. 彩色图像中的一个像素由3个通道(R,G,B(有时称为第4个通道,称为Alpha通道))表示,因此在图像宽度上表示3个连续字节。 Therefore, such a pixel cannot be represented with a value between 0 and 255. However, the value for each of the three channels (R, G, B) is represented between 0 and 255. 因此,这样的像素不能用0到255之间的值表示。但是,三个通道(R,G,B)中每个通道的值都用0到255之间的值表示。

Is there any way I can represent a pixel value between 0to 255 given the rgb values ? 给定rgb值,有什么办法可以表示0到255之间的像素值? Would that even be a correct representation ? 那会是正确的表示吗?

Like I mentioned in the second point above, you can represent each of the RGB channels / bytes with a value between 0 and 255. Let's say you have read the first pixel (Left top corner of the image) in a byte array. 就像我在上面第二点提到的那样,您可以用0到255之间的值表示每个RGB通道/字节。假设您已读取字节数组中的第一个像素(图像的左上角)。

byte[] pixel = new byte[3];
/* code to read the pixel in this byte array */
int r = pixel[0] & 0xFF;
int g = pixel[1] & 0xFF;
int b = pixel[2] & 0xFF;

Note the "anding" of the channel byte with 0xFF (255). 注意通道字节的“与”为0xFF(255)。 This operation makes the byte value to unsigned value. 该操作使字节值变为无符号值。

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

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