Python cv2.HuMoments() Examples

The following are 5 code examples of cv2.HuMoments(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: omr.py    From omr with MIT License 7 votes vote down vote up
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) 
Example #2
Source File: operations.py    From dffml with MIT License 5 votes vote down vote up
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 
Example #3
Source File: feature_extraction.py    From namsel with MIT License 5 votes vote down vote up
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() 
Example #4
Source File: global.py    From image-classification-python with MIT License 5 votes vote down vote up
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 
Example #5
Source File: getBlobsFeats.py    From tierpsy-tracker with MIT License 4 votes vote down vote up
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