python-3.xopencvgetpixel

OpenCV get pixels on an ellipse


I'm trying to get the pixels of an ellipse from an image.

For example, I draw an ellipse on a random image (sample geeksforgeeks code):

import cv2
path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'
image = cv2.imread(path)
window_name = 'Image'
center_coordinates = (120, 100)
axesLength = (100, 50)
angle = 0
startAngle = 0
endAngle = 360
color = (0, 0, 255)
thickness = 5
image = cv2.ellipse(image, center_coordinates, axesLength,
           angle, startAngle, endAngle, color, thickness)
cv2.imshow(window_name, image)

It gives output like below: enter image description here

Now, I want to get the pixel value of boundary line of ellipse. If it is possible I would like to get the pixel of ellipse using cv2.ellipse() back as an array of coordinates.
Can anyone help me with this please.


Solution

  • There is no direct OpenCV way probably to get these points of the ellipse but you can extract your points via indirect way like this:

    mask = cv2.inRange(image, np.array(color), np.array(color))
    contour = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[-2][0]
    

    contour will store the outer points of your red ellipse. Here, what I have done is created a mask image of the ellipse and found the externalmost contour's points that is the required thing.