这是我的代码我读了一个掩码“newmask.png”并试图应用这个掩码来分割图像“bill.jpg”掩码和bill.jpg的大小是相等的,我得到以下错误: 在抛出'std :: bad_alloc'的实例后终止调用 what():std :: bad_alloc 中止(核心倾销)
如果有人对此有任何想法,请帮助我。提前致谢....
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('bill1.jpg')
mask2 = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (50,50,450,290)
'''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()
'''
# newmask is the mask image I manually labelled
newmask = cv2.imread('newmask.png',0)
# whereever it is marked white (sure foreground), change mask=1
# whereever it is marked black (sure background), change mask=0
mask2[newmask == 0] = 0
mask2[newmask == 255] = 1
plt.imshow(mask2,'gray'),plt.show()
mask2, bgdModel, fgdModel = cv2.grabCut(img,mask2,None,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_MASK)
mask2 = np.where((mask2==2)|(mask2==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
plt.imshow(img),plt.colorbar(),plt.show()