Rebuild extension function in python opencv, at what point should it be used?

How can I calculate the first and second moment in python opencv and get the form lengthening function?

I'm not sure about the definition of aspect ratio, I found: "The less common aspect ratio of aspect ratio is defined as the square root of the ratio of the two second moments of a particle around its principal axes" (link: http://en.wikipedia.org/wiki/Shape_factor_%28image_analysis_and_microscopy%29 #Elongation_shape_factor )

f_elong = sqrt (i2/i1)

      

definition-based, are i2 and i1 spatial moments, focal points, or normalized focal points ( http://opencv.willowgarage.com/documentation/cpp/imgproc_structural_analysis_and_shape_descriptors.html )?

+3


source to share


1 answer


There are several ways to calculate elongation, see Measuring Elongation from Form Boundary for a more standard one, and a proposal for a new one.

Based on this work, the standard E

form elongation S

is defined as follows:

enter image description here



where all the points used are central. This feature "follows from the definition of the orientation of the shape, which is based on the axis of the last second moment of inertia. Similarly, the axis of the second least moment of inertia is the line that minimizes the integral of the squares of the distances of points (belonging to the shape) to the line" (taken in the same way as from the article ).

In OpenCV, this directly translates to:

import sys
import cv2

def elongation(m):
    x = m['mu20'] + m['mu02']
    y = 4 * m['mu11']**2 + (m['mu20'] - m['mu02'])**2
    return (x + y**0.5) / (x - y**0.5)

img = cv2.cvtColor(cv2.imread(sys.argv[1]), cv2.COLOR_BGR2GRAY)
# Assuming input has grayscale dark contours:
img = 255 - cv2.threshold(img, 0, 255, cv2.THRESH_OTSU)[1]

m = cv2.moments(img)
print elongation(m)

      

+2


source







All Articles