Untitled
unknown
python
a month ago
918 B
7
Indexable
Never
def apply_mask_with_label(img, polygons, label): try: # 创建一个和原图相同大小的掩码 overlay = img.copy() alpha = 0.5 # 半透明度 # 获取多边形的颜色 color = label_colors.get(label, (255, 255, 255)) # 默认白色 # 画多边形并填充颜色 pts = np.array(polygons, dtype=np.int32) cv2.fillPoly(overlay, [pts], color) # 将overlay与原图按alpha混合 img_new = cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0) # 在多边形旁边加上label文字 text_position = (pts[0][0], pts[0][1] - 10) # 文字位置在多边形的第一个顶点的上方 cv2.putText(img_new, label, text_position, cv2.FONT_HERSHEY_SIMPLEX, 1, color, 2) return img_new except Exception as e: print(f"Error while applying mask for label '{label}': {str(e)}") return img
Leave a Comment