I am looking to fill a 900-some by 38 array in python. Each row corresponds to a particular contour and each column corresponds to a feature for this contour. I'm looking to loop through each contour, and calculate the region properties for the contour, but I haven't figured out the most efficient way to do so.
I thought about making a list and appending each column value for each contour, and then trying to stack all those lists on top of each other, but it seems to me that I wouldn't want to make lists with the built in function and then try to use numpy to create arrays. I'm not even sure the data structures are compatible.
Every example I've seen has fed an array data, but I want to calculate a value and then stick it in an array.
The basics of my code are below. Is creating a 1D array 34,200 elements long, and then reshaping it my best option?
cs = find_contours(Image)
print len(cs)
for c in cs:
Area = moments['m00']
features.append(Area)
Perimeter = cv2.arcLength(c,True)
# bounding box: x,y,width,height
BoundingBox = cv2.boundingRect(c)
# centroid = m10/m00, m01/m00 (x,y)
Centroid = ( moments['m10']/moments['m00'],moments['m01']/moments['m00'] )
# EquivDiameter: diameter of circle with same area as region
EquivDiameter = ny.sqrt(4*Area/ny.pi)
features.append(EquivDiameter)
# Extent: ratio of area of region to area of bounding box
Extent = Area/(BoundingBox[2]*BoundingBox[3])
# CONVEX HULL stuff
# convex hull vertices
ConvexHull = cv2.convexHull(c)
ConvexArea = cv2.contourArea(ConvexHull)
# Solidity := Area/ConvexArea
Solidity = Area/ConvexArea
So I'm looking to save values such as Area, Perimeter, etc for each contour as I loop through them.
One option would be to make a single list of all the values, make that a numpy array, and then reshape the array. So for example if you had three contours with three data points each:
This allows you to avoid nested loops but you might need to look at faster options if your dataset gets large enough.