Skip to content

Commit 5fae7d8

Browse files
author
Dejan Skrelic
committed
feat(plot): add share_axes to show_segmentation for synced viewing
Refactor subplot creation into a loop to allow optional shared x and y axes between the image, outlines, masks, and flows.
1 parent 6d23968 commit 5fae7d8

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

cellpose/plot.py

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def dx_to_circ(dP):
4444
return rgb
4545

4646

47-
def show_segmentation(fig, img, maski, flowi, channels=[0, 0], file_name=None):
47+
def show_segmentation(fig, img, maski, flowi, channels=[0, 0], file_name=None, seg_norm=False, share_axes=False):
4848
"""Plot segmentation results (like on website).
4949
5050
Can save each panel of figure with file_name option. Use channels option if
@@ -58,47 +58,53 @@ def show_segmentation(fig, img, maski, flowi, channels=[0, 0], file_name=None):
5858
channels (list of int, optional): Channels used to run Cellpose, no need to use if image is RGB. Defaults to [0, 0].
5959
file_name (str, optional): File name of image. If file_name is not None, figure panels are saved. Defaults to None.
6060
seg_norm (bool, optional): Improve cell visibility under labels. Defaults to False.
61+
share_axes (bool, optional): Share x and y axes between subplots for synchronized zooming. Defaults to False.
6162
"""
6263
if not MATPLOTLIB_ENABLED:
6364
raise ImportError(
6465
"matplotlib not installed, install with 'pip install matplotlib'")
65-
ax = fig.add_subplot(1, 4, 1)
66+
67+
# 1. Process initial image
6668
img0 = img.copy()
67-
6869
if img0.shape[0] < 4:
6970
img0 = np.transpose(img0, (1, 2, 0))
7071
if img0.shape[-1] < 3 or img0.ndim < 3:
7172
img0 = image_to_rgb(img0, channels=channels)
7273
else:
7374
if img0.max() <= 50.0:
7475
img0 = np.uint8(np.clip(img0, 0, 1) * 255)
75-
ax.imshow(img0)
76-
ax.set_title("original image")
77-
ax.axis("off")
7876

77+
# 2. Prepare plot data
7978
outlines = utils.masks_to_outlines(maski)
80-
8179
overlay = mask_overlay(img0, maski)
82-
83-
ax = fig.add_subplot(1, 4, 2)
80+
8481
outX, outY = np.nonzero(outlines)
8582
imgout = img0.copy()
86-
imgout[outX, outY] = np.array([255, 0, 0]) # pure red
87-
88-
ax.imshow(imgout)
89-
ax.set_title("predicted outlines")
90-
ax.axis("off")
91-
92-
ax = fig.add_subplot(1, 4, 3)
93-
ax.imshow(overlay)
94-
ax.set_title("predicted masks")
95-
ax.axis("off")
83+
imgout[outX, outY] = np.array([255, 0, 0])
84+
85+
# List of (data, title) for the 4 subplots
86+
plot_data = [
87+
(img0, "original image"),
88+
(imgout, "predicted outlines"),
89+
(overlay, "predicted masks"),
90+
(flowi, "predicted cell pose")
91+
]
9692

97-
ax = fig.add_subplot(1, 4, 4)
98-
ax.imshow(flowi)
99-
ax.set_title("predicted cell pose")
100-
ax.axis("off")
93+
# 3. Create subplots in a loop
94+
axes = []
95+
for i, (data, title) in enumerate(plot_data):
96+
# Determine sharing: only for subplots index 1, 2, 3 and if share_axes is True
97+
if i > 0 and share_axes:
98+
ax = fig.add_subplot(1, 4, i + 1, sharex=axes[0], sharey=axes[0])
99+
else:
100+
ax = fig.add_subplot(1, 4, i + 1)
101+
102+
ax.imshow(data)
103+
ax.set_title(title)
104+
ax.axis("off")
105+
axes.append(ax)
101106

107+
# 4. Handle file saving
102108
if file_name is not None:
103109
save_path = os.path.splitext(file_name)[0]
104110
io.imsave(save_path + "_overlay.jpg", overlay)

0 commit comments

Comments
 (0)