I am using scikit image for python 3.5. I'd like to crop a sector of a circle and save it as a different image.
All I have is center of the circle(cx,cy), radius r, 2 coordinates(x1,y1),(x2,y2) on the perimeter of the circle, and the equations of 2 lines of a sector.
If it is entire circle, i can use the circle equation and blacken the rest of the image but as it is a sector, I am facing a problem.
Using the drawing functions from skimage.draw
, you can construct a circle and a polygon and intersect the two to obtain a slice:
import numpy as np
from skimage import data, draw
import matplotlib.pyplot as plt
# Random image
image = np.random.random((200, 200))
# --- coordinate specification
r0, c0 = 100, 70 # circle center (row, column)
R = 100 # circle radius
theta0 = np.deg2rad(20) # angle #1 for arc
theta1 = np.deg2rad(40) # angle #2 for arc
# Above, I provide two angles, but you can also just give the two
# coordinates below directly
r1, c1 = r0 - 1.5 * R * np.sin(theta0), c0 + 1.5 * R * np.cos(theta0) # arc coord #1
r2, c2 = r0 - 1.5 * R * np.sin(theta1), c0 + 1.5 * R * np.cos(theta1) # arc coord #2
# --- mask calculation
mask_circle = np.zeros(image.shape[:2], dtype=bool)
mask_poly = np.zeros(image.shape[:2], dtype=bool)
rr, cc = draw.circle(r0, c0, R, shape=mask_circle.shape)
mask_circle[rr, cc] = 1
rr, cc = draw.polygon([r0, r1, r2, r0],
[c0, c1, c2, c0], shape=mask_poly.shape)
mask_poly[rr, cc] = 1
mask = mask_circle & mask_poly
plt.imshow(mask, cmap='gray')
plt.show()