简体   繁体   English

UnboundLocalError在scipy中使用Kmeans

[英]UnboundLocalError using Kmeans in scipy

I'm trying to learn more about image processing in Python and, as part of the process, am doing some of the exercises in a book that I am reading. 我试图学习有关Python中图像处理的更多信息,作为该过程的一部分,我正在阅读的书中进行一些练习。 In one exercise I'm trying to do kmeans clustering of average pixel color in an image. 在一个练习中,我试图对图像中平均像素颜色进行kmeans聚类。 The code below is pretty much verbatim from the example, but I keep getting an error (stack as follows). 下面的代码几乎完全来自该示例,但是我不断收到错误消息(如下所示)。

File "C:/Users/xxx/gitStuff/version-control/image/data/practiceCh6.py", line 31, in centroids,variance = kmeans(features,3) 文件“ C:/Users/xxx/gitStuff/version-control/image/data/practiceCh6.py”,第31行,质心,方差= kmeans(功能,3)

File "C:\\Users\\xxx\\AppData\\Local\\Continuum\\Anaconda\\Lib\\site-packages\\scipy\\cluster\\vq.py", line 524, in kmeans result = best_book, best_dist 文件“ C:\\ Users \\ xxx \\ AppData \\ Local \\ Continuum \\ Anaconda \\ Lib \\ site-packages \\ scipy \\ cluster \\ vq.py”,第524行,以kmeans表示= best_book,best_dist

UnboundLocalError: local variable 'best_book' referenced before assignment UnboundLocalError:分配前已引用局部变量“ best_book”

Code is below: 代码如下:

from PIL import Image
from scipy.cluster.vq import kmeans,vq
from scipy.misc import imresize
from numpy import *


steps = 50
im = array(Image.open('frontside.jpg'))

dx = im.shape[0]
dy = im.shape[1]

#compute color features for each region
features =[]
for x in range(steps):
    for y in range(steps):
        R = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,0])
        G = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,1])
        B = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,2])
        features.append([R,G,B])

features = array(features,'f') #make into array

#cluster
centroids,variance = kmeans(features,3)
code,distance = vq(features,centroids)

#create image with clulster labels
codeim = code.reshape(steps,steps)
codeim = imresize(codeim,im.shape[:2],interp='nearest')

figure()
imshow(codeim)
show()

Any advice on what might be wrong would be much appreciated. 任何关于什么是错的建议将不胜感激。

I'm not familiar with image processing, but by adding a simple print statement to your code, you will see that values in your array are 'nan'. 我对图像处理不熟悉,但是通过在代码中添加简单的打印语句,您将看到数组中的值是“ nan”。

for x in range(steps):
    for y in range(steps):
        R = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,0])
        G = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,1])
        B = mean(im[x*dx:(x+1)*dx,y*dy:(y+1)*dy,2])
        features.append([R,G,B])

features = array(features,'f') #make into array
print features

Returns: 返回值:

[[ 186.93768311  159.18690491  157.92678833]
 [          nan           nan           nan]
 [          nan           nan           nan]
 ...,
 [          nan           nan           nan]
 [          nan           nan           nan]
 [          nan           nan           nan]]

You can't run K-means on nan's, I'd go back and check how you're assigning R,G,B. 您不能在nan上运行K-means,我会回去检查您如何分配R,G,B。

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

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