我试图量化一幅图像,使所有的原色保持在适当的位置,并删除所有的副色,如“抗锯齿”边框。 例如,下面的图像最终应量化为3种颜色,而原始图像中的实际颜色数量超过30种。所有“抗锯齿”边框颜色都应视为次要颜色,并在量化时消除,以及“jpeg伪影”,由于过度优化,这些伪影会为图像添加更多颜色。 注意:源图像可以是png或jpeg
对于量化本身,我使用PIL.quantize(...)
和K作为要离开的颜色数。它工作得相当好,并保持调色板与原作完美匹配
def color_quantize(path, K):
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
im_pil = Image.fromarray(np.uint8(img))
im_pil = im_pil.quantize(K, None, 0, None)
return cv2.cvtColor(np.array(im_pil.convert("RGB")), cv2.COLOR_RGB2BGR)
因此,如果我事先知道“K”(原色的数量),那么我会将其用于im_pil.quantize(...)
。基本上,我需要一种方法来得到那个“K”数字。
有没有办法确定原色的数量
顺便说一句,关于“jpeg伪影”的删除,我目前正在使用img = cv2.bilateralFilter(img, 9, 75, 75)
,这非常有效
您可以使用K-Means Clustering来确定带有^{} 的图像中的主色调。我不确定是否有办法确定原色的“K”数,因为这通常是用户定义的值。此方法提供主色调和百分比分布
输入图像
结果
对于
n_clusters=4
,以下是最主要的颜色和百分比分布每个颜色簇的可视化(白色不可见)
我使用以下函数来确定主色调的数量:
对于阈值“.0045”,它给出了一个可接受的结果。然而,它看起来仍然有点“人造”
您可能想尝试分析RGB通道的直方图,以找出它们有多少个峰值,希望您有几个大峰值和一些非常小的峰值,然后大峰值的数量应该是您的K
相关问题 更多 >
编程相关推荐