我正在做一个需要模糊图像背景的项目,我使用了pixellib,mask r-cnn和deeplabv3,但结果并不令人满意,我认为主要问题在于模型无法进行图像分割为了有效地分离背景和前景对象,谁能告诉我如何提高现有模型(如我提到的模型)的效率,而无需训练一个或任何其他替代线索,这些线索可以为上述任务显示更好的结果,我已附上我的实际和预期结果。
def run(self, image):
"""Runs inference on a single image.
Args:
image: A PIL.Image object, raw input image.
Returns:
resized_image: RGB image resized from original input image.
seg_map: Segmentation map of resized_image.
"""
width, height = image.size
resize_ratio = 1.0 * self.INPUT_SIZE / max(width, height)
target_size = (int(resize_ratio * width), int(resize_ratio * height))
resized_image = image.convert('RGB').resize(target_size, Image.ANTIALIAS)
batch_seg_map = self.sess.run(
self.OUTPUT_TENSOR_NAME,
feed_dict={self.INPUT_TENSOR_NAME: [np.asarray(resized_image)]})
seg_map = batch_seg_map[0]
return resized_image, seg_map
假设
IMAGE_NAME
已提前定义。
original_im = Image.open(IMAGE_NAME)
resized_im, seg_map = MODEL.run(original_im)
您可以在下面找到我的方法。它的工作方式是获取原始图像(存储在“image.png”中),从背景中提取前景(将此结果保存在“output.png”中),然后模糊原始图像并将提取的前景叠加在最上面(将最终结果保存在“sick_image.png”中):
from rembg import remove
from PIL import Image, ImageFilter
# Store paths
input_path = 'image.png'
output_path = 'output.png'
sick_image_path = 'sick_image.png'
# Open the input image
input_image = Image.open(input_path)
# Removing the background from the input image
output_image = remove(input_image)
# Save the extracted car
output_image.save(output_path)
# Blur the original image
blurred_image = input_image.filter(ImageFilter.GaussianBlur(10))
# Overlay the extracted car onto the blurred image
blurred_image.paste(output_image, (0, 0), output_image)
# Save the resulting image
blurred_image.save(sick_image_path)
我得到的结果如下:
请注意,边框仍然与您链接的参考图像不同。但是,我的方法解决了您关于轮廓的主要问题。更精细的图像处理很可能必须使用更复杂的软件和/或其他机器学习模型来完成。下面是一些与您类似问题的链接(如果您想查看其他答案,以对比我的答案):
以及一些可能对完善背景模糊方法感兴趣的链接:
祝你好运!愿代码与您同在...