本文整理汇总了Python中cv2.RETR_LIST属性的典型用法代码示例。如果您正苦于以下问题:Python cv2.RETR_LIST属性的具体用法?Python cv2.RETR_LIST怎么用?Python cv2.RETR_LIST使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类cv2
的用法示例。
在下文中一共展示了cv2.RETR_LIST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _find_size_candidates
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def _find_size_candidates(self, image):
binary_image = self._filter_image(image)
_, contours, _ = cv2.findContours(binary_image,
cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
size_candidates = []
for contour in contours:
bounding_rect = cv2.boundingRect(contour)
contour_area = cv2.contourArea(contour)
if self._is_valid_contour(contour_area, bounding_rect):
candidate = (bounding_rect[2] + bounding_rect[3]) / 2
size_candidates.append(candidate)
return size_candidates
示例2: find_squares
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def find_squares(img):
img = cv2.GaussianBlur(img, (5, 5), 0)
squares = []
for gray in cv2.split(img):
for thrs in xrange(0, 255, 26):
if thrs == 0:
bin = cv2.Canny(gray, 0, 50, apertureSize=5)
bin = cv2.dilate(bin, None)
else:
retval, bin = cv2.threshold(gray, thrs, 255, cv2.THRESH_BINARY)
bin, contours, hierarchy = cv2.findContours(bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
cnt_len = cv2.arcLength(cnt, True)
cnt = cv2.approxPolyDP(cnt, 0.02*cnt_len, True)
if len(cnt) == 4 and cv2.contourArea(cnt) > 1000 and cv2.isContourConvex(cnt):
cnt = cnt.reshape(-1, 2)
max_cos = np.max([angle_cos( cnt[i], cnt[(i+1) % 4], cnt[(i+2) % 4] ) for i in xrange(4)])
if max_cos < 0.1:
squares.append(cnt)
return squares
示例3: findPossibleCharsInPlate
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def findPossibleCharsInPlate(imgGrayscale, imgThresh):
listOfPossibleChars = [] # this will be the return value
contours = []
imgThreshCopy = imgThresh.copy()
# find all contours in plate
contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours: # for each contour
possibleChar = PossibleChar.PossibleChar(contour)
if checkIfPossibleChar(possibleChar): # if contour is a possible char, note this does not compare to other chars (yet) . . .
listOfPossibleChars.append(possibleChar) # add to list of possible chars
# end if
# end if
return listOfPossibleChars
# end function
###################################################################################################
示例4: polygons
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def polygons(self):
"""
Returns or generates :class:`Polygons` representation of mask.
:returns: Polygons representation
:rtype: :class:`Polygons`
"""
if not self._c_polygons:
# Generate polygons from mask
mask = self.array.astype(np.uint8)
mask = cv2.copyMakeBorder(mask, 1, 1, 1, 1, cv2.BORDER_CONSTANT, value=0)
polygons = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE, offset=(-1, -1))
polygons = polygons[0] if len(polygons) == 2 else polygons[1]
polygons = [polygon.flatten() for polygon in polygons]
self._c_polygons = Polygons(polygons)
self._c_polygons._c_mask = self
return self._c_polygons
示例5: crop_image
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def crop_image(silhs):
res = np.asarray(silhs).any(axis=0)
cnts, hier = cv2.findContours(
np.uint8(res) * 255, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
"""
checks = []
for cnt in cnts:
kk = np.zeros((1000, 1000, 3), dtype=np.uint8)
hull = cv2.convexHull(cnt)
cv2.drawContours(kk, [cnt], 0, (255,255,255), -1)
checks.append(kk)
"""
max_id = 0
max_length = len(cnts[0])
for i in range(1, len(cnts)):
if len(cnts[i]) > max_length:
max_id = i
max_length = len(cnts[i])
(x, y, w, h) = cv2.boundingRect(cnts[max_id])
return (x, y, w, h)
示例6: crop_and_clean_mask_to_int
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def crop_and_clean_mask_to_int(mask, x, y, w, h):
mask = np.uint8(mask[y:y + h, x:x + w]) * 255
# TODO: put this into a function (it's used above as well)
cnts, hier = cv2.findContours(mask.copy(), cv2.RETR_LIST,
cv2.CHAIN_APPROX_SIMPLE)
max_id = 0
max_length = len(cnts[0])
for i in range(1, len(cnts)):
if len(cnts[i]) > max_length:
max_id = i
max_length = len(cnts[i])
tmp_mask = np.dstack((mask, mask, mask))
for i, cnt in enumerate(cnts):
if i != max_id:
cv2.drawContours(tmp_mask, [cnt], 0, (0, 0, 0), -1)
return cv2.split(tmp_mask)[0]
示例7: detect_edge
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def detect_edge(self, image, enabled_transform = False):
dst = None
orig = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 0, 20)
_, contours, _ = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)
for cnt in contours:
epsilon = 0.051 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
if len(approx) == 4:
target = approx
cv2.drawContours(image, [target], -1, (0, 255, 0), 2)
if enabled_transform:
approx = rect.rectify(target)
# pts2 = np.float32([[0,0],[800,0],[800,800],[0,800]])
# M = cv2.getPerspectiveTransform(approx,pts2)
# dst = cv2.warpPerspective(orig,M,(800,800))
dst = self.four_point_transform(orig, approx)
break
return image, dst
示例8: find_marker
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def find_marker(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(gray, 35, 125)
(cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
c = max(cnts, key = cv2.contourArea)
# compute the bounding box of the of the paper region and return it
return cv2.minAreaRect(c)
示例9: find_bbox
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def find_bbox(img, img_closed): # 寻找身份证正反面区域
"""
根据二值化结果判定并裁剪出身份证正反面区域
:param img: 原始RGB图片
:param img_closed: 二值化后的图片
:return: 身份证正反面区域
"""
(contours, _) = cv2.findContours(img_closed.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # 求出框的个数
# 这里opencv如果版本不对(4.0或以上)会报错,只需把(contours, _)改成 (_, contours, _)
contours = sorted(contours, key=cv2.contourArea, reverse=True) # 按照面积大小排序
countours_res = []
for i in range(0, len(contours)):
area = cv2.contourArea(contours[i]) # 计算面积
if (area <= 0.4 * img.shape[0] * img.shape[1]) and (area >= 0.05 * img.shape[0] * img.shape[1]):
# 人为设定,身份证正反面框的大小不会超过整张图片大小的0.4,不会小于0.05(这个参数随便设置的)
rect = cv2.minAreaRect(contours[i]) # 最小外接矩,返回值有中心点坐标,矩形宽高,倾斜角度三个参数
box = cv2.boxPoints(rect)
left_down, right_down, left_up, right_up = point_judge([int(rect[0][0]), int(rect[0][1])], box)
src = np.float32([left_down, right_down, left_up, right_up]) # 这里注意必须对应
dst = np.float32([[0, 0], [int(max(rect[1][0], rect[1][1])), 0], [0, int(min(rect[1][0], rect[1][1]))],
[int(max(rect[1][0], rect[1][1])),
int(min(rect[1][0], rect[1][1]))]]) # rect中的宽高不清楚是个怎么机制,但是对于身份证,肯定是宽大于高,因此加个判定
m = cv2.getPerspectiveTransform(src, dst) # 得到投影变换矩阵
result = cv2.warpPerspective(img, m, (int(max(rect[1][0], rect[1][1])), int(min(rect[1][0], rect[1][1]))),
flags=cv2.INTER_CUBIC) # 投影变换
countours_res.append(result)
return countours_res # 返回身份证区域
示例10: find_items
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def find_items(maze_image):
# Preprocessing to find the contour of the shapes
h, w = maze_image.shape[0], maze_image.shape[1]
dim = (h+w)//2
b_and_w = cv2.cvtColor(maze_image, cv2.COLOR_BGR2GRAY)
edges = cv2.GaussianBlur(b_and_w, (11, 11), 0)
edges = cv2.adaptiveThreshold(edges, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 9, 2)
cv2.rectangle(edges,(0, 0),(w-1,h-1),(255,255,255),16)
contours, _ = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
# cv2.imshow('d', edges)
items = []
if contours:
item_mask = np.zeros(edges.shape, np.uint8)
conts = sorted(contours, key=lambda x: cv2.contourArea(x), reverse=False)
for cnt in conts:
if cv2.contourArea(cnt) > 0.35*dim:
return items, item_mask
elif cv2.contourArea(cnt) > 0.05*dim:
d = np.mean(cnt, axis=0)
d[0][0], d[0][1] = int(round(d[0][0])), int(round(d[0][1]))
# TODO adjust the size here?
if cv2.contourArea(cnt) < 0.1*dim:
items.append((d, 'smol'))
cv2.drawContours(item_mask, [cnt], -1, (255,255,255), -1)
else:
items.append((d, 'big'))
cv2.drawContours(item_mask, [cnt], -1, (255,255,255), -1)
return items, item_mask
示例11: findPossibleCharsInScene
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def findPossibleCharsInScene(imgThresh):
listOfPossibleChars = [] # this will be the return value
intCountOfPossibleChars = 0
imgThreshCopy = imgThresh.copy()
contours, npaHierarchy = cv2.findContours(imgThreshCopy, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) # find all contours
height, width = imgThresh.shape
imgContours = np.zeros((height, width, 3), np.uint8)
for i in range(0, len(contours)): # for each contour
possibleChar = PossibleChar.PossibleChar(contours[i])
if DetectChars.checkIfPossibleChar(possibleChar): # if contour is a possible char, note this does not compare to other chars (yet) . . .
intCountOfPossibleChars = intCountOfPossibleChars + 1 # increment count of possible chars
listOfPossibleChars.append(possibleChar) # and add to list of possible chars
# end if
# end for
return listOfPossibleChars
# end function
###################################################################################################
示例12: findEllipses
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def findEllipses(edges):
contours, _ = cv2.findContours(edges.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
ellipseMask = np.zeros(edges.shape, dtype=np.uint8)
contourMask = np.zeros(edges.shape, dtype=np.uint8)
pi_4 = np.pi * 4
for i, contour in enumerate(contours):
if len(contour) < 5:
continue
area = cv2.contourArea(contour)
if area <= 100: # skip ellipses smaller then 10x10
continue
arclen = cv2.arcLength(contour, True)
circularity = (pi_4 * area) / (arclen * arclen)
ellipse = cv2.fitEllipse(contour)
poly = cv2.ellipse2Poly((int(ellipse[0][0]), int(ellipse[0][1])), (int(ellipse[1][0] / 2), int(ellipse[1][1] / 2)), int(ellipse[2]), 0, 360, 5)
# if contour is circular enough
if circularity > 0.6:
cv2.fillPoly(ellipseMask, [poly], 255)
continue
# if contour has enough similarity to an ellipse
similarity = cv2.matchShapes(poly.reshape((poly.shape[0], 1, poly.shape[1])), contour, cv2.cv.CV_CONTOURS_MATCH_I2, 0)
if similarity <= 0.2:
cv2.fillPoly(contourMask, [poly], 255)
return ellipseMask, contourMask
示例13: find_markers_from_img_thresh
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def find_markers_from_img_thresh(img_thresh, max_dist_between_centers=3, min_radius_circle=4,
max_radius_circle=35, min_radius_marker=7):
contours = cv2.findContours(img_thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
list_potential_markers = []
for cnt in contours:
(x, y), radius = cv2.minEnclosingCircle(cnt)
if not min_radius_circle < radius < max_radius_circle:
continue
center = (int(round(x)), int(round(y)))
radius = int(radius)
list_potential_markers.append(PotentialMarker(center, radius, cnt))
list_potential_markers = sorted(list_potential_markers, key=lambda m: m.x)
list_good_candidates = []
for i, potential_marker in enumerate(list_potential_markers):
if potential_marker.is_merged:
continue
marker1 = Marker(potential_marker)
center_marker = marker1.get_center()
for potential_marker2 in list_potential_markers[i + 1:]:
if potential_marker.is_merged:
continue
center_potential = potential_marker2.get_center()
if center_potential[0] - center_marker[0] > max_dist_between_centers:
break
dist = euclidean_dist_2_pts(center_marker, center_potential)
if dist <= max_dist_between_centers:
marker1.add_circle(potential_marker2)
center_marker = marker1.get_center()
if marker1.nb_circles() > 2 and marker1.radius >= min_radius_marker:
list_good_candidates.append(marker1)
marker1.get_id_from_slice(img_thresh)
return list_good_candidates
示例14: biggest_contours_finder
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def biggest_contours_finder(img, nb_contours_max=3):
"""
Fonction to find the biggest contour in an binary image
:param img: Binary Image
:param nb_contours_max: maximal number of contours which will be returned
:return: biggest contours found
"""
contours = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)[-2]
if not contours:
return None
contours_area = list()
for cnt in contours:
contours_area.append(cv2.contourArea(cnt))
biggest_contours = []
le = len(contours_area)
if nb_contours_max > le:
nb_contours = le
id_contours_sorted_init = list(range(nb_contours))
else:
nb_contours = nb_contours_max
id_contours_sorted_init = np.argpartition(contours_area, -nb_contours)[-nb_contours:]
id_contours_sorted = [x for x in sorted(id_contours_sorted_init, key=lambda idi: -contours_area[idi])]
for i in range(nb_contours):
id_used = id_contours_sorted[i]
if contours_area[id_used] < 400:
break
biggest_contours.append(contours[id_used])
return biggest_contours
示例15: detect_biggest_polygon
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import RETR_LIST [as 别名]
def detect_biggest_polygon(binary_img):
binary_img_to_process = binary_img.copy() # contour detection will mess up the image
contours, _ = cv2.findContours(binary_img_to_process, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
biggest_contour = None
if contours != []:
biggest_contour_index = 0
for i in range(1, len(contours)):
if cv2.contourArea(contours[i]) > cv2.contourArea(contours[biggest_contour_index]):
biggest_contour_index = i
biggest_contour = contours[biggest_contour_index]
return biggest_contour