So I am working on a project where I need to find the angle of atleast 90% of the cells. This is how a sample image looks like:
Theoretically, the cells are supposed to be elliptical shaped so I want only the contours that can be fit into an elliptical shape without much error. I tried to work on it and wrote a pretty simple code but my problem is that the contours I am getting are not elliptical shaped. How can I fix that?
This is my code:
import cv2
import numpy as np
# Load the image
image = cv2.imread("/Users/yahya2/Desktop/1.png")
cv2.waitKey(0)
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Find Canny edges
edged = cv2.Canny(gray, 30, 200)
cv2.waitKey(0)
# Find contours
contours, hierarchy = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
cv2.imshow('Canny Edges After Contouring', edged)
cv2.waitKey(0)
print("Number of Contours found = " + str(len(contours)))
# Loop through contours and draw only those that can be approximated as ellipses
for cnt in contours:
# Fit an ellipse to the contour
ellipse = cv2.fitEllipse(cnt)
# Check if the fitted ellipse has a non-zero area
if ellipse[1][0] * ellipse[1][1] > 0:
# Draw the ellipse on the image
cv2.ellipse(image, ellipse, (0, 255, 0), 2)
# Show the image with ellipse contours
cv2.imshow('Ellipse Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
This is the result I am getting: