From 763e8441d7150cedea0f5ce3f2d3be120a7bfb61 Mon Sep 17 00:00:00 2001 From: pyhqos7bg Date: Thu, 30 May 2024 14:49:18 +0800 Subject: [PATCH] ADD file via upload --- 前景分割.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 前景分割.py diff --git a/前景分割.py b/前景分割.py new file mode 100644 index 0000000..338c11f --- /dev/null +++ b/前景分割.py @@ -0,0 +1,26 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt +img=cv2.imread('second.png') +mask=np.zeros((img.shape[:2]),np.uint8) +bgdModel=np.zeros((1,65),np.float64) +fgdModel=np.zeros((1,65),np.float64) +rect=(81,189,587,1041) +#这里计算了5次 +cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT) +#关于where函数第一个参数是条件,满足条件的话赋值为0,否则是1。如果只有第一个参数的话返回满足条件元素的坐标。 +mask2=np.where((mask==2)|(mask==0),0,1).astype('uint8') +#mask2就是这样固定的 +plt.subplot(1,2,1) +plt.imshow(img) +plt.title('original image ') +plt.xticks([]) +plt.yticks([]) +plt.subplot(1,2,2) +#这里的img也是固定的。 +img=img*mask2[:,:,np.newaxis] +plt.imshow(img) +plt.title('target image') +plt.xticks([]) +plt.yticks([]) +plt.show()