scipy.cluster.hierarchy中链接函数的用法

时间:2023-01-21 21:20:42

I am trying to apply hierarchial cluster to pixel values of an image. This is to assign different areas of the image and extract segments with similar color. The problem area is to segment image by near colors only, not by shapes. I am trying like (assume the image is loaded as numpy array of shape(256,256,3), can't share the picture due to copyright issue:

我正在尝试将层次结构集群应用于图像的像素值。这是为了分配图像的不同区域并提取具有相似颜色的片段。问题区域是仅按近颜色分割图像,而不是按形状分割。我想尝试(假设图像加载为numpy数组形状(256,256,3),由于版权问题无法共享图片:

from scipy.cluster.hierarchy import dendrogram, linkage
ppp=img.reshape(img.shape[0]*img.shape[1],img.shape[2])
Z = linkage(ppp, method = 'ward')
dendrogram(Z,leaf_rotation=90.,    leaf_font_size=8.,)

This is giving error:

这是错误的:

MemoryError                               Traceback (most recent call last)
<ipython-input-87-39453b2f2da1> in <module>()
     14     ppp=img.reshape(img.shape[0]*img.shape[1],img.shape[2])
---> 15     Z = linkage(ppp, method = 'ward')
     16     dendrogram(Z,leaf_rotation=90.,    leaf_font_size=8.,)

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\cluster\hierarchy.py in linkage(y, method, metric, optimal_ordering)
    706                          'matrix looks suspiciously like an uncondensed '
    707                          'distance matrix')
--> 708         y = distance.pdist(y, metric)
    709     else:
    710         raise ValueError("`y` must be 1 or 2 dimensional.")

~\AppData\Local\Continuum\anaconda3\lib\site-packages\scipy\spatial\distance.py in pdist(X, metric, *args, **kwargs)
   1650     out = kwargs.pop("out", None)
   1651     if out is None:
-> 1652         dm = np.empty((m * (m - 1)) // 2, dtype=np.double)
   1653     else:
   1654         if out.shape != (m * (m - 1) // 2,):

Can you please help?

你能帮忙吗?

1 个解决方案

#1


1  

ppp has shape (65536, 3), so m in that error message is 65536. Internally, linkage creates an array of floating point values with size m*(m-1)//2 to hold all the pairwise distances. In your case this is 2147450880 elements. Each floating point element requires eight bytes, so the total size of the array is 17179607040 bytes. That's over 17 gigabytes. Presumably you don't have enough memory to allocate such an array.

ppp具有形状(65536,3),因此该错误消息中的m为65536.在内部,链接创建一个浮点值数组,其大小为m *(m-1)// 2以保持所有成对距离。在您的情况下,这是2147450880元素。每个浮点元素需要8个字节,因此数组的总大小为17179607040字节。这超过17千兆字节。大概你没有足够的内存来分配这样的数组。

#1


1  

ppp has shape (65536, 3), so m in that error message is 65536. Internally, linkage creates an array of floating point values with size m*(m-1)//2 to hold all the pairwise distances. In your case this is 2147450880 elements. Each floating point element requires eight bytes, so the total size of the array is 17179607040 bytes. That's over 17 gigabytes. Presumably you don't have enough memory to allocate such an array.

ppp具有形状(65536,3),因此该错误消息中的m为65536.在内部,链接创建一个浮点值数组,其大小为m *(m-1)// 2以保持所有成对距离。在您的情况下,这是2147450880元素。每个浮点元素需要8个字节,因此数组的总大小为17179607040字节。这超过17千兆字节。大概你没有足够的内存来分配这样的数组。