上一篇文章【CV大模型SAM(Segment-Anything)】真是太强大了,分割一切的SAM大模型使用方法:可通过不同的提示得到想要的分割目标中详细介绍了大模型SAM(Segment-Anything)的不同使用方法,后面有很多小伙伴给我留言问我分割后的目标对象如何保存,这篇介绍一下分割后的mask对象的保存方法。
关于Segment-Anything模型的相关代码、论文PDF、预训练模型、使用方法等,我都已打包好,供需要的小伙伴交流研究,获取方式如下:
关注文末名片GZH:阿旭算法与机器学习,回复:【SAM】即可获取SAM相关代码、论文、预训练模型、使用方法文档等
1.导入需要的库
#coding:utf-8
import numpy as np
import torch
import matplotlib.pyplot as plt
import cv2
import os
import copy
2. 定义相关绘图函数
def show_mask(mask, ax, random_color=False):
if random_color:
color = np.concatenate([np.random.random(3), np.array([0.6])], axis=0)
else:
color = np.array([30 / 255, 144 / 255, 255 / 255, 0.6])
h, w = mask.shape[-2:]
mask_image = mask.reshape(h, w, 1) * color.reshape(1, 1, -1)
ax.imshow(mask_image)
def show_points(coords, labels, ax, marker_size=375):
pos_points = coords[labels == 1]
neg_points = coords[labels == 0]
ax.scatter(pos_points[:, 0], pos_points[:, 1], color='green', marker='*', s=marker_size, edgecolor='white',
linewidth=1.25)
ax.scatter(neg_points[:, 0], neg_points[:, 1], color='red', marker='*', s=marker_size, edgecolor='white',
linewidth=1.25)
def show_box(box, ax):
x0, y0 = box[0], box[1]
w, h = box[2] - box[0], box[3] - box[1]
ax.add_patch(plt.Rectangle((x0, y0), w, h, edgecolor='green', facecolor=(0, 0, 0, 0), lw=2))
3. 加载图片与SAM模型
image = cv2.imread('notebooks/images/truck.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
import sys
sys.path.append("..")
from segment_anything import sam_model_registry, SamPredictor
sam_checkpoint = "./models/sam_vit_b_01ec64.pth"
model_type = "vit_b"
device = "cpu"
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
sam.to(device=device)
predictor = SamPredictor(sam)
predictor.set_image(image)
4.指定单个提示点,并显示该提示点
input_point = np.array([[500, 375]])
input_label = np.array([1])
plt.figure(figsize=(10,10))
plt.imshow(image)
show_points(input_point, input_label, plt.gca())
plt.axis('on')
plt.show()
5.使用SAM模型对上面的提示点进行目标分割
masks, scores, logits = predictor.predict(
point_coords=input_point,
point_labels=input_label,
multimask_output=True,
)
for i, (mask, score) in enumerate(zip(masks, scores)):
plt.figure(figsize=(10,10))
plt.imshow(image)
show_mask(mask, plt.gca())
show_points(input_point, input_label, plt.gca())
plt.title(f"Mask {i+1}, Score: {score:.3f}", fontsize=18)
plt.axis('off')
plt.show()
6.保存分割结果的mask
由于我使用的是最小的一个SAM预训练模型:sam_vit_b_01ec64.pth,所以分割出来的结果没有那么完美。如果想得到更好的结果,可以使用较大的SAM预训练模型即可。
for i, (mask, score) in enumerate(zip(masks, scores)):
mask = mask + 255
plt.imshow(mask, cmap='gray')
plt.savefig('pic-{}.png'.format(i + 1))
plt.show()
7.提取并保存mask对应的图片区域
for i, (mask, score) in enumerate(zip(masks, scores)):
mask = ~mask
mask = mask + 255
mask = np.repeat(mask[:, :, np.newaxis], 3, axis=2)
mask = mask.astype(np.uint8)
res = cv2.bitwise_and(image, mask)
res[res == 0] = 255
plt.imshow(res)
plt.savefig('res-{}.png'.format(i + 1))
plt.show()
关于Segment-Anything模型的相关代码、论文PDF、预训练模型、使用方法等,我都已打包好,供需要的小伙伴交流研究,获取方式如下:
关注文末名片GZH:阿旭算法与机器学习,回复:【SAM】即可获取SAM相关代码、论文、预训练模型、使用方法文档等
如果文章对你有帮助,感谢点赞+关注!