def removeChildren( src, view) : contours = [] children = view.mChildren # black out mask = np.ones(src.shape, np.int8) thisContours = [] if (view.contour != None) : # draw this contour contour = RectUtil.convertToParentCorrdinate(view, view.contour) thisContours.append(contour); # Imgproc.drawContours(mask, contours, -1, new Scalar(0), Core.FILLED); cv2.polylines(mask, thisContours, True, ColorUtil.getScalar(0),-1) # Extra and update x, y of children's contours and rects rects = [] for child in children: contour = child.contour if (contour == None) : rect = RectUtil.convertToParentCorrdinate(view,child) rects.append(rect) else : contour = RectUtil.convertToParentCorrdinateContour(view, contour) contours.append(contour) cv2.polylines(mask, contours, True, ColorUtil.getScalar(255),cv2.FILLED) # Fill children's inner contours with white and outer contours with # black # Imgproc.drawContours(mask, contours, -1, new Scalar(255), Core.FILLED); # Fill inner rects with white and outer rects with black for rect in rects: cv2.rectangle(mask, rect.tl(), rect.br(), ColorUtil.getScalar(255), cv2.FILLED) # inverse: black -> white and white -> black newMask = np.zeros(src.shape,np.int8) cv2.bitwise_not(mask, newMask) # let's create a new image now # crop = new (src.rows(), src.cols(), CvType.CV_8UC3); # set background to dominate color width = 0 height = 0 if len(src.shape) == 2 : height, width = src.shape else: height, width,channels = src.shape crop = np.zeros(src.shape, src.dtype) crop[:] = ColorUtil.getScalar(ColorUtil.findDominateColor(Rect(0, 0, width, height), src)) np.copyto(crop, src, 'unsafe', newMask.astype(bool)) return crop
def removeChildrenAndCreateTransparentBackground( src, view) : contours = [] children = view.mChildren # black out mask = np.ones(src.shape,np.int8) alpha = np.zeros(src.shape,np.int8) if (view.contour != None) : # draw this contour thisContours = [] contour = RectUtil.convertToParentCorrdinate(view, view.contour) thisContours.append(contour) cv2.polylines(mask, thisContours, True, ColorUtil.getScalar(0),cv2.FILLED) # Imgproc.drawContours(mask, contours, -1, new Scalar(0), Core.FILLED); # Extra and update x, y of children's contours and rects rects = [] for child in children: contour = child.contour if (contour == None) : bound = RectUtil.convertToParentCorrdinate(view, child) (x1,y1,width,height)= cv2.boundingRect(bound) rect = Rect(x1,y1,width,height) rects.append(rect) else : contour = RectUtil.convertToParentCorrdinate(view, contour); contours.append(contour) # Fill children's inner contours with white and outer contours with # black # Imgproc.drawContours(mask, contours, -1, new Scalar(255), Core.FILLED); cv2.polylines(mask, contours, True, ColorUtil.getScalar(255),cv2.FILLED) # Fill inner rects with white and outer rects with black for rect in rects: cv2.rectangle(mask, rect.tl(), rect.br(), ColorUtil.getScalar(255), cv2.FILLED) # inverse: black -> white and white -> black newMask = np.zeros(src.shape, np.int8) cv2.bitwise_not(mask, newMask) retval, alpha = cv2.threshold(newMask,100,255,cv2.THRESH_BINARY) b,g,r = cv2.split(src) merge = cv2.merge((b,g,r,alpha)) return merge
def save(self): for key in self.drawableInfos: contour = self.drawableInfos[key].rectView.contour if (contour != None and not RectView.isContanerView(self.drawableInfos[key].rectView)): contour = RectUtil.convertToParentCorrdinate(self.drawableInfos[key].rectView, contour) overlay = ImageUtil.createTransparentBackground(self.drawableInfos[key].mat, contour) cv2.writePng(self.drawableInfos[key].path, overlay) else: cv2.imwrite(self.drawableInfos[key].path, self.drawableInfos[key].mat)