kkk2026 commited on
Commit
2f44738
·
verified ·
1 Parent(s): 4ba56d8

Delete USIS16K

Browse files
USIS16K/WaterOVS_TF/proj.py DELETED
@@ -1,25 +0,0 @@
1
- import cv2
2
- import numpy as np
3
- import glob
4
- import os
5
-
6
- input_dir = "./masks"
7
- output_dir = "./masks_proj"
8
- os.makedirs(output_dir, exist_ok=True)
9
-
10
- for path in glob.glob(os.path.join(input_dir, "*.png")):
11
- mask = cv2.imread(path, cv2.IMREAD_UNCHANGED) # 保持标签值
12
- new_mask = np.zeros_like(mask, dtype=np.uint8)
13
-
14
- # 背景 0 -> 255
15
- new_mask[mask == 0] = 255
16
-
17
- # 1 ~ 158 -> 0 ~ 157
18
- foreground = (mask >= 1) & (mask <= 158)
19
- new_mask[foreground] = mask[foreground] - 1
20
-
21
- # 保存
22
- save_path = os.path.join(output_dir, os.path.basename(path))
23
- cv2.imwrite(save_path, new_mask)
24
-
25
- print("✅ ", output_dir)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
USIS16K/WaterOVS_TF/resize_waterovs.py DELETED
@@ -1,53 +0,0 @@
1
- import cv2
2
- import os
3
-
4
-
5
- def resize_image_and_mask(image_path, mask_path, out_image_path, out_mask_path, size=(800, 600)):
6
- image = cv2.imread(image_path)
7
- if image is None:
8
- print(f"Failed to load image: {image_path}")
9
- return
10
- mask = cv2.imread(mask_path, cv2.IMREAD_UNCHANGED)
11
- if mask is None:
12
- print(f"Failed to load mask: {mask_path}")
13
- return
14
-
15
- resized_image = cv2.resize(image, size, interpolation=cv2.INTER_LINEAR)
16
- # 缩放mask,用最近邻插值(防止标签混合)
17
- resized_mask = cv2.resize(mask, size, interpolation=cv2.INTER_NEAREST)
18
-
19
- # 将图像保存为JPG格式(修改文件扩展名并调整保存参数)
20
- cv2.imwrite(out_image_path, resized_image, [int(cv2.IMWRITE_JPEG_QUALITY), 95])
21
- # 掩码保持原格式保存
22
- cv2.imwrite(out_mask_path, resized_mask)
23
-
24
- print(f"Resized and saved image: {out_image_path}")
25
- print(f"Resized and saved mask: {out_mask_path}")
26
-
27
-
28
- def batch_resize(image_dir, mask_dir, out_image_dir, out_mask_dir, size=(800, 600)):
29
- os.makedirs(out_image_dir, exist_ok=True)
30
- os.makedirs(out_mask_dir, exist_ok=True)
31
-
32
- image_files = sorted(os.listdir(image_dir))
33
- mask_files = sorted(os.listdir(mask_dir))
34
-
35
- for img_file, mask_file in zip(image_files, mask_files):
36
- img_path = os.path.join(image_dir, img_file)
37
- mask_path = os.path.join(mask_dir, mask_file)
38
-
39
- # 处理图像文件名,将扩展名改为.jpg
40
- img_name = os.path.splitext(img_file)[0] + ".jpg"
41
- out_img_path = os.path.join(out_image_dir, img_name)
42
- out_mask_path = os.path.join(out_mask_dir, mask_file)
43
-
44
- resize_image_and_mask(img_path, mask_path, out_img_path, out_mask_path, size)
45
-
46
-
47
- if __name__ == "__main__":
48
- image_dir = "./WaterOVSTF/images"
49
- mask_dir = "./WaterOVSTF/masks"
50
- out_image_dir = "./WaterOVSTF/images_o"
51
- out_mask_dir = "./WaterOVSTF/masks_o"
52
- target_size = (800, 600) # width x height
53
- batch_resize(image_dir, mask_dir, out_image_dir, out_mask_dir, target_size)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
USIS16K/WaterOVS_TF/vis_semantic.py DELETED
@@ -1,64 +0,0 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- from glob import glob
6
-
7
- # 自动生成 200 个类别的颜色(ID -> RGB)
8
- def generate_id2color(num_classes=200):
9
- id2color = {}
10
- for i in range(num_classes + 1):
11
- hue = int(255 * i / (num_classes + 1))
12
- color = np.array(cv2.cvtColor(np.uint8([[[hue, 255, 255]]]), cv2.COLOR_HSV2BGR))[0][0]
13
- id2color[i] = color.tolist()
14
- return id2color
15
-
16
- ID2COLOR = generate_id2color(158)
17
-
18
- def decode_semantic_mask(mask):
19
- """把单通道 mask 映射为彩色 RGB mask"""
20
- h, w = mask.shape
21
- color_mask = np.zeros((h, w, 3), dtype=np.uint8)
22
- for id, color in ID2COLOR.items():
23
- color_mask[mask == id] = color
24
- return color_mask
25
-
26
- def visualize_semantic(images_dir, masks_dir, output_dir=None, blend_alpha=0.6):
27
- os.makedirs(output_dir, exist_ok=True) if output_dir else None
28
-
29
- mask_paths = glob(os.path.join(masks_dir, "*.png"))
30
- for mask_path in mask_paths:
31
- filename = os.path.basename(mask_path).replace(".png", ".jpg")
32
- image_path = os.path.join(images_dir, filename)
33
-
34
- if not os.path.exists(image_path):
35
- print(f"Missing image: {image_path}")
36
- continue
37
-
38
- image = cv2.imread(image_path)
39
- image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
40
-
41
- mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
42
- color_mask = decode_semantic_mask(mask)
43
-
44
- # 叠加原图与mask(0.6原图 + 0.4mask)
45
- vis = cv2.addWeighted(image, blend_alpha, color_mask, 1 - blend_alpha, 0)
46
-
47
- plt.imshow(vis)
48
- plt.title(filename)
49
- plt.axis("off")
50
- plt.tight_layout()
51
- if output_dir:
52
- save_path = os.path.join(output_dir, filename.replace(".png", "_vis.png"))
53
- plt.imsave(save_path, vis)
54
- print(f"Saved: {save_path}")
55
- else:
56
- plt.show()
57
-
58
- if __name__ == "__main__":
59
- visualize_semantic(
60
- images_dir="images",
61
- masks_dir="masks",
62
- output_dir="masks_vis",
63
- blend_alpha=0.6 # 控制原图比例
64
- )