mask_depth.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import os
  2. from PIL import Image
  3. import numpy as np
  4. from .common import calculate_depth_mean_pil
  5. from tqdm import tqdm
  6. def depth_apply_mask(image_path):
  7. """
  8. 基于掩码图像对另一张图像进行掩码处理,背景区域设为黑色
  9. Args:
  10. image_path (str): 输入图像的路径
  11. mask_path (str): 掩码图像的路径
  12. output_path (str): 保存处理后图像的路径
  13. """
  14. # 解析各文件名
  15. image_name = os.path.splitext(os.path.basename(image_path))[0].split('-')[-1]
  16. mask_path = f"data/mask_image/mask-{image_name}.png"
  17. output_path = f"data/mask_depth_image/mask-depth-{image_name}.png"
  18. # 读取图像和掩码图像
  19. image = Image.open(image_path) # 假设已经是单通道
  20. mask = Image.open(mask_path) # 假设已经是单通道
  21. # 将图像和掩码转换为 NumPy 数组
  22. image_array = np.array(image)
  23. mask_array = np.array(mask)
  24. # 创建一个全黑的图像
  25. masked_image_array = np.zeros_like(image_array)
  26. # 应用掩码:保留掩码区域的像素,背景设为黑色
  27. masked_image_array[mask_array > 0] = image_array[mask_array > 0]
  28. # 将处理后的数组转换回图像
  29. masked_image = Image.fromarray(masked_image_array)
  30. # 保存处理后的图像
  31. masked_image.save(output_path)
  32. return masked_image
  33. def depth_apply_mask_pilimg(image, mask, image_path):
  34. """
  35. 基于掩码图像对另一张图像进行掩码处理,背景区域设为黑色
  36. Args:
  37. image_path (str): 输入图像的路径
  38. mask_path (str): 掩码图像的路径
  39. output_path (str): 保存处理后图像的路径
  40. """
  41. # # 解析各文件名
  42. image_name = os.path.splitext(os.path.basename(image_path))[0]
  43. # mask_path = f"data/mask_image/mask-{image_name}.png"
  44. output_path = f"data/mask_depth_image/mask-depth-{image_name}.png"
  45. # # 读取图像和掩码图像
  46. # image = Image.open(image_path) # 假设已经是单通道
  47. # mask = Image.open(mask_path) # 假设已经是单通道
  48. # 将图像和掩码转换为 NumPy 数组
  49. image_array = np.array(image)
  50. mask_array = np.array(mask)
  51. # 创建一个全黑的图像
  52. masked_image_array = np.zeros_like(image_array)
  53. # 应用掩码:保留掩码区域的像素,背景设为黑色
  54. masked_image_array[mask_array > 0] = image_array[mask_array > 0]
  55. # 将处理后的数组转换回图像
  56. masked_image = Image.fromarray(masked_image_array)
  57. # 保存处理后的图像
  58. masked_image.save(output_path)
  59. return masked_image
  60. def depth_apply_mask_pilimg_list(images, masks, output_paths):
  61. """
  62. 对图像列表和掩码列表应用掩码处理,并保存处理后的图像
  63. Args:
  64. images (list): 输入图像列表
  65. masks (list): 掩码图像列表
  66. output_paths (list): 保存处理后图像的路径列表
  67. """
  68. masked_depths = []
  69. depth_means = []
  70. for image, mask, output_path in tqdm(zip(images, masks, output_paths)):
  71. masked_image = depth_apply_mask_pilimg(image, mask, output_path)
  72. depth_mean = calculate_depth_mean_pil(masked_image)
  73. masked_depths.append(masked_image)
  74. depth_means.append(depth_mean)
  75. return masked_depths, depth_means
  76. # 使用示例
  77. if __name__ == "__main__":
  78. input_image_path = "depth_image.png" # 输入图像路径
  79. mask_image_path = "rmbgmask_image.png" # 掩码图像路径
  80. output_image_path = "masked_output_image11.png"
  81. apply_mask(input_image_path)