如何使用 scikit 图像在 python 中的图像上裁剪一个圆的扇区

how can I crop a sector of a circle on an image in python using scikit image

我正在为 python 3.5 使用 scikit 图像。我想裁剪一个圆的扇形并将其另存为不同的图像。

我只有圆心(cx,cy),半径r,圆周长上的2个坐标(x1,y1),(x2,y2),以及a的2条直线的方程部门。

如果它是整个圆,我可以使用圆方程并将图像的其余部分涂黑,但由于它是一个扇形,我遇到了问题。

利用skimage.draw中的绘图函数,可以构造一个圆和一个多边形,并将两者相交得到一个切片:

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()