本文整理汇总了Python中cv2.CHAIN_APPROX_NONE属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.CHAIN_APPROX_NONE属性的具体用法?Python cv2.CHAIN_APPROX_NONE怎么用?Python cv2.CHAIN_APPROX_NONE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.CHAIN_APPROX_NONE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mask2poly_single
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def mask2poly_single(binary_mask):
"""
:param binary_mask:
:return:
"""
try:
contours, hierarchy = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# contour_lens = np.array(list(map(len, contours)))
# max_id = contour_lens.argmax()
# max_contour = contours[max_id]
max_contour = max(contours, key=len)
rect = cv2.minAreaRect(max_contour)
poly = cv2.boxPoints(rect)
# poly = TuplePoly2Poly(poly)
except:
import pdb
pdb.set_trace()
return poly
示例2: vis_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [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)
示例3: vis_parsing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [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)
示例4: diff_rect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def diff_rect(img1, img2, pos=None):
"""find counters include pos in differences between img1 & img2 (cv2 images)"""
diff = cv2.absdiff(img1, img2)
diff = cv2.GaussianBlur(diff, (3, 3), 0)
edges = cv2.Canny(diff, 100, 200)
_, thresh = cv2.threshold(edges, 0, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
if not contours:
return None
contours.sort(key=lambda c: len(c))
# no pos provide, just return the largest different area rect
if pos is None:
cnt = contours[-1]
x0, y0, w, h = cv2.boundingRect(cnt)
x1, y1 = x0+w, y0+h
return (x0, y0, x1, y1)
# else the rect should contain the pos
x, y = pos
for i in range(len(contours)):
cnt = contours[-1-i]
x0, y0, w, h = cv2.boundingRect(cnt)
x1, y1 = x0+w, y0+h
if x0 <= x <= x1 and y0 <= y <= y1:
return (x0, y0, x1, y1)
示例5: sobelSecSearchPart
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def sobelSecSearchPart(self, bound, refpoint, out):
bound_threshold = self.sobelOperT(bound, 3, 6, 2)
tempBoundThread = bound_threshold.copy()
clearLiuDingOnly(tempBoundThread)
posLeft, posRight, flag = bFindLeftRightBound(tempBoundThread)
if flag:
if posRight != 0 and posLeft != 0 and posLeft < posRight:
posY = int(bound_threshold.shape[0] * 0.5)
for i in range(posLeft + int(bound_threshold.shape[0] * 0.1), posRight - 4):
bound_threshold[posY, i] = 255
for i in range(bound_threshold.shape[0]):
bound_threshold[i, posLeft] = 0
bound_threshold[i, posRight] = 0
_, contours, _ = cv2.findContours(bound_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for it in contours:
mr = cv2.minAreaRect(it)
if self.verifySizes(mr):
tmp = (mr[0][0] + refpoint[0], mr[0][1] + refpoint[1])
out.append((tmp, mr[1], mr[2]))
示例6: sobelFrtSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def sobelFrtSearch(self, src):
out_rects = []
src_threshold = self.sobelOper(src, self.m_GaussianBlurSize, self.m_MorphSizeWidth, self.m_MorphSizeHeight)
_, contours, _ = cv2.findContours(src_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for it in contours:
mr = cv2.minAreaRect(it)
if self.verifySizes(mr):
safeBoundRect, flag = self.calcSafeRect(mr, src)
if not flag:
continue
out_rects.append(safeBoundRect)
return out_rects
示例7: colorSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def colorSearch(self, src, color, out_rect):
"""
:param src:
:param color:
:param out_rect: minAreaRect
:return: binary
"""
color_morph_width = 10
color_morph_height = 2
match_gray = colorMatch(src, color, False)
_, src_threshold = cv2.threshold(match_gray, 0, 255, cv2.THRESH_OTSU + cv2.THRESH_BINARY)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (color_morph_width, color_morph_height))
src_threshold = cv2.morphologyEx(src_threshold, cv2.MORPH_CLOSE, element)
out = src_threshold.copy()
_, contours, _ = cv2.findContours(src_threshold, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for cnt in contours:
mr = cv2.minAreaRect(cnt)
if self.verifySizes(mr):
out_rect.append(mr)
return out
示例8: get_single_centerpoint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def get_single_centerpoint(self, mask):
contour, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contour.sort(key=lambda x: cv2.contourArea(x), reverse=True) #only save the biggest one
'''debug IndexError: list index out of range'''
count = contour[0][:, 0, :]
try:
center = self.get_centerpoint(count)
except:
x,y = count.mean(axis=0)
center=[int(x), int(y)]
# max_points = 360
# if len(contour[0]) > max_points:
# compress_rate = len(contour[0]) // max_points
# contour[0] = contour[0][::compress_rate, ...]
return center, contour
示例9: get_single_centerpoint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def get_single_centerpoint(self, mask):
contour, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
contour.sort(key=lambda x: cv2.contourArea(x), reverse=True) # only save the biggest one
'''debug IndexError: list index out of range'''
count = contour[0][:, 0, :]
try:
center = self.get_centerpoint(count)
except:
x,y = count.mean(axis=0)
center=[int(x), int(y)]
#decrease the number of contour, to speed up
# 360 points should ok, the performance drop very tiny.
max_points = 360
if len(contour[0]) > max_points:
compress_rate = len(contour[0]) // max_points
contour[0] = contour[0][::compress_rate, ...]
return center, contour
示例10: vis_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [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)
示例11: _mask_post_processing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def _mask_post_processing(mask, center_pos, size, track_mask_threshold):
target_mask = (mask > track_mask_threshold)
target_mask = target_mask.astype(np.uint8)
if cv2.__version__[-5] == '4':
contours, _ = cv2.findContours(target_mask,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
else:
_, contours, _ = cv2.findContours(
target_mask,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cnt_area = [cv2.contourArea(cnt) for cnt in contours]
if len(contours) != 0 and np.max(cnt_area) > 100:
contour = contours[np.argmax(cnt_area)]
polygon = contour.reshape(-1, 2)
prbox = cv2.boxPoints(cv2.minAreaRect(polygon))
rbox_in_img = prbox
else: # empty mask
location = cxy_wh_2_rect(center_pos, size)
rbox_in_img = np.array([[location[0], location[1]],
[location[0] + location[2], location[1]],
[location[0] + location[2], location[1] + location[3]],
[location[0], location[1] + location[3]]])
return rbox_in_img
示例12: draw_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [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
示例13: find_tissue_cnts
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def find_tissue_cnts(bw_img):
""" Fint contours of tissues
Parameters
----------
bw_img : np.array
2D binary image.
Returns
-------
cnts: list
List of all contours coordinates of tissues.
"""
cnts, _ = cv2.findContours(img_as_ubyte(bw_img), mode=cv2.RETR_EXTERNAL,
method=cv2.CHAIN_APPROX_NONE)
return cnts
示例14: find_marker_ellipses
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def find_marker_ellipses(im):
im_gray = cvtColor(im, COLOR_BGR2GRAY)
im_blur = GaussianBlur(im_gray, (3, 3), 0)
ret, th = threshold(im_blur, 0, 255, THRESH_BINARY_INV + THRESH_OTSU)
imgEdge, contours, hierarchy = findContours(th, RETR_TREE, CHAIN_APPROX_NONE)
points = []
origins = []
ellipses = []
id_point_candidates = []
small_point_candidates = []
for cnt in contours:
if contour_sanity_check(cnt, im.shape[0], point_d=0.02):
id_point_candidates.append(cnt)
elif contour_sanity_check(cnt, im.shape[0], point_d=0.01):
small_point_candidates.append(cnt)
for cnt in id_point_candidates:
x, y, w, h = boundingRect(cnt)
ellipse = fitEllipse(cnt)
points.append(im_gray[y:y + h, x:x + w])
origins.append((x, y))
ellipses.append(ellipse)
return points, origins, ellipses
示例15: _mask_post_processing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import CHAIN_APPROX_NONE [as 别名]
def _mask_post_processing(self, mask):
target_mask = (mask > cfg.TRACK.MASK_THERSHOLD)
target_mask = target_mask.astype(np.uint8)
if cv2.__version__[-5] == '4':
contours, _ = cv2.findContours(target_mask,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
else:
_, contours, _ = cv2.findContours(target_mask,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_NONE)
cnt_area = [cv2.contourArea(cnt) for cnt in contours]
if len(contours) != 0 and np.max(cnt_area) > 100:
contour = contours[np.argmax(cnt_area)]
polygon = contour.reshape(-1, 2)
prbox = cv2.boxPoints(cv2.minAreaRect(polygon))
rbox_in_img = prbox
else: # empty mask
location = cxy_wh_2_rect(self.center_pos, self.size)
rbox_in_img = np.array([[location[0], location[1]],
[location[0] + location[2], location[1]],
[location[0] + location[2], location[1] + location[3]],
[location[0], location[1] + location[3]]])
return rbox_in_img