当前位置:   article > 正文

Mask RCNN保存分割后的图片_maskrcnn将分割的图片保存

maskrcnn将分割的图片保存

文章的maskrcnn 基于参考文章

在utils文件夹下的visualize.py,改动主要在第60行左右。根据bbox坐标对原图进行切片,然后将RGB改成BGR,最后保存在save_dir文件夹中。

  1. import os
  2. import sys
  3. import random
  4. import itertools
  5. import colorsys
  6. import numpy as np
  7. from skimage.measure import find_contours
  8. from PIL import Image
  9. import cv2
  10. ROOT_DIR = os.path.abspath("../")
  11. sys.path.append(ROOT_DIR)
  12. save_dir = "你的目标路径"
  13. # ---------------------------------------------------------#
  14. # Visualization
  15. # ---------------------------------------------------------#
  16. def random_colors(N, bright=True):
  17. """
  18. 生成随机颜色
  19. """
  20. brightness = 1.0 if bright else 0.7
  21. hsv = [(i / N, 1, brightness) for i in range(N)]
  22. colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
  23. return colors
  24. def apply_mask(image, mask, color, alpha=0.5):
  25. """
  26. 打上mask图标
  27. """
  28. for c in range(3):
  29. image[:, :, c] = np.where(mask == 1,
  30. image[:, :, c] *
  31. (1 - alpha) + alpha * color[c] * 255,
  32. image[:, :, c])
  33. return image
  34. def display_instances(image, boxes, masks, class_ids, class_names,
  35. scores=None, title="",
  36. figsize=(16, 16),
  37. show_mask=True, show_bbox=True,
  38. colors=None, captions=None):
  39. # instance的数量
  40. N = boxes.shape[0]
  41. if not N:
  42. print("\n*** No instances to display *** \n")
  43. else:
  44. assert boxes.shape[0] == masks.shape[-1] == class_ids.shape[0]
  45. colors = colors or random_colors(N)
  46. # 当masked_image为原图时是在原图上绘制
  47. # 如果不想在原图上绘制,可以把masked_image设置成等大小的全0矩阵
  48. masked_image = np.array(image, np.uint8)
  49. for i in range(N):
  50. color = colors[i]
  51. # 该部分用于显示bbox
  52. if not np.any(boxes[i]):
  53. continue
  54. y1, x1, y2, x2 = boxes[i]
  55. if show_bbox:
  56. # 打印边界框坐标
  57. print(f"Box {i + 1}: (x1, y1, x2, y2) = ({x1}, {y1}, {x2}, {y2})")
  58. cropped_image_rgb = image[y1:y2, x1:x2]
  59. image_bgr = cv2.cvtColor(cropped_image_rgb, cv2.COLOR_RGB2BGR)
  60. save_path = os.path.join(save_dir, f"crop_{i + 1}.jpg")
  61. cv2.imwrite(save_path, image_bgr)
  62. cv2.rectangle(masked_image, (x1, y1), (x2, y2), (color[0] * 255, color[1] * 255, color[2] * 255), 2)
  63. # 该部分用于显示文字与置信度
  64. if not captions:
  65. class_id = class_ids[i]
  66. score = scores[i] if scores is not None else None
  67. label = class_names[class_id]
  68. caption = "{} {:.3f}".format(label, score) if score else label
  69. else:
  70. caption = captions[i]
  71. font = cv2.FONT_HERSHEY_SIMPLEX
  72. cv2.putText(masked_image, caption, (x1, y1 + 8), font, 1, (255, 255, 255), 2)
  73. # 该部分用于显示语义分割part
  74. mask = masks[:, :, i]
  75. if show_mask:
  76. masked_image = apply_mask(masked_image, mask, color)
  77. # 画出语义分割的范围
  78. padded_mask = np.zeros(
  79. (mask.shape[0] + 2, mask.shape[1] + 2), dtype=np.uint8)
  80. padded_mask[1:-1, 1:-1] = mask
  81. contours = find_contours(padded_mask, 0.5)
  82. for verts in contours:
  83. verts = np.fliplr(verts) - 1
  84. cv2.polylines(masked_image, [np.array([verts], np.int)], 1,
  85. (color[0] * 255, color[1] * 255, color[2] * 255), 2)
  86. img = Image.fromarray(np.uint8(masked_image))
  87. return img

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号