I'm trying to calculate the bending radius of a product under cyclic bending using image processing with OpenCV and Python. The image processing part is defined (the code is at Red dot coordinates detection )
The code returns center coordinates of contours of specific size. I'm now trying to populate a 2D array of these coordinates so that I can use coordinates of two adjacent points to calculate the bending radius.
The problem is that the array population should be inside the for loop and that when I allocate the array beforehand, I don't know the size.
EDIT: bellow is the code from the link above with the FOR loop for filtering out the contours by area and displaying the center coordinates.
import cv2
import numpy as np
from matplotlib import pyplot as plt
import imutils
#load image
img = cv2.imread('dot4_red.jpg')
#apply median blur, 15 means it's smoothing image 15x15 pixels
blur = cv2.medianBlur(img,15)
#convert to hsv
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
#color definition
red_lower = np.array([0,0,240])
red_upper = np.array([10,10,255])
#red color mask (sort of thresholding, actually segmentation)
mask = cv2.inRange(hsv, red_lower, red_upper)
#copy image for, .findContours distorts the source image
mask_copy = mask.copy()
#find contours
cnts = cv2.findContours(mask_copy,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
#extract contours from the list??
cnts = imutils.grab_contours(cnts)
#count number of conoturs of specific size
s1 = 500
s2 = 10000
array = [] #create empty array of no size
for cnt in cnts:
area = cv2.contourArea(cnt)
moment = cv2.moments(cnt)
if s1<area<s2:
print area
c_x = int(moment["m10"]/moment["m00"])
c_y = int(moment["m01"]/moment["m00"])
print (c_x, c_y)
array.append(c_x, c_y)