(12)GrabCut前景提取(12)GrabCut前景提取

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('opencv-python-foreground-extraction-tut    orial.jpg')
mask = np.zeros(img.shape[:2],np.uint8)

#指定背景和前景模型
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
#需要根据实际改变
rect=(161,79,150,150)

#grab and  cut a certain region
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()

 

相关文章:

  • 2021-08-31
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
  • 2022-12-23
  • 2021-10-19
  • 2021-06-09
猜你喜欢
  • 2021-06-15
  • 2022-12-23
  • 2021-08-28
  • 2021-08-02
  • 2021-04-11
  • 2021-05-14
相关资源
相似解决方案