I am trying to send a mask for which the border pixel of the mask is 0 (sure background) and the rest of the pixels are at some gray level(say 120 as we do not know the sure foreground)
This is the function for finding the mask.
def maskImage(img):
width = img.shape[1]
height = img.shape[0]
image = np.ones((height, width), dtype=np.uint8) * 120 #gray pixels
# Set boundary pixels to black
image[0, :] = 0 # Top boundary
image[height - 1, :] = 0 # Bottom boundary
image[:, 0] = 0 # Left boundary
image[:, width - 1] = 0 # Right boundary
_, image = cv2.threshold(image, 128,255,cv2.THRESH_BINARY)
Now , I want to call grabcut with this mask as initializer instead of rect.
def grabCut(img):
imgcopy = img.copy()
img = cv2.cvtColor(img,cv2.COLOR_RGBA2BGR)
mask = np.zeros(img.shape[:2], np.uint8)
m = maskImage(img)
mask[m == 0] = 0
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (0,0, img.shape[1]-1, img.shape[0]-1)
mask = (0,0, mask.shape[1]-1, mask.shape[0]-1)
mask, bgdModel, fgdModel = cv2.grabCut(img,mask,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK)
mask2 = np.where((mask==2)|(mask==0),1,0).astype('uint8')
img = img*mask2[:,:,np.newaxis]
cv2.imwrite("grab.png", img)
With this I am getting this error:
cv2.error: OpenCV(3.4.2) /opt/concourse/worker/volumes/live/9523d527-1b9e-48e0-7ed0-a36adde286f0/volume/opencv-suite_1535558719691/work/modules/imgproc/src/grabcut.cpp:328: error: (-5:Bad argument) mask must have CV_8UC1 type in function ‘checkMask’
Need help fixing it.