You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何借助Vertex AI与遮罩正确生成背景图像

问题解决方案:保留运动鞋仅生成新背景

核心问题:遮罩逻辑错误

  • Vertex AI edit_image接口的遮罩规则是:白色区域为模型允许修改的区域,黑色区域为完全保留的原始内容。
  • 你需要保留运动鞋、修改背景,因此必须使用反转遮罩(示例6)——即遮罩中白色对应背景区域,黑色对应运动鞋区域。之前使用标准遮罩(示例3)会让模型修改运动鞋(白色区域),导致示例1中运动鞋被错误编辑的问题。
  • 补充:你的PNG原图背景显示黑色是正常现象(透明通道在普通查看器中默认显示黑色),只要原图确实带有透明通道,就不影响模型处理。

参数调整建议

  • 降低guidance_scale至中等强度区间(10-20):当前设置的21为高强度,容易让模型过度遵循提示词,甚至侵蚀到遮罩的黑色保留区域(如示例4的混合异常)。建议调整为15左右,平衡背景生成效果与原始内容的保留。
  • 微调seed值:不同的seed会生成不同的背景细节,若当前背景效果不符合预期,更换seed值(如5、10)可获得更多备选结果。

修正后的代码示例

from vertexai.preview.vision_models import Image, ImageGenerationModel

project_id = "x"
input_file = "trainers_nobg_color.png"
# 替换为你的反转遮罩文件路径
mask_file = "trainers_mask_inverted.png"
output_file = "trainers_edited.png"
prompt = "A sandy beach background."

vertexai.init(project=project_id, location="europe-west2")

model = ImageGenerationModel.from_pretrained("imagegeneration@002")
base_img = Image.load_from_file(input_file)
mask_img = Image.load_from_file(mask_file)

images = model.edit_image(
    base_image=base_img,
    mask=mask_img,
    prompt=prompt,
    seed=5,
    # 调整为中等强度的引导值
    guidance_scale=15,
    number_of_images=1,
)

images[0].save(location=output_file, include_generation_parameters=False)
images[0].show()

内容的提问来源于stack exchange,提问作者Ben P

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 12:33:29