getPixel()和getRGB()的问题

博客指出用DirectGraphics.getPixels获取Nokia手机颜色值不准确,不同位置同种颜色点的获取值可能不同,MIDP2.0的Image.getRGB也有类似问题。Nokia文档未说明相关情况,还提到可通过DirectGraphics.getNativePixelFormat获取系统像素格式,如6600返回565,USHORT_4444_ARGB也可用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用DirectGraphics.getPixels(int[] ..., TYPE_INT_xxxx_xxxx) 得到的颜色值不准确。因为nokia的手机内部使用的是TYPE_USHORT_565_RGB的格式,在getPixels(int[])时需要进行映射。更严重的是,不同位置的同种颜色的点,get出来的颜色值也可能不一样(遇到过了这种情况,但还没发现规律。如果你知道,请告诉我:)。比如白色(0xffffffff),getPixel(int[])得到的结果是0xf8fcf8。

其实MIDP2.0的Image.getRGB()也有一样的问题,例如用wtk2.2, 所有的颜色都会被映射到"4096色",即0xabcdef,其中a=b,c=d,e=f。并且MIDP2.0中有相应的说明:

The returned values are not guaranteed to be identical to values from the original source, such as from createRGBImage or from a PNG image. Color values may be resampled to reflect the display capabilities of the device (for example, red, green or blue pixels may all be represented by the same gray value on a grayscale device). On devices that do not support alpha blending, the alpha value will be 0xFF for opaque pixels and 0x00 for all other pixels (see Alpha Processing for further discussion.) On devices that support alpha blending, alpha channel values may be resampled to reflect the number of levels of semitransparency supported.
Nokia的文档里没有说明.

nokia ui中,可以通过DirectGraphics.getNativePixelFormat()来得到系统的像素格式,6600返回的是565(DirectGraphics.TYPE_USHORT_565_RGB),实际测试中,USHORT_4444_ARGB也是可用的。

class B: def __init__(self): self.img_arr_r = [] self.img_arr_g = [] self.img_arr_b = [] self.coef = 255.0 def getRGB(self, path): img = Image.open(path) img = img.convert('RGB') width, height = img.size for i in range(width): for j in range(height): r, g, b = img.getpixel((i, j)) self.img_arr_r.append(r / self.coef) self.img_arr_g.append(g / self.coef) self.img_arr_b.append(b / self.coef) return self.img_arr_r, self.img_arr_g, self.img_arr_b class A: def __init__(self): self.coef_r = 0.299 self.coef_g = 0.587 self.coef_b = 0.114 def checkList(self, r_list, g_list, b_list): if (len(r_list) == len(g_list) == len(b_list)): print("Yes") else: print("No") def calBrightness(self, r_list, g_list, b_list): length = len(r_list) brightness_list = [0.0] * length coef_r = self.coef_r coef_g = self.coef_g coef_b = self.coef_b for i in range(length): brightness_list[i] = r_list[i] * coef_r + g_list[i] * coef_g + b_list[i] * coef_b return brightness_list 在Class A中,实现分析计算亮度的方法。按照亮度 Y=0.299R+0.587G+0.114B,计算每个像素的亮度,保存到一个数组中,并返回该数组。其中R, G, B为归一化之后的颜色值。 在Class B中,通过相应软件包读入一个1080P的jpg图像(1920×1080 像素)。然后调用Class A的对象,计算每个像素的亮度。这个计算亮度的过程反复1000或者10000次,记录下计算过程所需要的时间。 在不允许使用numpy等科学计算库的前提下,能否进一步优化运行效率?
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值