# -*- coding: utf-8 -*-"""图像分割"""import numpy as npimport cv2from matplotlib import pyplot as pltimg = cv2.imread('../../../datas/images/building.jpg')mask = np.zeros(img.shape[:2],np.uint8)# 背景模型bgdModel = np.zeros((1,65),np.float64)# 前景模型fgdModel = np.zeros((1,65),np.float64)rect = (50,50,450,290)# 使用grabCut算法cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')img = img*mask2[:,:,np.newaxis]plt.imshow(img),plt.colorbar(),plt.show()