Skip to content

Commit 6acd77e

Browse files
authored
Add opacity option to show_result (#425)
1 parent b818946 commit 6acd77e

File tree

6 files changed

+44
-11
lines changed

6 files changed

+44
-11
lines changed

demo/image_demo.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,24 @@ def main():
1515
'--palette',
1616
default='cityscapes',
1717
help='Color palette used for segmentation map')
18+
parser.add_argument(
19+
'--opacity',
20+
type=float,
21+
default=0.5,
22+
help='Opacity of painted segmentation map. In (0, 1] range.')
1823
args = parser.parse_args()
1924

2025
# build the model from a config file and a checkpoint file
2126
model = init_segmentor(args.config, args.checkpoint, device=args.device)
2227
# test a single image
2328
result = inference_segmentor(model, args.img)
2429
# show the results
25-
show_result_pyplot(model, args.img, result, get_palette(args.palette))
30+
show_result_pyplot(
31+
model,
32+
args.img,
33+
result,
34+
get_palette(args.palette),
35+
opacity=args.opacity)
2636

2737

2838
if __name__ == '__main__':

docs/get_started.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ result = inference_segmentor(model, img)
166166
# visualize the results in a new window
167167
model.show_result(img, result, show=True)
168168
# or save the visualization results to image files
169-
model.show_result(img, result, out_file='result.jpg')
169+
# you can change the opacity of the painted segmentation map in (0, 1].
170+
model.show_result(img, result, out_file='result.jpg', opacity=0.5)
170171

171172
# test a video and show the results
172173
video = mmcv.VideoReader('video.mp4')

mmseg/apis/inference.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@ def inference_segmentor(model, img):
9898
return result
9999

100100

101-
def show_result_pyplot(model, img, result, palette=None, fig_size=(15, 10)):
101+
def show_result_pyplot(model,
102+
img,
103+
result,
104+
palette=None,
105+
fig_size=(15, 10),
106+
opacity=0.5):
102107
"""Visualize the segmentation results on the image.
103108
104109
Args:
@@ -109,10 +114,14 @@ def show_result_pyplot(model, img, result, palette=None, fig_size=(15, 10)):
109114
map. If None is given, random palette will be generated.
110115
Default: None
111116
fig_size (tuple): Figure size of the pyplot figure.
117+
opacity(float): Opacity of painted segmentation map.
118+
Default 0.5.
119+
Must be in (0, 1] range.
112120
"""
113121
if hasattr(model, 'module'):
114122
model = model.module
115-
img = model.show_result(img, result, palette=palette, show=False)
123+
img = model.show_result(
124+
img, result, palette=palette, show=False, opacity=opacity)
116125
plt.figure(figsize=fig_size)
117126
plt.imshow(mmcv.bgr2rgb(img))
118127
plt.show()

mmseg/apis/test.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def single_gpu_test(model,
3535
data_loader,
3636
show=False,
3737
out_dir=None,
38-
efficient_test=False):
38+
efficient_test=False,
39+
opacity=0.5):
3940
"""Test with single GPU.
4041
4142
Args:
@@ -46,7 +47,9 @@ def single_gpu_test(model,
4647
the directory to save output results.
4748
efficient_test (bool): Whether save the results as local numpy files to
4849
save CPU memory during evaluation. Default: False.
49-
50+
opacity(float): Opacity of painted segmentation map.
51+
Default 0.5.
52+
Must be in (0, 1] range.
5053
Returns:
5154
list: The prediction results.
5255
"""
@@ -82,7 +85,8 @@ def single_gpu_test(model,
8285
result,
8386
palette=dataset.PALETTE,
8487
show=show,
85-
out_file=out_file)
88+
out_file=out_file,
89+
opacity=opacity)
8690

8791
if isinstance(result, list):
8892
if efficient_test:

mmseg/models/segmentors/base.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ def show_result(self,
212212
win_name='',
213213
show=False,
214214
wait_time=0,
215-
out_file=None):
215+
out_file=None,
216+
opacity=0.5):
216217
"""Draw `result` over `img`.
217218
218219
Args:
@@ -229,7 +230,9 @@ def show_result(self,
229230
Default: False.
230231
out_file (str or None): The filename to write the image.
231232
Default: None.
232-
233+
opacity(float): Opacity of painted segmentation map.
234+
Default 0.5.
235+
Must be in (0, 1] range.
233236
Returns:
234237
img (Tensor): Only if not `show` or `out_file`
235238
"""
@@ -246,13 +249,14 @@ def show_result(self,
246249
assert palette.shape[0] == len(self.CLASSES)
247250
assert palette.shape[1] == 3
248251
assert len(palette.shape) == 2
252+
assert 0 < opacity <= 1.0
249253
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
250254
for label, color in enumerate(palette):
251255
color_seg[seg == label, :] = color
252256
# convert to BGR
253257
color_seg = color_seg[..., ::-1]
254258

255-
img = img * 0.5 + color_seg * 0.5
259+
img = img * (1 - opacity) + color_seg * opacity
256260
img = img.astype(np.uint8)
257261
# if out_file specified, do not show image in window
258262
if out_file is not None:

tools/test.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def parse_args():
5555
choices=['none', 'pytorch', 'slurm', 'mpi'],
5656
default='none',
5757
help='job launcher')
58+
parser.add_argument(
59+
'--opacity',
60+
type=float,
61+
default=0.5,
62+
help='Opacity of painted segmentation map. In (0, 1] range.')
5863
parser.add_argument('--local_rank', type=int, default=0)
5964
args = parser.parse_args()
6065
if 'LOCAL_RANK' not in os.environ:
@@ -123,7 +128,7 @@ def main():
123128
if not distributed:
124129
model = MMDataParallel(model, device_ids=[0])
125130
outputs = single_gpu_test(model, data_loader, args.show, args.show_dir,
126-
efficient_test)
131+
efficient_test, args.opacity)
127132
else:
128133
model = MMDistributedDataParallel(
129134
model.cuda(),

0 commit comments

Comments
 (0)