from scipy.misc import imresize import graphcut from scipy.cluster.vq import * import sift from numpy import * from matplotlib.pyplot import * from PIL import Image im = array(Image.open("building.png"))[:,:,:3] im = imresize(im,0.1,interp="bilinear") size = im.shape[:2] # add two rectangular training regions labels = zeros(size) labels[3:18,3:18] = -1 labels[-18:-3,-18:-3] = 1 # create graph g = graphcut.build_bayes_graph(im,labels,kappa=1) # cut the graph res = graphcut.cut_graph(g,size) figure() graphcut.show_labeling(im,labels) figure() imshow(res) gray() axis("off") show()
from scipy.misc import imresize import graphcut from PIL import Image from numpy import * from pylab import * im = array(Image.open('../pcv_data/data/empire.jpg')) im = imresize(im, 0.03, interp='bilinear') size = im.shape[:2] # add two rectangular training regions labels = zeros(size) labels[3:8, 3:8] = -1 labels[-8:-3, -8:-3] = 1 # create graph g = graphcut.build_bayes_graph(im, labels, kappa=1) # cut the graph res = graphcut.cut_graph(g, size) figure() graphcut.show_labeling(im, labels) figure() imshow(res) gray() axis('off') show()
size = im.shape[:2] plt.imshow(im) #2つの長方形の訓練データ領域を追加する labels = np.zeros(size) labels[3:18,3:18] = -1 labels[-18:-3,-18:-3] = 1 plt.imshow(labels) #グラフを作成する g = graphcut.build_bayes_graph(im,labels,kappa=1) #グラフをカットする res = graphcut.cut_graph(g,size) plt.figure() graphcut.show_labeling(im,labels) plt.figure() plt.imshow(res) plt.gray() plt.axis('off') #9.1.2 ユーザー入力を用いた領域分割 #.tarを展開する #import os #os.chdir("C:\Users\minako\Downloads\images")
# create graph g = graphcut.build_bayes_graph(im, labels, kappa=1) out = open("tocpp.txt", "w") out.write(str(len(g)) + "\n") edlist = g.edges() for a in edlist: out.write(str(a[0]) + " " + str(a[1]) + " " + str(g.edge_weight((a[0], a[1]))) + "\n") out.write(str(-1) + " " + str(-1) + " " + str(-1) + "\n") out.close() algo = r"edmond_karp.exe" # algo=r"push_relabel.exe" print algo # cut the graph res = graphcut.cut_graph(g, size, algo) # figure() # graphcut.show_labeling(im,labels) figure() imshow(res) gray() axis("off") show()
9.1. Graph Cuts 245 pixel value meaning 0, 64 background 128 unknown 255 foreground # load image and annotation map im = array(Image.open(’376043.jpg’)) m = array(Image.open(’376043.bmp’)) # resize scale = 0.1 im = imresize(im,scale,interp=’bilinear’) m = imresize(m,scale,interp=’nearest’) # create training labels labels = create_msr_labels(m,False) # build graph using annotations g = graphcut.build_bayes_graph(im,labels,kappa=2) # cut graph res = graphcut.cut_graph(g,im.shape[:2]) # remove parts in background res[m==0] = 1 res[m==64] = 1 # plot the result figure() imshow(res) gray() xticks([]) yticks([]) savefig(’labelplot.pdf’)