Python cv2.RETR_CCOMP Examples

The following are 30 code examples of cv2.RETR_CCOMP(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: vis.py    From Parsing-R-CNN with MIT License 7 votes vote down vote up
def vis_parsing(img, parsing, colormap, show_segms=True):
    """Visualizes a single binary parsing."""
    img = img.astype(np.float32)
    idx = np.nonzero(parsing)

    parsing_alpha = cfg.VIS.SHOW_PARSS.PARSING_ALPHA
    colormap = colormap_utils.dict2array(colormap)
    parsing_color = colormap[parsing.astype(np.int)]

    border_color = cfg.VIS.SHOW_PARSS.BORDER_COLOR
    border_thick = cfg.VIS.SHOW_PARSS.BORDER_THICK

    img[idx[0], idx[1], :] *= 1.0 - parsing_alpha
    # img[idx[0], idx[1], :] += alpha * parsing_color
    img += parsing_alpha * parsing_color

    if cfg.VIS.SHOW_PARSS.SHOW_BORDER and not show_segms:
        _, contours, _ = cv2.findContours(parsing.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, border_color, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #2
Source File: vis.py    From Parsing-R-CNN with MIT License 7 votes vote down vote up
def vis_mask(img, mask, bbox_color, show_parss=False):
    """Visualizes a single binary mask."""
    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    border_color = cfg.VIS.SHOW_SEGMS.BORDER_COLOR
    border_thick = cfg.VIS.SHOW_SEGMS.BORDER_THICK

    mask_color = bbox_color if cfg.VIS.SHOW_SEGMS.MASK_COLOR_FOLLOW_BOX else _WHITE
    mask_color = np.asarray(mask_color)
    mask_alpha = cfg.VIS.SHOW_SEGMS.MASK_ALPHA

    _, contours, _ = cv2.findContours(mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
    if cfg.VIS.SHOW_SEGMS.SHOW_BORDER:
        cv2.drawContours(img, contours, -1, border_color, border_thick, cv2.LINE_AA)

    if cfg.VIS.SHOW_SEGMS.SHOW_MASK and not show_parss:
        img[idx[0], idx[1], :] *= 1.0 - mask_alpha
        img[idx[0], idx[1], :] += mask_alpha * mask_color

    return img.astype(np.uint8) 
Example #3
Source File: vis.py    From nuke-ML-server with Apache License 2.0 6 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        # cv2.findContours gives (image, contours, hierarchy) back in opencv 3.x
        # but gives back (contours, hierachy) in opencv 2.x and 4.x
        contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)[-2:]
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #4
Source File: vis.py    From DetectAndTrack with Apache License 2.0 6 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick,
                         cv2.CV_AA if cv2.__version__.startswith('2') else
                         cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #5
Source File: viz.py    From Object_Detection_Tracking with Apache License 2.0 6 votes vote down vote up
def draw_mask(im, mask, alpha=0.5, color=None, show_border=True,border_thick=1):
	"""
	Overlay a mask on top of the image.

	Args:
		im: a 3-channel uint8 image in BGR
		mask: a binary 1-channel image of the same size
		color: if None, will choose automatically
	"""
	if color is None:
		color = PALETTE_RGB[np.random.choice(len(PALETTE_RGB))][::-1]


	im = np.where(np.squeeze(np.repeat((mask > 0)[:, :, None], 3, axis=2)),
				  im * (1 - alpha) + color * alpha, im)
	if show_border:
		if cv2.__version__.startswith("2"):
			contours, _ = cv2.findContours(mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
		else: # cv 3
			_,contours, _ = cv2.findContours(mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
		cv2.drawContours(im, contours, -1, (255,255,255), border_thick, lineType=cv2.CV_AA)

	im = im.astype('uint8')
	return im 
Example #6
Source File: visualize.py    From ExtremeNet with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=2):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        # How to use `cv2.findContours` in different OpenCV versions?
        # https://stackoverflow.com/questions/48291581/how-to-use-cv2-findcontours-in-different-opencv-versions/48292371#48292371
        contours = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)[-2]
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #7
Source File: idcardocr.py    From idmatch with MIT License 6 votes vote down vote up
def recognize_text(original):
    idcard = original
    # gray = cv2.cvtColor(idcard, cv2.COLOR_BGR2GRAY)

    # Morphological gradient:
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
    opening = cv2.morphologyEx(idcard, cv2.MORPH_GRADIENT, kernel)

    # Binarization
    ret, binarization = cv2.threshold(opening, 0.0, 255.0, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

    # Connected horizontally oriented regions
    kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (9, 1))
    connected = cv2.morphologyEx(binarization, cv2.MORPH_CLOSE, kernel)

    # find countours
    _, contours, hierarchy = cv2.findContours(
        connected, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE
    )
    return contours, hierarchy 
Example #8
Source File: img.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def find_contours(mask, method = None):
    if method is None:
        method = cv2.CHAIN_APPROX_SIMPLE
    mask = np.asarray(mask, dtype = np.uint8)
    mask = mask.copy()
    try:
        contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                   method = method)
    except:
        _, contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                  method = method)
    return contours 
Example #9
Source File: img.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def find_two_level_contours(mask):
    mask = mask.copy()
    contours, tree = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                  method = cv2.CHAIN_APPROX_SIMPLE)
    return contours, tree 
Example #10
Source File: vis.py    From wifiperson with MIT License 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #11
Source File: img.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def find_contours(mask, method = None):
    if method is None:
        method = cv2.CHAIN_APPROX_SIMPLE
    mask = np.asarray(mask, dtype = np.uint8)
    mask = mask.copy()
    try:
        contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                   method = method)
    except:
        _, contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                  method = method)
    return contours 
Example #12
Source File: img.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def find_contours(mask, method = None):
    if method is None:
        method = cv2.CHAIN_APPROX_SIMPLE
    mask = np.asarray(mask, dtype = np.uint8)
    mask = mask.copy()
    try:
        contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                   method = method)
    except:
        _, contours, _ = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                  method = method)
    return contours 
Example #13
Source File: utils.py    From video_analyst with MIT License 5 votes vote down vote up
def mask_colorize(mask, num_classes, color_map):
    """
    transfor one mask to a maske with color

    :param mask: mask with shape [h, w]
    :param num_classes: number of classes
    :param color_map: color map with shape [N, 3]
    """
    color_mask = Image.fromarray(mask.astype(np.uint8)).convert('P')
    color_mask.putpalette(palette)
    raw_mask = np.array(color_mask).astype(np.uint8)
    color_mask = cv2.cvtColor(raw_mask, cv2.COLOR_GRAY2BGR)
    for c_index in range(num_classes):
        instance_mask = (raw_mask == c_index)
        if int(cv2.__version__.split(".")[0]) < 4:
            _, contour, hier = cv2.findContours(instance_mask.astype(np.uint8),
                                                cv2.RETR_CCOMP,
                                                cv2.CHAIN_APPROX_NONE)
        else:
            contour, hier = cv2.findContours(instance_mask.astype(np.uint8),
                                             cv2.RETR_CCOMP,
                                             cv2.CHAIN_APPROX_NONE)
        if len(contour) > 0:
            cv2.drawContours(color_mask, contour, -1, (225, 225, 225), 5)
        color_mask[np.where(raw_mask == c_index)] = color_map[c_index]
    return color_mask 
Example #14
Source File: img.py    From HUAWEIOCR-2019 with MIT License 5 votes vote down vote up
def find_two_level_contours(mask):
    mask = mask.copy()
    contours, tree = cv2.findContours(mask, mode = cv2.RETR_CCOMP, 
                                  method = cv2.CHAIN_APPROX_SIMPLE)
    return contours, tree 
Example #15
Source File: mask_morphology.py    From NucleiDetectron with Apache License 2.0 5 votes vote down vote up
def opencv_controur_smooth(mask, kernel, k):
    if len(mask.shape) == 3:
        mask = cv.cvtColor(mask, cv.COLOR_BGR2GRAY)

    mask = cv.dilate(mask, kernel, iterations=k)

    _, C, h = cv.findContours(mask.copy(), cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE)
    seg = [[float(x) for x in contour.flatten()] for contour in C]
    seg = [cont for cont in seg if len(cont) > 4]  # filter all polygons that are boxes
    rles = mask_util.frPyObjects(seg, mask.shape[0], mask.shape[1])
    masks = mask_util.decode(rles)
    res = []
    for idx in range(masks.shape[2]):
        res.append(masks[:, :, idx].astype(np.uint8))
    return res 
Example #16
Source File: vis.py    From NucleiDetectron with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #17
Source File: ImagePreprocess.py    From GaneratedHandsForReal_TIME with MIT License 5 votes vote down vote up
def get_largest_contour(org, mask):
    _, mask = cv2.threshold(mask, 0, 255, cv2.THRESH_BINARY)

    _, contours, hierachy = cv2.findContours(mask, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    if len(contours) == 0:
        return None, None, None
    max_contour = contours[0]
    max_area = cv2.contourArea(max_contour)
    for contour in contours:
        value = cv2.contourArea(contour)
        if max_area < value:
            max_contour = contour
            max_area = value

    x, y, w, h = cv2.boundingRect(max_contour)
    h_limit = False
    w_limit = False
    if h >= 256:
        h_limit = True
    if w >= 256:
        w_limit = True

    white = np.zeros((256, 256))
    white = cv2.drawContours(white, [max_contour], 0, (255, 255, 255), -1)
    white = cv2.drawContours(white, [max_contour], 0, (255, 255, 255), 1)
    white = np.asarray(white, np.uint8)
    mask = np.bitwise_and(mask, white)
    mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)

    back_real = np.zeros((256, 256, 3))

#    org = np.bitwise_and(org, mask)
    crop = org[y:y+h,x:x+w]
    back_real = org & mask

    mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
    back_mask = mask
    return crop, back_real, back_mask 
Example #18
Source File: geometry.py    From Cytomine-python-client with Apache License 2.0 5 votes vote down vote up
def find_components(self):
        return self._find_components(cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) 
Example #19
Source File: vis.py    From CBNet with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #20
Source File: visualizer.py    From detectron2 with Apache License 2.0 5 votes vote down vote up
def mask_to_polygons(self, mask):
        # cv2.RETR_CCOMP flag retrieves all the contours and arranges them to a 2-level
        # hierarchy. External contours (boundary) of the object are placed in hierarchy-1.
        # Internal contours (holes) are placed in hierarchy-2.
        # cv2.CHAIN_APPROX_NONE flag gets vertices of polygons from contours.
        mask = np.ascontiguousarray(mask)  # some versions of cv2 does not support incontiguous arr
        res = cv2.findContours(mask.astype("uint8"), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        hierarchy = res[-1]
        if hierarchy is None:  # empty mask
            return [], False
        has_holes = (hierarchy.reshape(-1, 4)[:, 3] >= 0).sum() > 0
        res = res[-2]
        res = [x.flatten() for x in res]
        res = [x for x in res if len(x) >= 6]
        return res, has_holes 
Example #21
Source File: vis.py    From Detectron-DA-Faster-RCNN with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #22
Source File: vis.py    From Detectron with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        contours = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)[-2]
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #23
Source File: vis.py    From Detectron-Cascade-RCNN with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #24
Source File: visualizer.py    From detectron2 with Apache License 2.0 5 votes vote down vote up
def mask_to_polygons(self, mask):
        # cv2.RETR_CCOMP flag retrieves all the contours and arranges them to a 2-level
        # hierarchy. External contours (boundary) of the object are placed in hierarchy-1.
        # Internal contours (holes) are placed in hierarchy-2.
        # cv2.CHAIN_APPROX_NONE flag gets vertices of polygons from contours.
        mask = np.ascontiguousarray(mask)  # some versions of cv2 does not support incontiguous arr
        res = cv2.findContours(mask.astype("uint8"), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        hierarchy = res[-1]
        if hierarchy is None:  # empty mask
            return [], False
        has_holes = (hierarchy.reshape(-1, 4)[:, 3] >= 0).sum() > 0
        res = res[-2]
        res = [x.flatten() for x in res]
        res = [x for x in res if len(x) >= 6]
        return res, has_holes 
Example #25
Source File: vis.py    From Parsing-R-CNN with MIT License 5 votes vote down vote up
def vis_uv_temp(img, uv, bbox, show_segms=True):
    """Visualizes a single binary parsing."""
    padded_uv = np.zeros((img.shape), dtype=np.float32)
    uv_temp = np.array([uv[0], uv[1] * 256, uv[2] * 256]).transpose(1, 2, 0)
    y2 = int(bbox[1]) + int(bbox[3] - bbox[1])
    x2 = int(bbox[0]) + int(bbox[2] - bbox[0])
    padded_uv[int(bbox[1]):y2, int(bbox[0]):x2] = uv_temp

    img = img.astype(np.float32)
    idx = np.nonzero(padded_uv[:, :, 0])

    uv_alpha = cfg.VIS.SHOW_UV.UV_ALPHA

    border_color = cfg.VIS.SHOW_UV.BORDER_COLOR
    border_thick = cfg.VIS.SHOW_UV.BORDER_THICK

    img[idx[0], idx[1], :] *= 1.0 - uv_alpha
    img += uv_alpha * padded_uv

    if cfg.VIS.SHOW_UV.SHOW_BORDER and not show_segms:
        _, contours, _ = cv2.findContours(
            padded_uv[:, :, 0].astype(np.uint8).copy(),
            cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE
        )
        cv2.drawContours(img, contours, -1, border_color, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #26
Source File: postprocess.py    From argus-tgs-salt with MIT License 5 votes vote down vote up
def fix_holes(mask):
    _, thresh = cv2.threshold(mask, 127, 255, 0)
    _, contour,hier = cv2.findContours(thresh, cv2.RETR_CCOMP,
                                       cv2.CHAIN_APPROX_SIMPLE)

    for cnt in contour:
        cv2.drawContours(mask, [cnt], 0, 255, -1)
    return mask 
Example #27
Source File: vis.py    From KL-Loss with Apache License 2.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8) 
Example #28
Source File: process_image.py    From RealTime-DigitRecognition with GNU General Public License v3.0 5 votes vote down vote up
def get_output_image(path):
  
    img = cv2.imread(path,2)
    img_org =  cv2.imread(path)

    ret,thresh = cv2.threshold(img,127,255,0)
    im2,contours,hierarchy = cv2.findContours(thresh, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

    for j,cnt in enumerate(contours):
        epsilon = 0.01*cv2.arcLength(cnt,True)
        approx = cv2.approxPolyDP(cnt,epsilon,True)
        
        hull = cv2.convexHull(cnt)
        k = cv2.isContourConvex(cnt)
        x,y,w,h = cv2.boundingRect(cnt)
        
        if(hierarchy[0][j][3]!=-1 and w>10 and h>10):
            #putting boundary on each digit
            cv2.rectangle(img_org,(x,y),(x+w,y+h),(0,255,0),2)
            
            #cropping each image and process
            roi = img[y:y+h, x:x+w]
            roi = cv2.bitwise_not(roi)
            roi = image_refiner(roi)
            th,fnl = cv2.threshold(roi,127,255,cv2.THRESH_BINARY)

            # getting prediction of cropped image
            pred = predict_digit(roi)
            print(pred)
            
            # placing label on each digit
            (x,y),radius = cv2.minEnclosingCircle(cnt)
            img_org = put_label(img_org,pred,x,y)

    return img_org 
Example #29
Source File: page_elements2.py    From namsel with MIT License 5 votes vote down vote up
def _contours(self):

#        return cv.findContours(self.img_arr.copy(), mode=cv.RETR_CCOMP, method=cv.CHAIN_APPROX_SIMPLE)
#         return cv.findContours(self.img_arr.copy(), mode=self._contour_mode , method=cv.CHAIN_APPROX_SIMPLE)
        return cv.findContours(self.img_arr.copy(), mode=self._contour_mode, 
                               method=cv.CHAIN_APPROX_SIMPLE) 
Example #30
Source File: vis.py    From densepose-video with GNU General Public License v3.0 5 votes vote down vote up
def vis_mask(img, mask, col, alpha=0.4, show_border=True, border_thick=1):
    """Visualizes a single binary mask."""

    img = img.astype(np.float32)
    idx = np.nonzero(mask)

    img[idx[0], idx[1], :] *= 1.0 - alpha
    img[idx[0], idx[1], :] += alpha * col

    if show_border:
        _, contours, _ = cv2.findContours(
            mask.copy(), cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)
        cv2.drawContours(img, contours, -1, _WHITE, border_thick, cv2.LINE_AA)

    return img.astype(np.uint8)