本文整理汇总了Python中cv2.boxPoints方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.boxPoints方法的具体用法?Python cv2.boxPoints怎么用?Python cv2.boxPoints使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.boxPoints方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mask2poly_single
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [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: getEllipseRotation
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def getEllipseRotation(image, cnt):
try:
# Gets rotated bounding ellipse of contour
ellipse = cv2.fitEllipse(cnt)
centerE = ellipse[0]
# Gets rotation of ellipse; same as rotation of contour
rotation = ellipse[2]
# Gets width and height of rotated ellipse
widthE = ellipse[1][0]
heightE = ellipse[1][1]
# Maps rotation to (-90 to 90). Makes it easier to tell direction of slant
rotation = translateRotation(rotation, widthE, heightE)
cv2.ellipse(image, ellipse, (23, 184, 80), 3)
return rotation
except:
# Gets rotated bounding rectangle of contour
rect = cv2.minAreaRect(cnt)
# Creates box around that rectangle
box = cv2.boxPoints(rect)
# Not exactly sure
box = np.int0(box)
# Gets center of rotated rectangle
center = rect[0]
# Gets rotation of rectangle; same as rotation of contour
rotation = rect[2]
# Gets width and height of rotated rectangle
width = rect[1][0]
height = rect[1][1]
# Maps rotation to (-90 to 90). Makes it easier to tell direction of slant
rotation = translateRotation(rotation, width, height)
return rotation
#################### FRC VISION PI Image Specific #############
示例3: forward_convert
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def forward_convert(coordinate, with_label=True):
"""
:param coordinate: format [x_c, y_c, w, h, theta]
:return: format [x1, y1, x2, y2, x3, y3, x4, y4]
"""
boxes = []
if with_label:
for rect in coordinate:
box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
box = np.reshape(box, [-1, ])
boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7], rect[5]])
else:
for rect in coordinate:
box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
box = np.reshape(box, [-1, ])
boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7]])
return np.array(boxes, dtype=np.float32)
示例4: forward_convert
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def forward_convert(coordinate, with_label=True):
"""
:param coordinate: format [x_c, y_c, w, h, theta]
:return: format [x1, y1, x2, y2, x3, y3, x4, y4]
"""
boxes = []
if with_label:
for rect in coordinate:
box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
box = np.reshape(box, [-1, ])
boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7], rect[5]])
else:
for rect in coordinate:
box = cv2.boxPoints(((rect[0], rect[1]), (rect[2], rect[3]), rect[4]))
box = np.reshape(box, [-1, ])
boxes.append([box[0], box[1], box[2], box[3], box[4], box[5], box[6], box[7]])
return np.array(boxes, dtype=np.float32)
示例5: calcSafeRect
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def calcSafeRect(self, roi, src):
'''
return [x, y, w, h]
'''
box = cv2.boxPoints(roi)
x, y, w, h = cv2.boundingRect(box)
src_h, src_w, _ = src.shape
tl_x = x if x > 0 else 0
tl_y = y if y > 0 else 0
br_x = x + w - 1 if x + w - 1 < src_w else src_w - 1
br_y = y + h - 1 if y + h - 1 < src_h else src_h - 1
roi_w = br_x - tl_x
roi_h = br_y - tl_y
if roi_w <= 0 or roi_h <= 0:
return [tl_x, tl_y, roi_w, roi_h], False
return [tl_x, tl_y, roi_w, roi_h], True
示例6: _mask_post_processing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [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
示例7: combine_line
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def combine_line(line):
"""Combine a set of boxes in a line into a single bounding
box.
Args:
line: A list of (box, character) entries
Returns:
A (box, text) tuple
"""
text = ''.join([character if character is not None else '' for _, character in line])
box = np.concatenate([coords[:2] for coords, _ in line] +
[np.array([coords[3], coords[2]])
for coords, _ in reversed(line)]).astype('float32')
first_point = box[0]
rectangle = cv2.minAreaRect(box)
box = cv2.boxPoints(rectangle)
# Put the points in clockwise order
box = np.array(np.roll(box, -np.linalg.norm(box - first_point, axis=1).argmin(), 0))
return box, text
示例8: _mask_post_processing
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [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
示例9: get_mini_boxes
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def get_mini_boxes(self, contour):
bounding_box = cv2.minAreaRect(contour)
points = sorted(list(cv2.boxPoints(bounding_box)), key=lambda x: x[0])
index_1, index_2, index_3, index_4 = 0, 1, 2, 3
if points[1][1] > points[0][1]:
index_1 = 0
index_4 = 1
else:
index_1 = 1
index_4 = 0
if points[3][1] > points[2][1]:
index_2 = 2
index_3 = 3
else:
index_2 = 3
index_3 = 2
box = [points[index_1], points[index_2], points[index_3], points[index_4]]
return box, min(bounding_box[1])
示例10: compute_cell_hulls
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def compute_cell_hulls(self):
"""
Run find_table_cell_polygons() and compute a rectangle enclosing the cell (for each cell).
For most (4-point) cells, this is equivalent to the original path, however this removes
small irregularities and extra points from larger, 5+-point cells (mostly merged cells)
"""
self.compute_cell_polygons()
# cv2 convexHull / minAreaRect only work with integer coordinates.
self.cell_hulls = [
cv2.boxPoints(cv2.minAreaRect(np.rint(self.cluster_coords[path]).astype(int)))
for path in self.cell_polygons]
# Compute centers of cell hulls
self.cell_centers = np.zeros((len(self.cell_hulls), 2))
for i in range(len(self.cell_hulls)):
hull_points = self.cell_hulls[i]
self.cell_centers[i] = cv_algorithms.meanCenter(hull_points)
示例11: masks_to_rects
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def masks_to_rects(masks):
rects = []
for mask in masks:
decoded_mask = mask
contours = cv2.findContours(decoded_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2]
areas = []
boxes = []
for contour in contours:
area = cv2.contourArea(contour)
areas.append(area)
rect = cv2.minAreaRect(contour)
box = cv2.boxPoints(rect)
box = np.int0(box)
boxes.append(box)
if areas:
i = np.argmax(areas)
rects.append(boxes[i])
return rects
示例12: remove_border
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def remove_border(contour, ary):
"""Remove everything outside a border contour."""
# Use a rotated rectangle (should be a good approximation of a border).
# If it's far from a right angle, it's probably two sides of a border and
# we should use the bounding box instead.
c_im = np.zeros(ary.shape)
r = cv2.minAreaRect(contour)
degs = r[2]
if angle_from_right(degs) <= 10.0:
box = cv2.boxPoints(r)
box = np.int0(box)
cv2.drawContours(c_im, [box], 0, 255, -1)
cv2.drawContours(c_im, [box], 0, 0, 4)
else:
x1, y1, x2, y2 = cv2.boundingRect(contour)
cv2.rectangle(c_im, (x1, y1), (x2, y2), 255, -1)
cv2.rectangle(c_im, (x1, y1), (x2, y2), 0, 4)
return np.minimum(c_im, ary)
示例13: drawBrushesOnImage
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def drawBrushesOnImage(brushes, color, im, pc, image_scale, fill=True):
for brush in brushes:
center = pc.tgcToCV2(brush["position"]["x"], brush["position"]["z"], image_scale)
center = (center[1], center[0]) # In point coordinates, not pixel
width = brush["scale"]["x"] / image_scale
height = brush["scale"]["z"] / image_scale
rotation = - brush["rotation"]["y"] # Inverted degrees, cv2 bounding_box uses degrees
thickness = 4
if fill:
thickness = -1 # Negative thickness is a filled ellipse
brush_type_name = tgc_definitions.brushes.get(int(brush["type"]), "unknown")
if 'square' in brush_type_name:
box_points = cv2.boxPoints((center, (2.0*width, 2.0*height), rotation)) # Squares seem to be larger than circles
box_points = np.int32([box_points]) # Bug with fillPoly, needs explict cast to 32bit
if fill:
cv2.fillPoly(im, box_points, color, lineType=cv2.LINE_AA)
else:
cv2.polylines(im, box_points, True, color, thickness, lineType=cv2.LINE_AA)
else: # Draw as ellipse for now
'''center – The rectangle mass center.
size – Width and height of the rectangle.
angle – The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.'''
bounding_box = (center, (1.414*width, 1.414*height), rotation) # Circles seem to scale according to radius
cv2.ellipse(im, bounding_box, color, thickness=thickness, lineType=cv2.LINE_AA)
示例14: mask2poly_single
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def mask2poly_single(binary_mask):
"""
:param binary_mask:
:return:
"""
# try:
contours, hierarchy = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
max_contour = max(contours, key=len)
rect = cv2.minAreaRect(max_contour)
poly = cv2.boxPoints(rect)
poly = TuplePoly2Poly(poly)
return poly
示例15: seg2poly_old
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import boxPoints [as 别名]
def seg2poly_old(rle):
# TODO: debug for this function
"""
This function transform a single encoded RLE to a single poly
:param seg: RlE
:return: poly
"""
# try:
# binary_mask = mask_utils.decode(rle)
# contours, hierarchy = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL)
# contour_lens = np.array(list(map(len, contours)))
# max_id = contour_lens.argmax()
# max_contour = contours[max_id]
# rect = cv2.minAreaRect(max_contour)
# poly = cv2.boxPoints(rect)
# return poly
# except:
# return -1
try:
binary_mask = mask_utils.decode(rle)
contours, hierarchy = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# len is not appropriate
# contour_lens = np.array(list(map(len, contours)))
# max_id = contour_lens.argmax()
contour_areas = np.array(list(map(cv2.contourArea, contours)))
max_id = contour_areas.argmax()
max_contour = contours[max_id]
rect = cv2.minAreaRect(max_contour)
poly = cv2.boxPoints(rect)
poly = TuplePoly2Poly(poly)
return poly
except:
return []