简体   繁体   English

将24位图像RGB转换为3位8色

[英]Convert 24-bit image RGB to 3-bit 8-color

I am able with an BufferedImage to obtain the RGB values for every pixel of a 640x640 image, the output looks like this: 我能够使用BufferedImage来获取640x640图像的每个像素的RGB值,输出如下所示:

[...]
Pixel :(640, 634) RGB: 166 94 82
Pixel :(640, 635) RGB: 166 94 80
Pixel :(640, 636) RGB: 163 91 77
Pixel :(640, 637) RGB: 157 88 73
Pixel :(640, 638) RGB: 157 88 73
Pixel :(640, 639) RGB: 159 90 74
Pixel :(640, 640) RGB: 158 89 73
This image has 69197 colors.

I need to classify each of the RGB colors obtained this way into 3-bit RGB (8 colors), I would need to have an idea on how to do this... How to know the range of RGB for each of the 8 colors of the 3-bit RGB. 我需要将这种获得的每种RGB颜色分类为3位RGB(8种颜色),我需要知道如何做到这一点...如何知道8种颜色中每种颜色的RGB范围3位RGB。 Thanks! 谢谢!

Your resulting image will have 1 bits for each color component. 生成的图像每个颜色分量将包含1位。

For red for instance you should do this: 例如,对于红色,你应该这样做:

 red1Bit = redOriginal > 127 ? 1 : 0;

You should do the same for blue and green as well. 蓝色和绿色也应该这样做。

A 24-bit RGB palette uses 8 bits for each of the red, green, and blue color components: 24位RGB调色板为每个红色,绿色和蓝色组件使用8位:

RGB([0..255], [0..255], [0..255]) . RGB([0..255], [0..255], [0..255])

A 3-bit RGB palette uses 1 bit for each of the red, green and blue color components: 3位RGB调色板为每个红色,绿色和蓝色组件使用1位:

RGB([0..1], [0..1], [0..1]) . RGB([0..1], [0..1], [0..1])

Since the range of each channel is only two values, you need to reduce the 8-bit encoding to a 1 bit encoding. 由于每个通道的范围只有两个值,因此需要将8位编码减少为1位编码。 To do this, divide by half of the maximum value in an eight bit encoding ( 255 / 2 = 127 ) 为此,在8位编码中除以最大值的一半( 255 / 2 = 127

You can use integer division for the conversion of each RGB compenent: 您可以使用整数除法来转换每个RGB部分:

3 bit R : (8-bit Red Value) / 127
      G : (8-bit Green Value) / 127 
      B : (8-bit Blue Value) / 127 

Example: 例:

3 bit R : (56) / 127 = 0
      G : (225) / 127 = 1
      B : (127) / 127 = 1

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

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