python - 如何使用 fillConvexPoly 从所有轮廓制作遮罩
问题描述
我正在尝试从轮廓制作面具。
这是我的全df
这是前 7 行的一瞥
>>> df
contour.ID xrT yrT xlT ylT
1057 20 6259.2300 4620.7845 5670.1260 4651.4670
1058 20 6253.0935 4620.7845 5682.3990 4651.4670
1059 20 6253.0935 4633.0575 5694.6720 4657.6035
1060 20 6240.8205 4633.0575 5694.6720 4657.6035
1061 20 6228.5475 4645.3305 5700.8085 4669.8765
1062 20 6228.5475 4645.3305 5700.8085 4669.8765
1063 20 6216.2745 4645.3305 5713.0815 4669.8765
我可以使用函数绘制我关心的所有轮廓。
def display_all_contours(img, df, grouping_var):
# display with matplotlib
# Create figure and axes
fig, ax = plt.subplots(1)
# Display the image
ax.imshow(img)
# split by contour
grouped_frame = df.groupby(grouping_var)
li = [grouped_frame.get_group(x) for x in grouped_frame.groups]
# for every contour
for i in range(len(li)):
poly = patches.Polygon(np.array([li[i].xrT, li[i].yrT]).T,
fill=False)
ax.add_patch(poly)
for i in range(len(li)):
poly = patches.Polygon(np.array([li[i].xlT, li[i].ylT]).T,
fill=False, color="white")
ax.add_patch(poly)
return("Displaying " + str(len(np.unique(df[grouping_var]))) + " contours.")
这是在具有我的图像形状的东西上绘制圆弧的结果。
mask = np.zeros((9373, 12273), dtype=np.uint8)
display_all_contours(mask, df, "contour.ID")
问题
现在,我想创建所有多边形的蒙版(在本例中为左侧)。所以我创建了一个蒙版并使用每个多边形烧入它cv2.fillConvexPoly
mask = np.zeros((9373, 12273), dtype=np.uint8)
display_all_contours(mask, df, "contour.ID")
for poly in np.unique(df["contour.ID"]):
# subset
sub_df = df[df["contour.ID"] == poly]
# burn into the mask
# explicitly burn into the mask
mask = cv2.fillConvexPoly(mask, np.array(sub_df[["xlT", "ylT"]], 'int32'), 1)
出于某种我不明白的原因,这不会产生我想要的结果。
plt.imshow(mask)
解决方案
解决了,我真正要找的功能是fillPoly
更换此行即可解决问题
# mind the np.array(..., "int32") is wrapped in [] because that's how fillPoly likes it
mask = cv2.fillPoly(mask, [np.array(sub_df[["xlT", "ylT"]], 'int32')], 1)
推荐阅读
- python - 从整个 dask 数据帧中更快地检索记录
- reactjs - 重新定位材料自动完成弹出图标
- ios - 当总内容宽度小于屏幕宽度时,SwiftUI如何在动态水平滚动视图中居中内容
- django - 如果我的 JSON 字段值的最大值为 0,我如何排除条目?
- python - 如何根据包含要更改的行信息的数组替换 df 的特定数据
- allennlp - 如何为 allennlp coref 模型指定 -1 标签索引
- rust - 是否需要在单元测试结束时删除指针?
- dolphindb - 如何从一个函数中选择多个输出?
- ios - UIView.animate() 调用开始时 isUserInteraction 设置不生效
- php - htaccess 单个域的多个重写规则