Get the mean elipse from two elipses

Viewed 43

As you can see in the problem there are two elipses from the bright blue contours. I tried to get the "mean" - elipse by merging the contour arrays into one array. The result is the blue line. Thats not what i exspected. Can somebody explain me how i can archive this. I want the green line as a elipse. Thx to everyone.

Here is part of the code

image_read = cv2.imread(file)
    image_read_gray = cv2.cvtColor(image_read, cv2.COLOR_BGR2GRAY)
    image_copy = image_read_gray.copy()
    kernel = np.ones((3, 3), np.uint8)
    closing = cv2.morphologyEx(image_copy, cv2.MORPH_CLOSE, kernel)
    #cv2.imshow("blabla",closing)
    #cv2.waitKey(0)

    image_read2 = cv2.imread(path_folder_croppedoriginal + "\\image_cropped" + str(count) + ".jpg")
    contours, hierarchy = cv2.findContours(closing, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)


    sorted_contour = sorted(contours, key=cv2.contourArea, reverse=True)
    bigcontour1 = sorted_contour[0]
    bigcontour2 = sorted_contour[1]

    bigcontour_merged = np.concatenate((bigcontour1,bigcontour2))

    ellipse = cv2.fitEllipse(bigcontour1)
    ellipse2 = cv2.fitEllipse(bigcontour2)
    ellipse3 = cv2.fitEllipse(bigcontour_merged)

    image_read_BGR = cv2.cvtColor(image_read_gray, cv2.COLOR_GRAY2BGR)

    ImgWithEllipse = cv2.ellipse(image_read_BGR, ellipse, (255, 0 , 255), 4, cv2.LINE_AA)
    cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse)

    ImgWithEllipse2 = cv2.ellipse(image_read_BGR, ellipse2, (255, 0, 255), 4, cv2.LINE_AA)
    cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse2)

    ImgWithEllipse_merged = cv2.ellipse(image_read_BGR, ellipse3, (255, 0, 0), 4, cv2.LINE_AA)
    cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithEllipse_merged)

    ImgWithCounter2 = cv2.drawContours(image_read2, [bigcontour1, bigcontour2], -1, (255, 255, 0), 3)
    cv2.imwrite(os.path.join(path_folder_croppedoriginal, "image_cropped" + str(count) + ".jpg"), ImgWithCounter2)


    ImgWithCounter = cv2.drawContours(image_read_BGR, [bigcontour1,bigcontour2], -1, (255, 255, 0), 3)

    print(cv2.contourArea(bigcontour1), cv2.contourArea(bigcontour2))
    cv2.imwrite(os.path.join(path_folder_contours, "image_contours" + str(count) + ".jpg"), ImgWithCounter)
    count += 1

Maybe its bc there are more points for the outer elipse (like 1900 points to represent) then for the inner elipse(like 600)

0 Answers
Related