本文整理匯總了Python中cv2.minAreaRect方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.minAreaRect方法的具體用法?Python cv2.minAreaRect怎麽用?Python cv2.minAreaRect使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.minAreaRect方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mask2poly_single
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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 minAreaRect [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: check_plate
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [as 別名]
def check_plate(self, input_img, contour):
min_rect = cv2.minAreaRect(contour)
if self.validateRotationAndRatio(min_rect):
x, y, w, h = cv2.boundingRect(contour)
after_validation_img = input_img[y:y+h, x:x+w]
after_clean_plate_img, plateFound, coordinates = self.clean_plate(after_validation_img)
if plateFound:
characters_on_plate = self.find_characters_on_plate(after_clean_plate_img)
if (characters_on_plate is not None and len(characters_on_plate) > 5):
x1, y1, w1, h1 = coordinates
coordinates = x1+x, y1+y
after_check_plate_img = after_clean_plate_img
return after_check_plate_img, characters_on_plate, coordinates
return None, None, None
#################### PLATE FEATURES ####################
示例4: min_area_rect
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [as 別名]
def min_area_rect(xs, ys):
"""
Args:
xs: numpy ndarray with shape=(N,4). N is the number of oriented bboxes. 4 contains [x1, x2, x3, x4]
ys: numpy ndarray with shape=(N,4), [y1, y2, y3, y4]
Note that [(x1, y1), (x2, y2), (x3, y3), (x4, y4)] can represent an oriented bbox.
Return:
the oriented rects sorrounding the box, in the format:[cx, cy, w, h, theta].
"""
xs = np.asarray(xs, dtype = np.float32)
ys = np.asarray(ys, dtype = np.float32)
num_rects = xs.shape[0]
box = np.empty((num_rects, 5))#cx, cy, w, h, theta
for idx in xrange(num_rects):
points = zip(xs[idx, :], ys[idx, :])
cnt = util.img.points_to_contour(points)
rect = cv2.minAreaRect(cnt)
cx, cy = rect[0]
w, h = rect[1]
theta = rect[2]
box[idx, :] = [cx, cy, w, h, theta]
box = np.asarray(box, dtype = xs.dtype)
return box
示例5: sobelSecSearchPart
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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 minAreaRect [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 minAreaRect [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_contour_angle
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [as 別名]
def get_contour_angle(cnt):
"""
Return orientation of a contour
:param cnt: contour
:return: Angle in radians
"""
rotrect = cv2.minAreaRect(cnt)
angle = rotrect[-1]
size1, size2 = rotrect[1][0], rotrect[1][1]
ratio_size = float(size1) / float(size2)
if 1.25 > ratio_size > 0.75:
if angle < -45:
angle = 90 + angle
else:
if size1 < size2:
angle = angle + 180
else:
angle = angle + 90
if angle > 90:
angle = angle - 180
return math.radians(angle)
示例9: remove_border
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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.cv.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)
示例10: combine_line
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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
示例11: clean_plate
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [as 別名]
def clean_plate(self, plate):
gray = cv2.cvtColor(plate, cv2.COLOR_BGR2GRAY)
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
_, contours, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
if contours:
areas = [cv2.contourArea(c) for c in contours]
max_index = np.argmax(areas) # index of the largest contour in the area array
max_cnt = contours[max_index]
max_cntArea = areas[max_index]
x,y,w,h = cv2.boundingRect(max_cnt)
rect = cv2.minAreaRect(max_cnt)
rotatedPlate = self.crop_rotated_contour(plate, rect)
if not self.ratioCheck(max_cntArea, rotatedPlate.shape[1], rotatedPlate.shape[0]):
return plate, False, None
return rotatedPlate, True, [x, y, w, h]
else:
return plate, False, None
示例12: iou_rotate
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [as 別名]
def iou_rotate(box_a, box_b, method='union'):
rect_a = cv2.minAreaRect(box_a)
rect_b = cv2.minAreaRect(box_b)
r1 = cv2.rotatedRectangleIntersection(rect_a, rect_b)
if r1[0] == 0:
return 0
else:
inter_area = cv2.contourArea(r1[1])
area_a = cv2.contourArea(box_a)
area_b = cv2.contourArea(box_b)
union_area = area_a + area_b - inter_area
if union_area == 0 or inter_area == 0:
return 0
if method == 'union':
iou = inter_area / union_area
elif method == 'intersection':
iou = inter_area / min(area_a, area_b)
else:
raise NotImplementedError
return iou
示例13: get_mini_boxes
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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])
示例14: compute_cell_hulls
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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)
示例15: masks_to_rects
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import minAreaRect [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