File size: 1,399 Bytes
7bd850b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import cv2
from PIL import Image
import numpy as np

img_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/JPEGImages/LongVOS/train/9ErzfLGq"
anno_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/Annotations/LongVOS/train/9ErzfLGq"

frame_path_list = sorted(os.listdir(img_path))
mask_path_list = sorted(os.listdir(anno_path))

# 可选:输出目录
output_dir = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/overlay_results_9ErzfLGq"
os.makedirs(output_dir, exist_ok=True)

for frame_name, mask_name in zip(frame_path_list, mask_path_list):
    frame = cv2.imread(os.path.join(img_path, frame_name))
    mask = Image.open(os.path.join(anno_path, mask_name))
    gt_mask = np.array(mask)

    # 将类别为2的区域设为1,其他设为0
    tmp_masks = np.zeros_like(gt_mask)
    tmp_masks[gt_mask == 4] = 1
    gt_mask = tmp_masks

    # 创建红色掩码图层 (BGR)
    red_mask = np.zeros_like(frame)
    red_mask[:, :, 0] = 0  # B通道
    red_mask[:, :, 1] = 0  # G通道
    red_mask[:, :, 2] = 255  # R通道

    # 掩码区域透明叠加到原图上
    alpha = 0.3
    mask_3c = np.stack([gt_mask]*3, axis=-1)  # 扩展成3通道
    overlayed = np.where(mask_3c == 1, cv2.addWeighted(frame, 1 - alpha, red_mask, alpha, 0), frame)

    # 保存
    save_path = os.path.join(output_dir, frame_name)
    cv2.imwrite(save_path, overlayed)