本文整理汇总了Python中cv2.RETR_TREE属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.RETR_TREE属性的具体用法?Python cv2.RETR_TREE怎么用?Python cv2.RETR_TREE使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.RETR_TREE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: FindHullDefects
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def FindHullDefects(self, segment):
_,contours,hierarchy = cv2.findContours(segment, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# find largest area contour
max_area = -1
for i in range(len(contours)):
area = cv2.contourArea(contours[i])
if area>max_area:
cnt = contours[i]
max_area = area
cnt = cv2.approxPolyDP(cnt,0.01*cv2.arcLength(cnt,True),True)
hull = cv2.convexHull(cnt, returnPoints=False)
defects = cv2.convexityDefects(cnt, hull)
return [cnt,defects]
示例2: canny
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def canny(filepathname, left=70, right=140):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v, cv2.COLOR_BGR2GRAY)
s = cv2.Canny(s, left, right)
cv2.imshow('nier',s)
return s
# 圈出最小方矩形框,这里Canny算法后都是白色线条,所以取色范围 127-255 即可。
# ret, binary = cv2.threshold(s,127,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10: # 有约束的画框
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# # cv2.drawContours(s,contours,-1,(0,0,255),3) # 画所有框
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()
示例3: laplacian
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def laplacian(filepathname):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v, cv2.COLOR_BGR2GRAY)
s = cv2.Laplacian(s, cv2.CV_16S, ksize=3)
s = cv2.convertScaleAbs(s)
cv2.imshow('nier',s)
return s
# ret, binary = cv2.threshold(s,40,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10:
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()
示例4: __get_annotation__
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def __get_annotation__(self, mask, image=None):
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
segmentation = []
for contour in contours:
# Valid polygons have >= 6 coordinates (3 points)
if contour.size >= 6:
segmentation.append(contour.flatten().tolist())
RLEs = cocomask.frPyObjects(segmentation, mask.shape[0], mask.shape[1])
RLE = cocomask.merge(RLEs)
# RLE = cocomask.encode(np.asfortranarray(mask))
area = cocomask.area(RLE)
[x, y, w, h] = cv2.boundingRect(mask)
if image is not None:
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
cv2.drawContours(image, contours, -1, (0,255,0), 1)
cv2.rectangle(image,(x,y),(x+w,y+h), (255,0,0), 2)
cv2.imshow("", image)
cv2.waitKey(1)
return segmentation, [x, y, w, h], area
示例5: contours
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def contours(mask):
"""Extracts contours and the relationship between them from a binary mask.
Args:
mask: the binary mask to find contours in.
Returns:
The detected contours as a list of points and the contour hierarchy.
Note: the hierarchy can be used to re-construct polygons with holes as one entity.
"""
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
return contours, hierarchy
# Todo: should work for lines, too, but then needs other epsilon criterion than arc length
示例6: movement
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def movement(mat_1,mat_2):
mat_1_gray = cv2.cvtColor(mat_1.copy(),cv2.COLOR_BGR2GRAY)
mat_1_gray = cv2.blur(mat_1_gray,(blur1,blur1))
_,mat_1_gray = cv2.threshold(mat_1_gray,100,255,0)
mat_2_gray = cv2.cvtColor(mat_2.copy(),cv2.COLOR_BGR2GRAY)
mat_2_gray = cv2.blur(mat_2_gray,(blur1,blur1))
_,mat_2_gray = cv2.threshold(mat_2_gray,100,255,0)
mat_2_gray = cv2.bitwise_xor(mat_1_gray,mat_2_gray)
mat_2_gray = cv2.blur(mat_2_gray,(blur2,blur2))
_,mat_2_gray = cv2.threshold(mat_2_gray,70,255,0)
mat_2_gray = cv2.erode(mat_2_gray,np.ones((erodeval,erodeval)))
mat_2_gray = cv2.dilate(mat_2_gray,np.ones((4,4)))
_, contours,__ = cv2.findContours(mat_2_gray,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) > 0:return True #If there were any movements
return False #if not
#Pedestrian Recognition Thread
示例7: overlay_masks
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def overlay_masks(im, masks, alpha=0.5):
colors = np.load(os.path.join(os.path.dirname(__file__), 'pascal_map.npy'))/255.
if isinstance(masks, np.ndarray):
masks = [masks]
assert len(colors) >= len(masks), 'Not enough colors'
ov = im.copy()
im = im.astype(np.float32)
total_ma = np.zeros([im.shape[0], im.shape[1]])
i = 1
for ma in masks:
ma = ma.astype(np.bool)
fg = im * alpha+np.ones(im.shape) * (1 - alpha) * colors[i, :3] # np.array([0,0,255])/255.0
i = i + 1
ov[ma == 1] = fg[ma == 1]
total_ma += ma
# [-2:] is s trick to be compatible both with opencv 2 and 3
contours = cv2.findContours(ma.copy().astype(np.uint8), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
cv2.drawContours(ov, contours[0], -1, (0.0, 0.0, 0.0), 1)
ov[total_ma == 0] = im[total_ma == 0]
return ov
示例8: find_components
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def find_components(im, max_components=16):
"""Dilate the image until there are just a few connected components.
Returns contours for these components."""
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 10))
dilation = dilate(im, kernel, 6)
count = 21
n = 0
sigma = 0.000
while count > max_components:
n += 1
sigma += 0.005
result = cv2.findContours(dilation, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
if len(result) == 3:
_, contours, hierarchy = result
elif len(result) == 2:
contours, hierarchy = result
possible = find_likely_rectangles(contours, sigma)
count = len(possible)
return (dilation, possible, n)
示例9: overlay_mask
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def overlay_mask(self, image, predictions):
"""
Adds the instances contours for each predicted object.
Each label has a different color.
Arguments:
image (np.ndarray): an image as returned by OpenCV
predictions (BoxList): the result of the computation by the model.
It should contain the field `mask` and `labels`.
"""
masks = predictions.get_field("mask").numpy()
labels = predictions.get_field("labels")
colors = self.compute_colors_for_labels(labels).tolist()
for mask, color in zip(masks, colors):
thresh = mask[0, :, :, None]
contours, hierarchy = cv2_util.findContours(
thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE
)
image = cv2.drawContours(image, contours, -1, color, 3)
composite = image
return composite
示例10: sobel
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def sobel(filepathname):
v = cv2.imread(filepathname)
s = cv2.cvtColor(v,cv2.COLOR_BGR2GRAY)
x, y = cv2.Sobel(s,cv2.CV_16S,1,0), cv2.Sobel(s,cv2.CV_16S,0,1)
s = cv2.convertScaleAbs(cv2.subtract(x,y))
s = cv2.blur(s,(9,9))
cv2.imshow('nier',s)
return s
# ret, binary = cv2.threshold(s,40,255,cv2.THRESH_BINARY)
# contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# for c in contours:
# x,y,w,h = cv2.boundingRect(c)
# if w>5 and h>10:
# cv2.rectangle(v,(x,y),(x+w,y+h),(155,155,0),1)
# cv2.imshow('nier2',v)
# cv2.waitKey()
# cv2.destroyAllWindows()
示例11: tightboundingbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def tightboundingbox(self, image):
ret, thresh = cv2.threshold(np.array(image, dtype=np.uint8), 0, 255, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
bb = []
for c in contours:
x, y, w, h = cv2.boundingRect(c)
# +1 is done to encapsulate entire figure
w += 2
h += 2
x -= 1
y -= 1
x = np.max([0, x])
y = np.max([0, y])
bb.append([y, x, w, h])
bb = self.nms(bb)
return bb
示例12: findTargets
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def findTargets(frame, mask):
# Finds contours
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
# Take each frame
# Gets the shape of video
screenHeight, screenWidth, _ = frame.shape
# Gets center of height and width
centerX = (screenWidth / 2) - .5
centerY = (screenHeight / 2) - .5
# Copies frame and stores it in image
image = frame.copy()
# Processes the contours, takes in (contours, output_image, (centerOfImage)
if len(contours) != 0:
image = findTape(contours, image, centerX, centerY)
else:
# pushes that it deosn't see vision target to network tables
networkTable.putBoolean("tapeDetected", False)
# Shows the contours overlayed on the original video
return image
# Finds the balls from the masked image and displays them on original stream + network tables
示例13: findCargo
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [as 别名]
def findCargo(frame, mask):
# Finds contours
_, contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_KCOS)
# Take each frame
# Gets the shape of video
screenHeight, screenWidth, _ = frame.shape
# Gets center of height and width
centerX = (screenWidth / 2) - .5
centerY = (screenHeight / 2) - .5
# Copies frame and stores it in image
image = frame.copy()
# Processes the contours, takes in (contours, output_image, (centerOfImage)
if len(contours) != 0:
image = findBall(contours, image, centerX, centerY)
else:
# pushes that it doesn't see cargo to network tables
networkTable.putBoolean("cargoDetected", False)
# Shows the contours overlayed on the original video
return image
# Draws Contours and finds center and yaw of orange ball
# centerX is center x coordinate of image
# centerY is center y coordinate of image
示例14: get_single_centerpoint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [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
示例15: get_single_centerpoint
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_TREE [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