本文整理汇总了Python中cv2.RETR_EXTERNAL属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.RETR_EXTERNAL属性的具体用法?Python cv2.RETR_EXTERNAL怎么用?Python cv2.RETR_EXTERNAL使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.RETR_EXTERNAL属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: segment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#-----------------
# MAIN FUNCTION
#-----------------
示例2: contour_filter
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def contour_filter(self, frame):
_, contours, _ = cv2.findContours(frame,
cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
new_frame = np.zeros(frame.shape, np.uint8)
for i, contour in enumerate(contours):
c_area = cv2.contourArea(contour)
if self.contour_min_area <= c_area <= self.contour_max_area:
mask = np.zeros(frame.shape, np.uint8)
cv2.drawContours(mask, contours, i, 255, cv2.FILLED)
mask = cv2.bitwise_and(frame, mask)
new_frame = cv2.bitwise_or(new_frame, mask)
frame = new_frame
if self.contour_disp_flag:
frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
cv2.drawContours(frame, contours, -1, (255, 0, 0), 1)
return frame
# A number of methods corresponding to the various trackbars available.
示例3: prediction
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def prediction(self, image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.GaussianBlur(image, (21, 21), 0)
if self.avg is None:
self.avg = image.copy().astype(float)
cv2.accumulateWeighted(image, self.avg, 0.5)
frameDelta = cv2.absdiff(image, cv2.convertScaleAbs(self.avg))
thresh = cv2.threshold(
frameDelta, DELTA_THRESH, 255,
cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
self.avg = image.copy().astype(float)
return cnts
示例4: mask2poly_single
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [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
示例5: _findContours
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def _findContours(self):
contours = []
masks = self.masks.detach().numpy()
for mask in masks:
mask = cv2.UMat(mask)
contour, hierarchy = cv2.findContours(
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1
)
reshaped_contour = []
for entity in contour:
assert len(entity.shape) == 3
assert entity.shape[1] == 1, "Hierarchical contours are not allowed"
reshaped_contour.append(entity.reshape(-1).tolist())
contours.append(reshaped_contour)
return contours
示例6: find_corners_of_largest_polygon
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def find_corners_of_largest_polygon(img):
"""Finds the 4 extreme corners of the largest contour in the image."""
opencv_version = cv2.__version__.split('.')[0]
if opencv_version == '3':
_, contours, h = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Find contours
else:
contours, h = cv2.findContours(img.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Find contours
contours = sorted(contours, key=cv2.contourArea, reverse=True) # Sort by area, descending
polygon = contours[0] # Largest image
# Use of `operator.itemgetter` with `max` and `min` allows us to get the index of the point
# Each point is an array of 1 coordinate, hence the [0] getter, then [0] or [1] used to get x and y respectively.
# Bottom-right point has the largest (x + y) value
# Top-left has point smallest (x + y) value
# Bottom-left point has smallest (x - y) value
# Top-right point has largest (x - y) value
bottom_right, _ = max(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
top_left, _ = min(enumerate([pt[0][0] + pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
bottom_left, _ = min(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
top_right, _ = max(enumerate([pt[0][0] - pt[0][1] for pt in polygon]), key=operator.itemgetter(1))
# Return an array of all 4 points using the indices
# Each point is in its own array of one coordinate
return [polygon[top_left][0], polygon[top_right][0], polygon[bottom_right][0], polygon[bottom_left][0]]
示例7: _findContours
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def _findContours(self):
contours = []
masks = self.masks.detach().numpy()
for mask in masks:
mask = cv2.UMat(mask)
contour, hierarchy = cv2_util.findContours(
mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1
)
reshaped_contour = []
for entity in contour:
assert len(entity.shape) == 3
assert (
entity.shape[1] == 1
), "Hierarchical contours are not allowed"
reshaped_contour.append(entity.reshape(-1).tolist())
contours.append(reshaped_contour)
return contours
示例8: sobelSecSearchPart
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [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]))
示例9: sobelFrtSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [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
示例10: colorSearch
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [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
示例11: segment
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def segment(image, threshold=25):
global bg
# find the absolute difference between background and current frame
diff = cv2.absdiff(bg.astype("uint8"), image)
# threshold the diff image so that we get the foreground
thresholded = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)[1]
# get the contours in the thresholded image
(_, cnts, _) = cv2.findContours(thresholded.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# return None, if no contours detected
if len(cnts) == 0:
return
else:
# based on contour area, get the maximum contour which is the hand
segmented = max(cnts, key=cv2.contourArea)
return (thresholded, segmented)
#--------------------------------------------------------------
# To count the number of fingers in the segmented hand region
#--------------------------------------------------------------
示例12: compare_ssim_debug
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def compare_ssim_debug(image_a, image_b, color=(255, 0, 0)):
"""
Args:
image_a, image_b: opencv image or PIL.Image
color: (r, g, b) eg: (255, 0, 0) for red
Refs:
https://www.pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/
"""
ima, imb = conv2cv(image_a), conv2cv(image_b)
score, diff = compare_ssim(ima, imb, full=True)
diff = (diff * 255).astype('uint8')
_, thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cv2color = tuple(reversed(color))
im = ima.copy()
for c in cnts:
x, y, w, h = cv2.boundingRect(c)
cv2.rectangle(im, (x, y), (x+w, y+h), cv2color, 2)
# todo: show image
cv2pil(im).show()
return im
示例13: mask_to_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def mask_to_bbox(mask, image, num_class, area_threhold=0, out_path=None, out_file_name=None):
bbox_list = []
im = copy.copy(image)
mask = mask.astype(np.uint8)
for i in range(1, num_class, 1):
c_bbox_list = []
c_mask = np.zeros_like(mask)
c_mask[np.where(mask==i)] = 255
bimg , countours, hier = cv2.findContours(c_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for cnt in countours:
area = cv2.contourArea(cnt)
if area < area_threhold:
continue
epsilon = 0.005 * cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
(x, y, w, h) = cv2.boundingRect(approx)
c_bbox_list.append([x, y, x+w, y+h])
if out_path is not None:
color = COLOR_LIST[i-1]
im=cv2.rectangle(im, pt1=(x, y), pt2=(x+w, y+h),color=color, thickness=2)
bbox_list.append(c_bbox_list)
if out_path is not None:
outf = os.path.join(out_path, out_file_name)
cv2.imwrite(outf, im)
return bbox_list
示例14: _mask_post_processing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [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
示例15: find_bounding_box
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_EXTERNAL [as 别名]
def find_bounding_box(pane, bounding_box_lower_thresholds, bounding_box_upper_thresholds, sort=True):
# thresholds: turple
# dimension_resized: turple
segmented_pictures = []
rect_coordinates = []
width_lower_threshold, height_lower_threshold = bounding_box_lower_thresholds
width_upper_threshold, height_upper_threshold = bounding_box_upper_thresholds
contours, hierarchy = cv2.findContours(pane, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
if h > height_lower_threshold and w > width_lower_threshold and h <= height_upper_threshold and w <= width_upper_threshold:
rect_coordinates.append((x, y, w, h))
else:
continue
if sort:
x_coordinates = [x for (x,y,w,h) in rect_coordinates]
rect_coordinates= [e for _,e in sorted(zip(x_coordinates,rect_coordinates))]
return rect_coordinates