本文整理匯總了Python中cv2.RETR_CCOMP屬性的典型用法代碼示例。如果您正苦於以下問題:Python cv2.RETR_CCOMP屬性的具體用法?Python cv2.RETR_CCOMP怎麽用?Python cv2.RETR_CCOMP使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類cv2
的用法示例。
在下文中一共展示了cv2.RETR_CCOMP屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: vis_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例2: vis_parsing
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例3: vis_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例4: draw_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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
示例5: vis_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例6: vis_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例7: recognize_text
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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
示例8: fix_holes
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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
示例9: vis_uv_temp
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例10: vis_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例11: get_output_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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
示例12: _contours
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
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)
示例13: mask_to_polygons
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
def mask_to_polygons(mask, min_area=8.):
"""Convert a mask ndarray (binarized image) to Multipolygons"""
# first, find contours with cv2: it's much faster than shapely
image, contours, hierarchy = cv2.findContours(mask,
cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_NONE)
if not contours:
return Polygon()
# now messy stuff to associate parent and child contours
cnt_children = defaultdict(list)
child_contours = set()
assert hierarchy.shape[0] == 1
# http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html
for idx, (_, _, _, parent_idx) in enumerate(hierarchy[0]):
if parent_idx != -1:
child_contours.add(idx)
cnt_children[parent_idx].append(contours[idx])
# create actual polygons filtering by area (removes artifacts)
all_polygons = []
for idx, cnt in enumerate(contours):
if idx not in child_contours and cv2.contourArea(cnt) >= min_area:
assert cnt.shape[1] == 1
poly = Polygon(
shell=cnt[:, 0, :],
holes=[c[:, 0, :] for c in cnt_children.get(idx, [])
if cv2.contourArea(c) >= min_area])
all_polygons.append(poly)
if len(all_polygons) > 1:
print('more than one polygon!')
wkt = dumps(all_polygons[0], rounding_precision=0)
return wkt
示例14: contours_hierarchy
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
def contours_hierarchy(mask):
# first, find contours with cv2: it's much faster than shapely
image, contours, hierarchy = cv2.findContours(
((mask == 1) * 255).astype(np.uint8),
cv2.RETR_CCOMP,
cv2.CHAIN_APPROX_TC89_KCOS) # cv2.CHAIN_APPROX_SIMPLE,#orig cv2.RETR_CCOMP, cv2.CHAIN_APPROX_TC89_KCOS
return contours, hierarchy
示例15: __image_clean__
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import RETR_CCOMP [as 別名]
def __image_clean__(self,image):
"""
after removing grid lines and applying thresholding, we will probably still have small "ticks" - bits of the
grid line which weren't removed but can still cause problems for Tesseract (and probably other approaches too)
"""
_,contours, hier = cv2.findContours(image.copy(),cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
# contours are probably in sorted order but just to be sure
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
perimeter = cv2.arcLength(cnt,True)
if (h <= 7) or (w <= 7) or (perimeter <= 30):
cv2.drawContours(image,[cnt],0,255,-1)
return image