简体   繁体   中英

how to set python numpy array size limit

I'm trying to calculate the average size of a contour/ball. I've used arclength to calculate the perimeter of the ball, then found out the diameter. My problem is that the contour values is constantly changing.

I want to input the first 10 values of the diameter into a np.array and calculate the mean to use as my object size. I'm new to python, been trying multiple methods but haven't found a solution to either set the size or extract the first 10 array tuples. I've tried pulling u the first 10 using for i in range.

My current code is:

def average_diam (diameter):
    av_diameter = np.array(diameter)
    for i in np.arange(1, len(av_diameter)):
            for i in av_diameter >= 10:
                    average = np.mean()
                    print(average)


perimeter = cv2.arcLength(c, True)
diameter = perimeter / pi
average = average_diam(diameter)

thanks for the help!!

Assuming diameter is an list of elements you simply can do something like

def average_diam(x):
    av_diameter = np.zeros(10)
    for i in range(0, 10):
         av_diameter[i] = x[i]
    return np.mean(av_diameter)

average_diam(diameter)

You honestly can just do a one liner for this without calling a function:

average = np.mean(np.asarray(diameter[0:10]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM