本文整理匯總了Python中cv2.convexHull方法的典型用法代碼示例。如果您正苦於以下問題:Python cv2.convexHull方法的具體用法?Python cv2.convexHull怎麽用?Python cv2.convexHull使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類cv2
的用法示例。
在下文中一共展示了cv2.convexHull方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: FindHullDefects
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [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: calculateFingers
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def calculateFingers(res,drawing): # -> finished bool, cnt: finger count
# convexity defect
hull = cv2.convexHull(res, returnPoints=False)
if len(hull) > 3:
defects = cv2.convexityDefects(res, hull)
if type(defects) != type(None): # avoid crashing. (BUG not found)
cnt = 0
for i in range(defects.shape[0]): # calculate the angle
s, e, f, d = defects[i][0]
start = tuple(res[s][0])
end = tuple(res[e][0])
far = tuple(res[f][0])
a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c)) # cosine theorem
if angle <= math.pi / 2: # angle less than 90 degree, treat as fingers
cnt += 1
cv2.circle(drawing, far, 8, [211, 84, 0], -1)
return True, cnt
return False, 0
# Camera
示例3: manage_image_opr
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def manage_image_opr(frame, hand_hist):
hist_mask_image = hist_masking(frame, hand_hist)
hist_mask_image = cv2.erode(hist_mask_image, None, iterations=2)
hist_mask_image = cv2.dilate(hist_mask_image, None, iterations=2)
contour_list = contours(hist_mask_image)
max_cont = max(contour_list, key=cv2.contourArea)
cnt_centroid = centroid(max_cont)
cv2.circle(frame, cnt_centroid, 5, [255, 0, 255], -1)
if max_cont is not None:
hull = cv2.convexHull(max_cont, returnPoints=False)
defects = cv2.convexityDefects(max_cont, hull)
far_point = farthest_point(defects, max_cont, cnt_centroid)
print("Centroid : " + str(cnt_centroid) + ", farthest Point : " + str(far_point))
cv2.circle(frame, far_point, 5, [0, 0, 255], -1)
if len(traverse_point) < 20:
traverse_point.append(far_point)
else:
traverse_point.pop(0)
traverse_point.append(far_point)
draw_circles(frame, traverse_point)
示例4: merge_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def merge_img(src_img, dst_img, dst_matrix, dst_points, blur_detail_x=None, blur_detail_y=None, mat_multiple=None):
face_mask = np.zeros(src_img.shape, dtype=src_img.dtype)
for group in core.OVERLAY_POINTS:
cv2.fillConvexPoly(face_mask, cv2.convexHull(dst_matrix[group]), (255, 255, 255))
r = cv2.boundingRect(np.float32([dst_points[:core.FACE_END]]))
center = (r[0] + int(r[2] / 2), r[1] + int(r[3] / 2))
if mat_multiple:
mat = cv2.getRotationMatrix2D(center, 0, mat_multiple)
face_mask = cv2.warpAffine(face_mask, mat, (face_mask.shape[1], face_mask.shape[0]))
if blur_detail_x and blur_detail_y:
face_mask = cv2.blur(face_mask, (blur_detail_x, blur_detail_y), center)
return cv2.seamlessClone(np.uint8(dst_img), src_img, face_mask, center, cv2.NORMAL_CLONE)
示例5: iou_rotate_calculate2
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def iou_rotate_calculate2(boxes1, boxes2):
ious = []
if boxes1.shape[0] != 0:
area1 = boxes1[:, 2] * boxes1[:, 3]
area2 = boxes2[:, 2] * boxes2[:, 3]
for i in range(boxes1.shape[0]):
temp_ious = []
r1 = ((boxes1[i][0], boxes1[i][1]), (boxes1[i][2], boxes1[i][3]), boxes1[i][4])
r2 = ((boxes2[i][0], boxes2[i][1]), (boxes2[i][2], boxes2[i][3]), boxes2[i][4])
int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]
if int_pts is not None:
order_pts = cv2.convexHull(int_pts, returnPoints=True)
int_area = cv2.contourArea(order_pts)
inter = int_area * 1.0 / (area1[i] + area2[i] - int_area)
temp_ious.append(inter)
else:
temp_ious.append(0.0)
ious.append(temp_ious)
return np.array(ious, dtype=np.float32)
示例6: blendImages
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def blendImages(src, dst, mask, featherAmount=0.2):
#indeksy nie czarnych pikseli maski
maskIndices = np.where(mask != 0)
#te same indeksy tylko, ze teraz w jednej macierzy, gdzie kazdy wiersz to jeden piksel (x, y)
maskPts = np.hstack((maskIndices[1][:, np.newaxis], maskIndices[0][:, np.newaxis]))
faceSize = np.max(maskPts, axis=0) - np.min(maskPts, axis=0)
featherAmount = featherAmount * np.max(faceSize)
hull = cv2.convexHull(maskPts)
dists = np.zeros(maskPts.shape[0])
for i in range(maskPts.shape[0]):
dists[i] = cv2.pointPolygonTest(hull, (maskPts[i, 0], maskPts[i, 1]), True)
weights = np.clip(dists / featherAmount, 0, 1)
composedImg = np.copy(dst)
composedImg[maskIndices[0], maskIndices[1]] = weights[:, np.newaxis] * src[maskIndices[0], maskIndices[1]] + (1 - weights[:, np.newaxis]) * dst[maskIndices[0], maskIndices[1]]
return composedImg
#uwaga, tutaj src to obraz, z ktorego brany bedzie kolor
示例7: fill_convex
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def fill_convex (image):
H, W = image.shape
padded = np.zeros((H+20, W+20), dtype=np.uint8)
padded[10:(10+H),10:(10+W)] = image
contours = measure.find_contours(padded, 0.5)
if len(contours) == 0:
return image
if len(contours) == 1:
contour = contours[0]
else:
contour = np.vstack(contours)
cc = np.zeros_like(contour, dtype=np.int32)
cc[:,0] = contour[:, 1]
cc[:,1] = contour[:, 0]
hull = cv2.convexHull(cc)
contour = hull.reshape((1, -1, 2))
cv2.fillPoly(padded, contour, 1)
return padded[10:(10+H),10:(10+W)]
示例8: visualize
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def visualize(frame, coordinates_list, alpha = 0.80, color=[255, 255, 255]):
"""
Args:
1. frame: OpenCV's image which has to be visualized.
2. coordinates_list: List of coordinates which will be visualized in the given `frame`
3. alpha, color: Some parameters which help in visualizing properly.
A convex hull will be shown for each element in the `coordinates_list`
"""
layer = frame.copy()
output = frame.copy()
for coordinates in coordinates_list:
c_hull = cv2.convexHull(coordinates)
cv2.drawContours(layer, [c_hull], -1, color, -1)
cv2.addWeighted(layer, alpha, output, 1 - alpha, 0, output)
cv2.imshow("Output", output)
示例9: check_if_top_is_unreliable
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def check_if_top_is_unreliable(mean_pred, albu_pred):
unreliable = np.zeros_like(albu_pred)
rows, cols = unreliable.shape
unreliable[(albu_pred > 30) & (albu_pred < 210)] = 255
unreliable = cv2.erode(unreliable, (55, 55), iterations=10)
unreliable = unreliable[0:rows // 2, ...]
biggest = biggest_contour(unreliable)
if biggest is None:
return None
if cv2.contourArea(biggest) > 40000:
x, y, w, h = cv2.boundingRect(biggest)
x, y, w, h = max(x - 50, 0), y - 50, w + 100, h + 100
mask = (albu_pred > 55).astype(np.uint8) * 255
c = biggest_contour(mask[y:y + h, x:x + w])
c = cv2.convexHull(c)
mask[y:y + h, x:x + w] = cv2.drawContours(mask[y:y + h, x:x + w], [c], -1, 255, -1)
result = (mean_pred > 127).astype(np.uint8) * 255
result[y:y + h, x:x + w] = mask[y:y + h, x:x + w]
return result
return None
示例10: crop_image
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [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)
示例11: fit_circle
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def fit_circle(self, contour, eccentricity, area_ratio,min_radius = 0, max_radius = 500000):
#convert to convex hull
hull = cv2.convexHull(contour)
min_area = math.pi * min_radius * min_radius
max_area = math.pi * max_radius * max_radius
c_area = cv2.contourArea(hull)
#check for a shape of a certain size and corner resolution
if len(hull) > 4:
#fit an ellipse
ellipse = cv2.fitEllipse(hull)
radius = int((ellipse[1][0] + ellipse[1][0]) /4.0)
#check for a circular ellipse
if ellipse[1][0] * 1.0/ ellipse[1][1] > eccentricity and max_radius > radius > min_radius:
#compare area of raw hull vs area of ellipse to ellinate objects with corners
e_area = (ellipse[1][0]/2.0) * (ellipse[1][1]/2.0) * math.pi
if (c_area / e_area) > area_ratio:
center = Point(int(ellipse[0][0]), int(ellipse[0][1]))
radius = int((ellipse[1][0] + ellipse[1][0]) /4.0) #average and diameter -> radius
return Circle(center,radius,contour,ellipse)
return None
示例12: blur
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def blur(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = detector(gray, 0)
mask = np.zeros(image.shape[:2], np.uint8)
blurred_image = image.copy()
for face in faces: # if there are faces
(x, y, w, h) = (face.left(), face.top(), face.width(), face.height())
blurred_image[y : y + h, x : x + w, :] = anonymize_face_pixelate(
blurred_image[y : y + h, x : x + w, :], blocks=10
)
# *** Facial Landmarks detection
shape = predictor(gray, face)
shape = face_utils.shape_to_np(shape)
# Get mask with only face shape
shape = cv2.convexHull(shape)
cv2.drawContours(mask, [shape], -1, 255, -1)
# Replace blurred image only in mask
mask = mask / 255.0
mask = np.expand_dims(mask, axis=-1)
image = (1.0 - mask) * image + mask * blurred_image
image = image.astype(np.uint8)
return image
示例13: merge_img
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def merge_img(src_img, dst_img, dst_matrix, dst_points, k_size=None, mat_multiple=None):
face_mask = np.zeros(src_img.shape, dtype=src_img.dtype)
for group in core.OVERLAY_POINTS:
cv2.fillConvexPoly(face_mask, cv2.convexHull(dst_matrix[group]), (255, 255, 255))
r = cv2.boundingRect(np.float32([dst_points[:core.FACE_END]]))
center = (r[0] + int(r[2] / 2), r[1] + int(r[3] / 2))
if mat_multiple:
mat = cv2.getRotationMatrix2D(center, 0, mat_multiple)
face_mask = cv2.warpAffine(face_mask, mat, (face_mask.shape[1], face_mask.shape[0]))
if k_size:
face_mask = cv2.blur(face_mask, k_size, center)
return cv2.seamlessClone(np.uint8(dst_img), src_img, face_mask, center, cv2.NORMAL_CLONE)
示例14: get_image_hull_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def get_image_hull_mask (image_shape, image_landmarks, eyebrows_expand_mod=1.0 ):
hull_mask = np.zeros(image_shape[0:2]+(1,),dtype=np.float32)
lmrks = expand_eyebrows(image_landmarks, eyebrows_expand_mod)
r_jaw = (lmrks[0:9], lmrks[17:18])
l_jaw = (lmrks[8:17], lmrks[26:27])
r_cheek = (lmrks[17:20], lmrks[8:9])
l_cheek = (lmrks[24:27], lmrks[8:9])
nose_ridge = (lmrks[19:25], lmrks[8:9],)
r_eye = (lmrks[17:22], lmrks[27:28], lmrks[31:36], lmrks[8:9])
l_eye = (lmrks[22:27], lmrks[27:28], lmrks[31:36], lmrks[8:9])
nose = (lmrks[27:31], lmrks[31:36])
parts = [r_jaw, l_jaw, r_cheek, l_cheek, nose_ridge, r_eye, l_eye, nose]
for item in parts:
merged = np.concatenate(item)
cv2.fillConvexPoly(hull_mask, cv2.convexHull(merged), (1,) )
return hull_mask
示例15: get_image_eye_mask
# 需要導入模塊: import cv2 [as 別名]
# 或者: from cv2 import convexHull [as 別名]
def get_image_eye_mask (image_shape, image_landmarks):
if len(image_landmarks) != 68:
raise Exception('get_image_eye_mask works only with 68 landmarks')
h,w,c = image_shape
hull_mask = np.zeros( (h,w,1),dtype=np.float32)
image_landmarks = image_landmarks.astype(np.int)
cv2.fillConvexPoly( hull_mask, cv2.convexHull( image_landmarks[36:42]), (1,) )
cv2.fillConvexPoly( hull_mask, cv2.convexHull( image_landmarks[42:48]), (1,) )
dilate = h // 32
hull_mask = cv2.dilate(hull_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(dilate,dilate)), iterations = 1 )
blur = h // 16
blur = blur + (1-blur % 2)
hull_mask = cv2.GaussianBlur(hull_mask, (blur, blur) , 0)
hull_mask = hull_mask[...,None]
return hull_mask