我在Windows上使用Python中的OpenCV 3 alpha。我有一个背景减法方法,可以使用grabcut导入图像分割。所以我有MOG探测器,它提供了一些关于可能的前景和背景的信息。因此,例如这里是当前图像(直接给出可视化)。在OpenCV中同时使用rect和遮罩python
这里是从MOG探测器的输出。
我要养活这个信息到cv2.grabcut。我的希望是我不需要分割整个图像,并且它将更快(?)指定已知对象周围的区域并传递可能的前景和背景。该斑点被存储为匀称的多边形,其边界是XMIN,YMIN,XMAX,YMAX
#expand the bounding box of the polygons about 5 times
b=blob.buffer(50).bounds
#change to integer
rect=[int(x) for x in b]
#Shapely give coordinates in xmin,ymin,xmax,ymax
#Format into x,y,w,h required by grabcut in opencv
rectf=tuple([rect[0],rect[1],rect[2]-rect[0],rect[3]-rect[1]])
#create a mask
mask = np.zeros(grabCUTimage.shape[:2],np.uint8)
#Make anywhere black in the grey_image (output from MOG) as likely background
#Make anywhere white in the grey_image (output from MOG) as definite foreground
mask[grey_image == 0] = 2
mask[grey_image == 255] = 1
#Make containers
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
#Run grabcut
cv2.grabCut(grabCUTimage,mask,rectf,bgdModel,fgdModel,4,cv2.GC_INIT_WITH_RECT)
#Multiple new mask by original image to get cut
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
cGB =grabCUTimage*mask2[:,:,np.newaxis]
这总是给我一个黑色的图像。所有背景。
当我初始化与cv2.GC_INIT_WITH_MASK它正常工作(请忽略红色正方形)。然而,它肯定忽略了矩形,因为它有时包括在矩形边界之外的估计前景(在这种情况下未示出)。
我是存储矩形错了吗?它不是x,y,w,h吗?将指定rect实际上使其更快,或者我应该裁剪图像?
鉴于您对边界框有一个粗略的想法,为什么不把整个图像裁剪到更小的区域并在那里使用grabCut?这肯定会加快算法的速度,它甚至可能是有利的,因为它不会被远离您想要隔离的对象的背景分散注意力。 – 2014-10-30 22:03:53