Drawing a line using centroid point in opencv - python-3.x

How do I pass a line through the center of a contour? I have the center coordinates of my contour.

This is how you solve this question -
Original image -
Result image -
You first need to do basic filtering and find the contour. Then -
1) Find out the area of contour (minAreaRect)
2) Extract points from the contour (BoxPoints)
3) Convert it to a numpy array (np.array)
4) Order the points (perspective.order_points)
5) Take out Top-left, Top-right, Bottom-right and Bottom-left(tl, tr, br, bl) = box (Line 52)
6) Calculate the midpoints ( (point1 + point2) / 2)
7) Draw the lines (line 76)
Here is the code for it
# import the necessary packages
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import imutils
import cv2
# Method to find the mid point
def midpoint(ptA, ptB):
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)
# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("test.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
# perform edge detection, then perform a dilation + erosion to
# close gaps in between object edges
edged = cv2.Canny(gray, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)
# find contours in the edge map
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
# loop over the contours individually
for c in cnts:
# This is to ignore that small hair countour which is not big enough
if cv2.contourArea(c) < 1000:
continue
# compute the rotated bounding box of the contour
box = cv2.minAreaRect(c)
box = cv2.cv.BoxPoints(box) if imutils.is_cv2() else cv2.boxPoints(box)
box = np.array(box, dtype="int")
# order the points in the contour such that they appear
# in top-left, top-right, bottom-right, and bottom-left
# order, then draw the outline of the rotated bounding
# box
box = perspective.order_points(box)
# draw the contours on the image
orig = image.copy()
cv2.drawContours(orig, [box.astype("int")], -1, (0, 255, 0), 3)
# unpack the ordered bounding box, then compute the midpoint
# between the top-left and top-right coordinates, followed by
# the midpoint between bottom-left and bottom-right coordinates
(tl, tr, br, bl) = box
(tltrX, tltrY) = midpoint(tl, tr)
(blbrX, blbrY) = midpoint(bl, br)
# compute the midpoint between the top-left and top-right points,
# followed by the midpoint between the top-righ and bottom-right
(tlblX, tlblY) = midpoint(tl, bl)
(trbrX, trbrY) = midpoint(tr, br)
# draw and write the midpoints on the image
cv2.circle(orig, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
cv2.putText(orig, "({},{})".format(tltrX, tltrY), (int(tltrX - 50), int(tltrY - 10) - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
cv2.circle(orig, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
cv2.putText(orig, "({},{})".format(blbrX, blbrY), (int(blbrX - 50), int(blbrY - 10) - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
cv2.circle(orig, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
cv2.putText(orig, "({},{})".format(tlblX, tlblY), (int(tlblX - 50), int(tlblY - 10) - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
cv2.circle(orig, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)
cv2.putText(orig, "({},{})".format(trbrX, trbrY), (int(trbrX - 50), int(trbrY - 10) - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
# draw lines between the midpoints
cv2.line(orig, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)),
(255, 0, 255), 2)
cv2.line(orig, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)),
(255, 0, 255), 2)
# compute the Euclidean distance between the midpoints
dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))
# loop over the original points
for (xA, yA) in list(box):
# draw circles corresponding to the current points and
cv2.circle(orig, (int(xA), int(yA)), 5, (0,0,255), -1)
cv2.putText(orig, "({},{})".format(xA, yA), (int(xA - 50), int(yA - 10) - 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,0,0), 2)
# show the output image, resize it as per your requirements
cv2.imshow("Image", orig)
cv2.waitKey(0)

Related

Cropping an ROI in image

I am trying to crop ROI in an image. I have the bounding box coordinates, but I don't understand how to use it to extract sub-array from the original image
Here's a snippet of my code:
`for j, box in enumerate(draw_boxes):
cv2.rectangle(orig_image,
(int(box[0]), int(box[1])),
(int(box[2]), int(box[3])),
(0, 0, 255), 2)
cv2.putText(orig_image, pred_classes[j],
(int(box[0]), int(box[1]-5)),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0),
2, lineType=cv2.LINE_AA)
print(box[0], box[1], box[2], box[3])
cropped = orig_image[int(box[0]): int(box[1]), int(box[2]): int(box[3])]
cv2.imwrite(f"../test_predictions/{image_name}.jpg", orig_image)
cv2.imshow('Cropped', cropped)
cv2.imshow('Prediction', orig_image)
cv2.waitKey()`

How to improve cone - make the bases more like a circle?

I have a code for plotting a cone with some text and arrows like axes. How to change this code to get the bases of the cone looking like circles? Is the problem in the settings of margins? Or is it necessary to define circles in another way?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d
from matplotlib.patches import FancyArrowPatch
from mpl_toolkits.mplot3d import proj3d
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0, 0), (0, 0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0], ys[0]), (xs[1], ys[1]))
FancyArrowPatch.draw(self, renderer)
nn = 400 # number of points along circle's perimeter
theta = np.linspace(0, 2*np.pi, nn)
rho = np.ones(nn)
# (x,y) represents points on circle's perimeter
x = np.ravel(rho*np.cos(theta))
y = np.ravel(rho*np.sin(theta))
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [5, 5]
figsize = (5, 5)
ax = plt.axes(projection='3d') # set the axes for 3D plot
ax.azim = -88 # y rotation (default=270)
ax.elev = 13 # x rotation (default=0)
# Low, high values of z for plotting 2 circles at different elevation
loz, hiz = -15, 15
# Plot two circles
ax.plot(x, y, hiz)
ax.plot(x, y, loz)
# Set some indices to get proper (x,y) for line plotting
lo1,hi1 = 15, 15+nn//2
lo2,hi2 = lo1+nn//2-27, hi1-nn//2-27
# Plot 3d lines using coordinates of selected points
ax.plot([x[lo1], x[hi1]], [y[lo1], y[hi1]], [loz, hiz])
ax.plot([x[lo2], x[hi2]], [y[lo2], y[hi2]], [loz, hiz])
eps = 0.005
ax.plot([0, 0], [0, 0], [0, 20]) # extend in z direction
ax.plot([0-eps, 0], [0-eps, -5], [0-eps, 0]) # extend in y direction
ax.plot([0, 1.3], [0, 0], [0, 0]) # extend in x direction
ax.plot([0+eps, 0.6], [0+eps, -4], [0+eps, 16]) # v vector
ax.plot([0.63, 0.63], [-4, -4], [16, -0.005]) # vertical projection
ax.plot([0+eps, 0.6], [0+eps, -4], [0+eps, -0.005]) # to the beginning
ax.scatter(0, 0, 20, marker=(3, 0, 0), s=100, clip_on=False)
ax.scatter(0, -5, 0, marker=(3, 0, 43), s=100, clip_on=False)
ax.scatter(1.3, 0, 0, marker=(3, 0, 30), s=100, clip_on=False)
ax.scatter(0.6, -4, 16, marker=(3, 0, 80), s=100, clip_on=False)
ax.scatter(0.6, -4, -0.005, marker=(3, 0, 0), s=100, clip_on=False)
a2 = Arrow3D([0.14, -0.515], [-5.581, 1.358], [14.73, 4.983], mutation_scale=20, arrowstyle="-|>", color="k", connectionstyle="arc3,rad=0.3")
ax.add_artist(a2)
ax.text3D(0.23, -5.23, 23.33, r'$A$')
ax.text3D(1.41, 1.29, -2.7, r'$B$')
ax.text3D(-0.31, 1.46, -12.6, r'$C$')
ax.text3D(0.4, -5.48, 17, r'$D$')
ax.text3D(0.64, 1.57, -9.95, r'$E$')
ax.text3D(-0.2, -5.5, 15.73, r'$F$')
# Hide axes
ax._axis3don = False
# Save the figure (.pdf)
margins = { # vvv margin in inches
"left" : 1 / figsize[0],
"bottom" : -2.45 / figsize[1],
"right" : 1 - 0.5 / figsize[0],
"top" : 1 + 1.8 / figsize[1]
}
fig.subplots_adjust(**margins)
plt.savefig('output.pdf')
plt.show()
From this code I got the following output:
The desired output is bases looking like a circle.
This is the view from above:
It is not a circle but an ellipse.
Desired shape of the cone:

How to correctly add a light to make object get a better view with pygame and pyopengl

I am following the tutorial available in https://pythonprogramming.net/opengl-pyopengl-python-pygame-tutorial/ where he teachs how to render a cube with pyOpenGL an pygame.
When rendering the cube, the tutorial set color to all the vertices of the cubes and then dispays it. However, im my project, i load object from a .obj file using the code provided in https://www.pygame.org/wiki/OBJFileLoader and most of my objects is fully white.
Conclusion: when i render it on screen, i only see full white, which is very ugly. So i tought to use a light to better view the object, but i cannot make this work.
I know very little of pyOpenGl and i cannot find a deeper tutorial of it.
Here is part of the code and the result provided in the tutorial. (vertices, edges, surfaces and color variables are tuple of tuples)
def Cube():
glBegin(GL_QUADS)
for surface in surfaces:
x = 0
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotatef(1, 3, 1, 1)
Cube()
pygame.display.flip()
pygame.time.wait(10)
main()
i tried to edit the main function to insert a simple light, but the colors in the cube just disapeared:
def main():
pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glTranslatef(0.0,0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glRotatef(1, 3, 1, 1)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glPushMatrix()
glTranslatef(0.0,0.0, 5)
glLight(GL_LIGHT0, GL_POSITION, (0, 1, 0, 1))
glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 1.5, 1, 0))
glPopMatrix()
Cube()
glDisable(GL_LIGHT0)
glDisable(GL_LIGHTING)
pygame.display.flip()
pygame.time.wait(10)
What i want is the cube with its colors and iluminated by a light. What is wrong with my code and how to fix it?
When lighting (GL_LIGHTING) is enabled, then the color is taken from the material parameters (glMaterial).
If you still want to use the current color, the you have to enable GL_COLOR_MATERIAL
and to set the color material paramters (glColorMaterial).
The ambient light does not depend on the the direction of the light source. You've to define a diffuse and/or specular light. See glLightfv:
When the light position is set by glLightfv(GL_LIGHT0, GL_POSITION, pos), then the position is multiplied by the current model view matrix. So the light position in world space has to be set before the model transformation.
Switch to the matrix mode GL_PROJECTION, before the projection matrix is set. Otherwise the light position would be multiplied by the projection matrix.
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
#glLight(GL_LIGHT0, GL_POSITION, (0, 0, 1, 0)) # directional light from the front
glLight(GL_LIGHT0, GL_POSITION, (5, 5, 5, 1)) # point light from the left, top, front
glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 0, 0, 1))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1, 1))
glEnable(GL_DEPTH_TEST)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE )
glRotatef(1, 3, 1, 1)
Cube()
glDisable(GL_LIGHT0)
glDisable(GL_LIGHTING)
glDisable(GL_COLOR_MATERIAL)
pygame.display.flip()
The diffuse (and specular) light depends on the Normal vector of the surface.
Define an array of normal vector tuples (x, y, z). Note the following definition is an example. Since you draw a cube, which has 6 faces, you've to define 6 normal vectors, but the direction of the vectors depends on your vertex coordinates, which I do not know.
normals = [
( 0, 0, -1), # surface 0
(-1, 0, 0), # surface 1
( 0, 1, 1), # surface 2
( 1, 0, 0), # surface 3
( 0, 1, 0), # surface 4
( 0, -1, 0) # surface 5
]
And set the proper normal vector when the object is drawn:
def Cube():
glBegin(GL_QUADS)
for i_surface, surface in enumerate(surfaces):
x = 0
glNormal3fv(normals[i_surface]) # set the normal vector the vertices of the surface
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glColor3fv(colors[0])
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
Enable the Depth Test (glEnable(GL_DEPTH_TEST)) to get an animation like the following:
Full example code:
import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
verticies = (
( 1, -1, -1), # 0
( 1, 1, -1), # 1
(-1, 1, -1), # 2
(-1, -1, -1), # 3
( 1, -1, 1), # 4
( 1, 1, 1), # 5
(-1, -1, 1), # 6
(-1, 1, 1), # 7
)
surfaces = (
(0,1,2,3),
(3,2,7,6),
(6,7,5,4),
(4,5,1,0),
(1,5,7,2),
(4,0,3,6),
)
normals = [
( 0, 0, -1), # surface 0
(-1, 0, 0), # surface 1
( 0, 0, 1), # surface 2
( 1, 0, 0), # surface 3
( 0, 1, 0), # surface 4
( 0, -1, 0) # surface 5
]
colors = (
(1,1,1),
(0,1,0),
(0,0,1),
(0,1,0),
(0,0,1),
(1,0,1),
(0,1,0),
(1,0,1),
(0,1,0),
(0,0,1),
)
edges = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7),
)
def Cube():
glBegin(GL_QUADS)
for i_surface, surface in enumerate(surfaces):
x = 0
glNormal3fv(normals[i_surface])
for vertex in surface:
x+=1
glColor3fv(colors[x])
glVertex3fv(verticies[vertex])
glEnd()
glColor3fv(colors[0])
glBegin(GL_LINES)
for edge in edges:
for vertex in edge:
glVertex3fv(verticies[vertex])
glEnd()
def main():
global surfaces
pygame.init()
display = (800, 600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
clock = pygame.time.Clock()
glMatrixMode(GL_PROJECTION)
gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)
glTranslatef(0, 0, -5)
#glLight(GL_LIGHT0, GL_POSITION, (0, 0, 1, 0)) # directional light from the front
glLight(GL_LIGHT0, GL_POSITION, (5, 5, 5, 1)) # point light from the left, top, front
glLightfv(GL_LIGHT0, GL_AMBIENT, (0, 0, 0, 1))
glLightfv(GL_LIGHT0, GL_DIFFUSE, (1, 1, 1, 1))
glEnable(GL_DEPTH_TEST)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE )
glRotatef(1, 3, 1, 1)
Cube()
glDisable(GL_LIGHT0)
glDisable(GL_LIGHTING)
glDisable(GL_COLOR_MATERIAL)
pygame.display.flip()
clock.tick(60)
main()

Find local maxima points from contours OPENCV PYTHON

I already have the code to find the contours of hand and to find the extreme points of the of the contour.
Now I want to find the fingertips of the hand for that the only approach comes to my mind is the local maxima technique but I don't know how to find these.
# import the necessary packages
import imutils
import cv2
# load the image, convert it to grayscale, and blur it slightly
image = cv2.imread("hand.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# threshold the image, then perform a series of erosions +
# dilations to remove any small regions of noise
thresh = cv2.threshold(gray, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.erode(thresh, None, iterations=2)
thresh = cv2.dilate(thresh, None, iterations=2)
# find contours in thresholded image, then grab the largest
# one
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
# determine the most extreme points along the contour
extLeft = tuple(c[c[:, :, 0].argmin()][0])
extRight = tuple(c[c[:, :, 0].argmax()][0])
extTop = tuple(c[c[:, :, 1].argmin()][0])
extBot = tuple(c[c[:, :, 1].argmax()][0])
# draw the outline of the object, then draw each of the
# extreme points, where the left-most is red, right-most
# is green, top-most is blue, and bottom-most is teal
cv2.drawContours(image, [c], -1, (0, 255, 255), 2)
cv2.circle(image, extLeft, 8, (0, 0, 255), -1)
cv2.circle(image, extRight, 8, (0, 255, 0), -1)
cv2.circle(image, extTop, 8, (255, 0, 0), -1)
cv2.circle(image, extBot, 8, (255, 255, 0), -1)
# show the output image
cv2.imshow("Image", image)
cv2.waitKey(0)
Example image:

Can someone explain the different parameters in matplotlib.patches.arc?

I just want to understand the basic parameters and what they do specifically - width, height, angle, theta1, theta 2. I followed the official documentation and I understood what the centre is, but I don't get what the theta 1 or 2 does, or the angle does or what the length of the horizontal or vertical axis means.
I tried experimenting with the parameters using different numbers but failed to hit upon an accurate result.
I'm trying to create the arc of the 3-point area in the basketball court
The Arc type is a subclass of Ellipse, extended to add two values theta1 and theta2. The behaviour of angle is the same for both Ellipse and Arc and determines the angle at which the ellipse is drawn.
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
fig = plt.figure(figsize=(2,5))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 50)
ax.set_xlim(0, 20)
ax.axis('off')
a = Ellipse((10, 45), 10, 3, 0, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 40), 10, 3, 10, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 35), 10, 3, 20, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 30), 10, 3, 30, color='red', lw=1)
ax.add_patch(a)
for a in range(0, 360, 40):
a = Ellipse((10, 20), 10, 3, a, color='red', lw=1, fc='none')
ax.add_patch(a)
This produces —
Note that for a perfect circle (an ellipse of equal height and width) this makes no difference (as a circle is rotationally symmetrical).
from matplotlib import pyplot as plt
from matplotlib.patches import Ellipse
fig = plt.figure(figsize=(2,4))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 40)
ax.set_xlim(0, 20)
ax.axis('off')
a = Ellipse((10, 25), 10, 10, 0, color='red', lw=1)
ax.add_patch(a)
a = Ellipse((10, 10), 10, 10, 45, color='red', lw=1)
ax.add_patch(a)
Both circles are the same.
The Arc documentation for the matplotlib.patches.Arc explains that theta 1 & 2 are —
theta1, theta2 : float, optional
Starting and ending angles of the arc in degrees. These values are relative to angle, .e.g. if angle = 45 and theta1 = 90 the absolute starting angle is 135. Default theta1 = 0, theta2 = 360, i.e. a complete ellipse.
The key statement there is "Default theta1 = 0, theta2 = 360, i.e. a complete ellipse." — these parameters are used to draw partial ellipses, to create an arc. theta1 is the angle (or position on) the ellipse at which to start drawing, and theta2 is when to stop. Note that the calculation of the ellipse is unaffected.
The following code draws a series of arcs which should make the logic apparent —
from matplotlib import pyplot as plt
from matplotlib.patches import Arc
fig = plt.figure(figsize=(2,5))
ax = fig.add_subplot(1,1,1)
ax.set_ylim(0, 50)
ax.set_xlim(0, 20)
ax.axis('off')
# A complete ellipse, using theta1=0, theta2=360.
a = Arc((10, 45), 10, 3, 0, 0, 360, color='red', lw=1)
ax.add_patch(a)
# Reduce theta2 to 350, last 10 deg of ellipse not drawn.
a = Arc((10, 40), 10, 3, 0, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Rotate the ellipse (angle=90), theta1 & theta2 are relative to start angle & rotate too.
a = Arc((10, 30), 10, 3, 90, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Rotate the ellipse (angle=180), as above.
a = Arc((10, 20), 10, 3, 180, 0, 350, color='red', lw=1)
ax.add_patch(a)
# Draw the top half of the ellipse (theta 0-180 deg).
a = Arc((10, 10), 10, 3, 0, 0, 180, color='red', lw=1)
ax.add_patch(a)
# Draw the bottom half of the ellipse (theta 180-360 deg).
a = Arc((10, 5), 10, 3, 0, 180, 360, color='red', lw=1)
ax.add_patch(a)
This produces the following image, with arcs drawn above going from top to bottom. Compare with the comments in the code for explanation.

Resources