import ncut, numpy as np from scipy.misc import imresize from PIL import Image im = np.array(Image.open('C-uniform03.ppm')) m,n = im.shape[:2] wid = 50 rim = imresize(im,(wid,wid),interp='bilinear') rim = np.array(rim,'f') A = ncut.ncut_graph_matrix(rim,sigma_d=1,sigma_g=1e-2) code,V = ncut.cluster(A,k=3,ndim=3) codeim = imresize(code.reshape(wid,wid),(m,n),interp='nearest') figure() imshow(codeim) gray() show()
# from PCV.tools import ncut # from scipy.misc import imresize import ncut from pylab import * from PIL import Image im = array(Image.open('../data/C-uniform03.ppm')) m, n = im.shape[:2] print(n,m) # resize image to (wid,wid) wid = 50 # rim = imresize(im, (wid, wid), interp='bilinear') rim = np.array(Image.fromarray(im).resize((wid,wid),Image.BILINEAR)) rim = array(rim, 'f') # create normalized cut matrix A = ncut.ncut_graph_matrix(rim, sigma_d=1, sigma_g=1e-2) # cluster code, V = ncut.cluster(A, k=3, ndim=3) print(array(V).shape) print("ncut finish") # 变换到原来的图像大小 # codeim = imresize(code.reshape(wid,wid),(m,n),interp='nearest') codeim = array(Image.fromarray(code.reshape(wid,wid)).resize((n,m),Image.NEAREST)) # imshow(imresize(V[i].reshape(wid,wid),(m,n),interp=’bilinear’)) # v = zeros((m,n,4),int) v = zeros((4,m,n),int) for i in range(4): v[i] = array(Image.fromarray(V[i].reshape(wid,wid)).resize((n,m),Image.BILINEAR)) # 绘制分割结果