本文整理汇总了Python中cv2.HuMoments方法的典型用法代码示例。如果您正苦于以下问题:Python cv2.HuMoments方法的具体用法?Python cv2.HuMoments怎么用?Python cv2.HuMoments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cv2
的用法示例。
在下文中一共展示了cv2.HuMoments方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: calculate_contour_features
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import HuMoments [as 别名]
def calculate_contour_features(contour):
"""Calculates interesting properties (features) of a contour.
We use these features to match shapes (contours). In this script,
we are interested in finding shapes in our input image that look like
a corner. We do that by calculating the features for many contours
in the input image and comparing these to the features of the corner
contour. By design, we know exactly what the features of the real corner
contour look like - check out the calculate_corner_features function.
It is crucial for these features to be invariant both to scale and rotation.
In other words, we know that a corner is a corner regardless of its size
or rotation. In the past, this script implemented its own features, but
OpenCV offers much more robust scale and rotational invariant features
out of the box - the Hu moments.
"""
moments = cv2.moments(contour)
return cv2.HuMoments(moments)
示例2: HuMoments
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import HuMoments [as 别名]
def HuMoments(m: List[int]) -> List[int]:
"""
Calculates seven Hu invariants
"""
# If image is not a single channel image convert it
if len(m.shape) != 2:
m = cv2.cvtColor(m, cv2.COLOR_BGR2GRAY)
m = cv2.moments(m)
hu_moments = cv2.HuMoments(m).flatten()
return hu_moments
示例3: get_hu_moments
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import HuMoments [as 别名]
def get_hu_moments(arr):
arr = invert_binary_image(arr)
if arr.shape != (32, 32):
arr.shape = (32, 32)
m = moments(arr.astype(np.float64), binaryImage=True)
hu = HuMoments(m)
return hu.flatten()
示例4: fd_hu_moments
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import HuMoments [as 别名]
def fd_hu_moments(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
feature = cv2.HuMoments(cv2.moments(image)).flatten()
return feature
# feature-descriptor-2: Haralick Texture
示例5: _getBlobFeatures
# 需要导入模块: import cv2 [as 别名]
# 或者: from cv2 import HuMoments [as 别名]
def _getBlobFeatures(blob_cnt, blob_mask, roi_image, roi_corner):
if blob_cnt.size > 0:
area = float(cv2.contourArea(blob_cnt))
# find use the best rotated bounding box, the fitEllipse function produces bad results quite often
# this method is better to obtain an estimate of the worm length than
# eccentricity
(CMx, CMy), (L, W), angle = cv2.minAreaRect(blob_cnt)
#adjust CM from the ROI reference frame to the image reference
CMx += roi_corner[0]
CMy += roi_corner[1]
if L == 0 or W == 0:
return None #something went wrong abort
if W > L:
L, W = W, L # switch if width is larger than length
quirkiness = np.sqrt(1 - W**2 / L**2)
hull = cv2.convexHull(blob_cnt) # for the solidity
solidity = area / cv2.contourArea(hull)
perimeter = float(cv2.arcLength(blob_cnt, True))
compactness = 4 * np.pi * area / (perimeter**2)
# calculate the mean intensity of the worm
intensity_mean, intensity_std = cv2.meanStdDev(roi_image, mask=blob_mask)
intensity_mean = intensity_mean[0,0]
intensity_std = intensity_std[0,0]
# calculate hu moments, they are scale and rotation invariant
hu_moments = cv2.HuMoments(cv2.moments(blob_cnt))
# save everything into the the proper output format
mask_feats = (CMx,
CMy,
area,
perimeter,
L,
W,
quirkiness,
compactness,
angle,
solidity,
intensity_mean,
intensity_std,
*hu_moments.flatten())
else:
return tuple([np.nan]*19)
return mask_feats