Skip to content

Commit 0cd2d90

Browse files
committed
Manual foreground extraction
1 parent fe63d88 commit 0cd2d90

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
59.9 KB
Loading

src13.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import cv2
3+
from matplotlib import pyplot as plt
4+
5+
img = cv2.imread('opencv-python-foreground-extraction-tutorial.jpg')
6+
mask = np.zeros(img.shape[:2],np.uint8)
7+
8+
bgdModel = np.zeros((1,65),np.float64)
9+
fgdModel = np.zeros((1,65),np.float64)
10+
11+
rect = (161,79,150,150)
12+
13+
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
14+
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
15+
img = img*mask2[:,:,np.newaxis]
16+
17+
plt.imshow(img)
18+
plt.colorbar()
19+
plt.show()

0 commit comments

Comments
 (0)