OpenCV3 Python语言实现 笔记3

xiaoxiao2021-02-28  135

分割    GrabCut算法   分水岭算法

一、GrabCut

import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('../images/statue_small.jpg') mask = np.zeros(img.shape[:2],np.uint8) bgdModel = np.zeros((1,65),np.float64)#前景模型 fgdModel = np.zeros((1,65),np.float64)#背景模型 rect = (100,1,421,378) 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.subplot(121), plt.imshow(img) plt.title("grabcut"), plt.xticks([]), plt.yticks([]) plt.subplot(122), plt.imshow(cv2.cvtColor(cv2.imread('../images/statue_small.jpg'), cv2.COLOR_BGR2RGB)) plt.title("original"), plt.xticks([]), plt.yticks([]) plt.show() 二、分水岭算法

。。。

转载请注明原文地址: https://www.6miu.com/read-35061.html

最新回复(0)