简体   繁体   English

Python 3:图像中最常见的颜色与数据类型匹配

[英]Python 3: Most common color in image kmeans data type match

I am trying to adapt some python 2.4 code to 3.5. 我试图将一些python 2.4代码改编为3.5。 I am trying to use the top answer from this thread: Python - Find dominant/most common color in an image , but it's giving me trouble. 我试图使用这个主题的最佳答案: Python - 在图像中找到显性/最常见的颜色 ,但它给了我麻烦。 This author also had trouble, but different trouble Error with hex encode in Python 3.3 这个作者也有麻烦,但不同的麻烦错误与Python 3.3中的十六进制编码

Specifically something to do with scipy and kmeans variable types? 特别是与scipy和kmeans变量类型有关? Here is the code and traceback. 这是代码和回溯。 Your wisdom is appreciated! 感谢您的智慧! -S -S

import struct
from PIL import Image
import scipy
import scipy.misc
import scipy.cluster
import numpy as np

NUM_CLUSTERS = 3

print('reading image')
im = Image.open('image.jpg')
im = im.resize((150, 150))      # optional, to reduce time
ar = scipy.misc.fromimage(im)
shape = ar.shape
ar = ar.reshape(scipy.product(shape[:2]), shape[2])

print ('finding clusters')
print(ar)
print("Variable type:", type(ar))
codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
print('cluster centres:\n', codes)

vecs, dist = scipy.cluster.vq.vq(ar, codes)         # assign codes
counts, bins = scipy.histogram(vecs, len(codes))    # count occurrences
index_max = scipy.argmax(counts)                    # find most frequent
peak = codes[index_max]

colour = ''.join(format(c, '02x') for c in peak).encode('hex_codec')
print ('most frequent is %s (#%s)' % (peak, colour))

and the traceback is: 并且追溯是:

=========== RESTART: /Users/splash/Dropbox/PY/image-dom-color.py ============
reading image
finding clusters
[[255 255 255]
 [255 255 255]
 [255 255 255]
 ..., 
 [255 255 255]
 [255 255 255]
 [255 255 255]]
Variable type: <class 'numpy.ndarray'>
Traceback (most recent call last):
  File "/Users/splash/Dropbox/PY/image-dom-color.py", line 20, in <module>
    codes, dist = scipy.cluster.vq.kmeans(ar, NUM_CLUSTERS)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/cluster/vq.py", line 568, in kmeans
    book, dist = _kmeans(obs, guess, thresh=thresh)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/scipy/cluster/vq.py", line 436, in _kmeans
    code_book, has_members = _vq.update_cluster_means(obs, obs_code, nc)
  File "scipy/cluster/_vq.pyx", line 347, in scipy.cluster._vq.update_cluster_means (scipy/cluster/_vq.c:4695)
TypeError: type other than float or double not supported
>>> 

The stacktrace tells us that the only supported data types for scipy.cluster._vq.update_cluster_means() are float and double . stacktrace告诉我们scipy.cluster._vq.update_cluster_means()唯一支持的数据类型是floatdouble Checking out scipy's source code confirms this: 检查scipy的源代码确认了这一点:

def update_cluster_means(np.ndarray obs, np.ndarray labels, int nc):
    """
    The update-step of K-means. Calculate the mean of observations in each
    cluster.
    Parameters
    ----------
    obs : ndarray
        The observation matrix. Each row is an observation. Its dtype must be
        float32 or float64.
    ...

Source: _vq.pyx on GitHub 来源: GitHub上的_vq.pyx

To solve your problem you first need to convert the input into a supported data type using numpy.ndarray.astype() : 要解决您的问题,首先需要使用numpy.ndarray.astype()将输入转换为支持的数据类型:

codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), NUM_CLUSTERS)
# Or:       = scipy.cluster.vq.kmeans(ar.astype('double'), NUM_CLUSTERS)

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

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