I am trying to use cv2.FitEllipse to fit an ellipse to data using the following python code:
def getContours_test(clusters):
copy_clusters=clusters.copy()
int_cnts=[]
for label in np.unique(clusters):
mask = np.zeros(clusters.shape, dtype="uint8")
mask[clusters == label] = 255
cnts, hierarchy = \
cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
int_cnts.append(cnts)
list_contours=list(itertools.chain.from_iterable(int_cnts))
return copy_clusters, list_contours
def getEllipse_test(clusters, list_contours):
for index,cnt in enumerate(list_contours):
map_ellipse=np.ones(clusters.shape).astype(np.uint8)
cimg = np.zeros_like(clusters)
cv2.drawContours(cimg, list_contours, index, color=255, thickness=-1)
ellipse=cv2.fitEllipse(cnt)
cv2.ellipse(map_ellipse,ellipse,(255,0,0))
return map_ellipse
test_result, test_contours = getContours_test(test)
test_ellipse = getEllipse_test(test_result,test_contours)
mpl.contourf(test_result)
mpl.contour(test_ellipse)
mpl.show()
this first data set yields an answer:
test2=np.zeros((400,1000))
test2[240:260,110:120]=1
test2[260:265,117:134]=1
test2[265:270,134:155]=1
however when using this data I get the error at the bottom:
test=np.zeros((500,500))
test[ 30 , 219 ]=1
test[ 40 , 238 ]=1
test[ 50 , 250 ]=1
test[ 60 , 259 ]=1
test[ 70 , 266 ]=1
test[ 80 , 272 ]=1
test[ 90 , 277 ]=1
test[ 100 , 282 ]=1
test[ 110 , 285 ]=1
test[ 120 , 289 ]=1
test[ 130 , 291 ]=1
test[ 140 , 294 ]=1
test[ 150 , 296 ]=1
test[ 160 , 297 ]=1
test[ 170 , 298 ]=1
test[ 180 , 299 ]=1
test[ 190 , 300 ]=1
test[ 200 , 300 ]=1
test[ 210 , 300 ]=1
test[ 220 , 299 ]=1
test[ 230 , 298 ]=1
test[ 240 , 297 ]=1
test[ 250 , 296 ]=1
test[ 260 , 294 ]=1
test[ 270 , 291 ]=1
error message:
error: (-201:Incorrect size of input array) There should be at least 5 points to fit the ellipse in function 'cv::fitEllipseNoDirect'
why does one data set work and not the other?