就是用PLT,
上一篇中主要写了kmeans图像分割的算法主体
这里对他做可视化
可视化两个函数,
第一个是visualize,遍历一遍不同的K值的图像,每个暂停0.01s,嘿嘿嘿可以做动画
第二个是交互式的inter_vis 写了两个按钮,+K和-K。
然后为了方便命令行使用又加了parser解析
啊,就是这样
嗯就是这样,UI果然好麻烦。以后还是少写,感觉代码风格都变丑了
from scipy.misc import imread,imshow,imsave
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button
from functools import partial
from optparse import OptionParser
def kmeans(img,K,epsilon):
img = img.astype(np.float64)
randpos = partial(np.random.randint,0,min(img.shape[0],img.shape[1]))
cx,cy = [randpos(K) for i in range(2)]
center = img[cx,cy]
img = img.reshape(1, img.shape[0], img.shape[1], -1)
center = center.reshape(K, 1