#利用grabcut算法分割图像import numpy as np
import cv2
from matplotlib import pyplot as plt
import warningswarnings.filterwarnings("ignore", module ="matplotlib")imgpath = 'E:/code/image_opencv_test/lena.jpg'
img = cv2.imread(imgpath)Coords1x, Coords1y = 'NA', 'NA'
Coords2x, Coords2y = 'NA', 'NA'def OnClick(event):#获取当鼠标”按下“的时候鼠标的位置global Coords1x, Coords1yif event.button == 1:try:Coords1x = int(event.xdata)Coords1y = int(event.ydata)except:Coords1x = event.xdataCoords1y = event.ydataprint("###左上角坐标:", Coords1x, Coords1y)
def OnMouseMotion(event):#获取当鼠标”移动“的时候鼠标的位置global Coords2x, Coords2yif event.button == 3:try:Coords2x = int(event.xdata)Coords2y = int(event.ydata)except:Coords2x = event.xdataCoords2y = event.ydataprint("###右下角坐标:", Coords2x, Coords2y)def OnMouseRelease(event):if event.button == 2:fig = plt.gca()img = cv2.imread(imgpath)#创建一个与所加载图像同形状的maskmask = np.zeros(img.shape[:2], np.uint8)#算法内部使用的数组,必须创建两个np.float64类型的0数组,大小为(1,65)bgdModel = np.zeros((1, 65), np.float64)fgdModel = np.zeros((1, 65), np.float64)#计算人工前景的矩形区域(rect.x ,rect.y ,rect.width, rect.height)if (Coords2x - Coords1x) > 0 and (Coords2y - Coords1y) > 0:try:rect = (Coords1x, Coords1y, Coords2x - Coords1x, Coords2y - Coords1y)print('### 分割区域: ', rect )iterCount = 5cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, cv2.GC_INIT_WITH_RECT)mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')img = img * mask2[:, :, np.newaxis]plt.subplot(121), plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))plt.subplot(122), plt.imshow(cv2.cvtColor(cv2.imread(imgpath), cv2.COLOR_BGR2RGB))fig.figure.canvas.draw()print("May the force be with me !")except:print("### 先左键 后右键")else:print("### 左下角坐标值必须大于右上角坐标")
#预先绘制图片
fig = plt.figure()
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))#鼠标左键,选取分割区域的左上角点
fig.canvas.mpl_connect('button_press_event', OnClick)
#鼠标右键,选取分割区域的右下角点
fig.canvas.mpl_connect('button_press_event', OnMouseMotion)
#鼠标中键,在所选区域执行分割操作
fig.canvas.mpl_connect('button_press_event', OnMouseRelease)
plt.show()
最后结果
运行完成后要点figure1中右边第三个图标才能显示分割后的图片