将浮点格式的量化颜色转换为RGB整数
我正在通过量化来减少图像中的颜色,但我需要把结果转换成RGB格式,而不是使用浮点数(比如说,array(255, 255, 255))。我找到了类似的问题,但没有一个简单直接的解决办法。
返回的clustered
会生成浮点数的数组。怎么把浮点数转换成RGB格式呢?
# Pixel Matrix
pixel = reshape(img,(img.shape[0]*img.shape[1],3))
# Clustering
centroids,_ = kmeans(pixel,8) # six colors will be found
# Quantization
qnt,_ = vq(pixel,centroids)
# Shape Quantization Result
centers_idx = reshape(qnt,(img.shape[0],img.shape[1]))
clustered = centroids[centers_idx]
1 个回答
1
如果你想把一个浮点数数组转换成字节数组(也就是8位无符号整数,范围从0到255),你有几种选择。我比较喜欢用这种方法来进行更一般的转换:
bytearray = (floatarray*255).astype('uint8')
这个方法适用于任何正浮点数数组,前提是每个通道的像素值都在0.0到1.0之间。如果你的值比较随意,可以先用floatarray /= floatarray.max()
来归一化这些值。
希望这对你有帮助!