【问题标题】:A bit wrong average color. (np.mean())平均颜色有点不对。 (np.mean())
【发布时间】:2020-01-29 01:29:24
【问题描述】:

我编写了一个脚本,将图像的平均颜色写入文件中。但是,它返回一个 bit 错误值。

# coding=utf-8

from __future__ import print_function
import cv2, sys, os
import numpy as np

palette = []

if len(sys.argv) < 2:
    print(u'Drag file on me.')
    print(u'(Press Enter to close)',end='')
    raw_input()
    sys.exit()

if not os.path.exists(sys.argv[1]):
    print(u'Invalid file name.')
    print(u'(Press Enter to close)',end='')
    raw_input()
    sys.exit()
for file in sys.argv[1:]:
    im = cv2.imread(file)
    if im is None:
        print(u'The specified file is corrupted or is not a picture.')
        print(u'(Press Enter to close)',end='')
        raw_input()
        sys.exit()

    colors = np.unique(im.reshape(-1, im.shape[2]), axis=0)
    color = np.flip(colors.mean(axis=0,dtype=np.float64).astype(int)).tolist()
    palette.append([color,os.path.basename(file)[:-4]])
palette = np.array(palette)
palette = palette[palette[:,0].argsort(kind='mergesort')]
out = open('palette.txt','w')
out.write(str(palette.tolist()))
out.close()

示例:(image) - 在 Photoshop 中,here,平均颜色为 [105, 99, 89],但我的脚本返回 [107,100,90]

【问题讨论】:

  • 你为什么使用np.unique?您希望它是托盘的平均值还是实际使用图像的颜色及其频率的平均值?
  • 非常感谢!我删除了np.unique(),它按预期工作!

标签: python image numpy rgb


【解决方案1】:

您可能希望删除unique 命令以重现 javascript 正在执行的操作。替换为

colors = im.reshape(-1, im.shape[2])

不同之处在于您对味觉进行平均(使用的每种颜色出现一次),而脚本对图像进行平均(对图像中出现的颜色进行平均)。

【讨论】:

    【解决方案2】:

    换行

    colors = np.unique(im.reshape(-1, im.shape[2]), axis=0)

    colors = im.reshape(-1, im.shape[2])

    对于平均颜色计算,如果多次使用一种颜色很重要,因此使用np.unique 会给出不正确的结果。

    【讨论】:

    • 也谢谢你,斯蒂芬!
    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2016-09-08
    • 2012-01-28
    • 2011-07-09
    相关资源
    最近更新 更多