2

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)

GetBoosted
  • 31
  • 5
  • Could you add what code you have so far? – PirateNinjas Apr 26 '22 at 21:05
  • How are your ellipses represented? There are like a dozen possible ways to represent an ellipse... – Him Apr 26 '22 at 21:06
  • Are your ellipses just arrays, or can you get their polynomial representation? – ddejohn Apr 26 '22 at 21:14
  • 3
    [this question](https://stackoverflow.com/questions/38530899/python-fit-ellipse-to-an-image) suggests that `fitEllipse` returns `(x,y), (m, M), a` where `x,y` is the center, `m,M` are the lengths of the minor and major axis, and `a` is the angle of rotation. You should just be able to average each of these things in a "add together and divide by 2" sense, yeah? – Him Apr 26 '22 at 21:15
  • oh this could be an easy solution i will try it out. Be right back. – GetBoosted Apr 26 '22 at 21:17

0 Answers0