我有一个image,我已使用cv2
提取了轮廓并计算了它们的面积。
image = cv2.imread("shapes_and_colors.jpg")
"""Find contours"""
gray = cv2.cvtColor(shapes.copy(), cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.threshold(src=blur, thresh=60, maxval=255, type=cv2.THRESH_BINARY)[1]
new_image, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
"""Plot image and contours"""
fig, ax = plt.subplots(ncols=2)
ax[0].imshow(image)
ax[0].axis('off')
canvas = np.zeros_like(image)
for i, c in enumerate(contours):
M = cv2.moments(c)
if M["m00"] != 0:
cX = int((M["m10"] / M["m00"]))
cY = int((M["m01"] / M["m00"]))
else:
cX,cY = 0,0
cv2.drawContours(canvas, [c], -1, (255, 0, 255), 2)
ax[1].text(x=cX, y=cY, s=u"{}".format(cv2.contourArea(c)), color="cyan", size=8)
ax[1].imshow(canvas)
ax[1].axis('off')
plt.tight_layout()
现在,我想使用轮廓作为标记来绘制它们;像
fig, ax = plt.subplots()
"""Convert contour to marker"""
def contour2marker(contour):
...
return marker
for i,c in enumerate(sorted(contours, key=cv2.contourArea)):
ax.scatter(x=i, y=cv2.contourArea(c), marker=contour2marker(c))
plt.tight_layout()
我不知道从轮廓转换为标记的起点。我知道等高线被保存为点的集合,并且看着this post,将它们裁剪出图像并不容易。而是创建蒙版或从图像中裁剪出矩形。但是,如果形状不符合规则的多边形,则此技术无效。如果可以将轮廓转换为图像,则可以像在this example中一样轻松地绘制轮廓。
我有一幅图像,其中我已经使用cv2提取了轮廓并计算了它们的面积。 image = cv2.imread(“ shapes_and_colors.jpg”)“”“查找轮廓”“”灰色= cv2.cvtColor(shapes.copy(),cv2 ....