Я переработаю свою идею из этого ответа , включающего функцию arctan2
из numpy
.
Это входное изображение, подобное этому:
![Input circle](https://i.stack.imgur.com/F0eW2.png)
На выходе будет такой график:
![Plot circle](https://i.stack.imgur.com/dgYsX.png)
Вот код, который, как мы надеемся, самоочевиден:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Generate artificial image
img = np.zeros((400, 400), np.uint8)
center = (150, 150)
img = cv2.circle(img, center, 100, 255, 1)
cv2.imwrite('images/img.png', img)
# Find contour(s)
cnts, _ = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Center contour
cnt = np.squeeze(cnts[0]) - center
# Calculate atan2 values, and sort
val = np.sort(np.arctan2(cnt[:, 0], cnt[:, 1]))
idx = np.argsort(np.arctan2(cnt[:, 0], cnt[:, 1]))
# atan2 uses (1, 0) as starting point, so correct by 1/2 * pi
corr = np.where(val <= (-0.5 * np.pi))[0][-1]
# Build final indices
indFinal = np.concatenate((idx[corr - 1:], idx[0:corr]))
x = cnt[indFinal, 0]
y = cnt[indFinal, 1]
# Generate plot
ax = plt.subplot(121)
plt.plot(x)
plt.title('x')
ax = plt.subplot(122)
plt.plot(y)
plt.title('y')
plt.savefig('images/plot.png')
Предупреждение: вогнутые контуры могут привести к искажению результатов.