Skip to content

Commit ad2c6f3

Browse files
committed
fix #1121 auto adjust saturation checkbox
1 parent 0574ce5 commit ad2c6f3

File tree

2 files changed

+55
-44
lines changed

2 files changed

+55
-44
lines changed

cellpose/gui/gui.py

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ def __init__(self, image=None, logger=None):
273273
self.ratio = 1.
274274
self.reset()
275275

276+
# This needs to go after .reset() is called to get state fully set up:
277+
self.autobtn.checkStateChanged.connect(self.compute_saturation_if_checked)
278+
276279
self.load_3D = False
277280

278281
# if called with image, load it
@@ -954,12 +957,15 @@ def reset(self):
954957
self.opacity = 128 # how opaque masks should be
955958
self.outcolor = [200, 200, 255, 200]
956959
self.NZ, self.Ly, self.Lx = 1, 256, 256
957-
self.saturation = []
958-
for r in range(3):
959-
self.saturation.append([[0, 255] for n in range(self.NZ)])
960-
self.sliders[r].setValue([0, 255])
961-
self.sliders[r].setEnabled(False)
962-
self.sliders[r].show()
960+
self.saturation = self.saturation if hasattr(self, 'saturation') else []
961+
962+
# only adjust the saturation if auto-adjust is on:
963+
if self.autobtn.isChecked():
964+
for r in range(3):
965+
self.saturation.append([[0, 255] for n in range(self.NZ)])
966+
self.sliders[r].setValue([0, 255])
967+
self.sliders[r].setEnabled(False)
968+
self.sliders[r].show()
963969
self.currentZ = 0
964970
self.flows = [[], [], [], [], [[]]]
965971
# masks matrix
@@ -1658,6 +1664,10 @@ def get_normalize_params(self):
16581664
normalize_params = {**normalize_default, **normalize_params}
16591665

16601666
return normalize_params
1667+
1668+
def compute_saturation_if_checked(self):
1669+
if self.autobtn.isChecked():
1670+
self.compute_saturation()
16611671

16621672
def compute_saturation(self, return_img=False):
16631673
norm = self.get_normalize_params()
@@ -1707,42 +1717,43 @@ def compute_saturation(self, return_img=False):
17071717
else:
17081718
img_norm = self.stack if self.restore is None or self.restore == "filter" else self.stack_filtered
17091719

1710-
self.saturation = []
1711-
for c in range(img_norm.shape[-1]):
1712-
self.saturation.append([])
1713-
if np.ptp(img_norm[..., c]) > 1e-3:
1714-
if norm3D:
1715-
x01 = np.percentile(img_norm[..., c], percentile[0])
1716-
x99 = np.percentile(img_norm[..., c], percentile[1])
1717-
if invert:
1718-
x01i = 255. - x99
1719-
x99i = 255. - x01
1720-
x01, x99 = x01i, x99i
1721-
for n in range(self.NZ):
1722-
self.saturation[-1].append([x01, x99])
1723-
else:
1724-
for z in range(self.NZ):
1725-
if self.NZ > 1:
1726-
x01 = np.percentile(img_norm[z, :, :, c], percentile[0])
1727-
x99 = np.percentile(img_norm[z, :, :, c], percentile[1])
1728-
else:
1729-
x01 = np.percentile(img_norm[..., c], percentile[0])
1730-
x99 = np.percentile(img_norm[..., c], percentile[1])
1720+
if self.autobtn.isChecked():
1721+
self.saturation = []
1722+
for c in range(img_norm.shape[-1]):
1723+
self.saturation.append([])
1724+
if np.ptp(img_norm[..., c]) > 1e-3:
1725+
if norm3D:
1726+
x01 = np.percentile(img_norm[..., c], percentile[0])
1727+
x99 = np.percentile(img_norm[..., c], percentile[1])
17311728
if invert:
17321729
x01i = 255. - x99
17331730
x99i = 255. - x01
17341731
x01, x99 = x01i, x99i
1735-
self.saturation[-1].append([x01, x99])
1736-
else:
1737-
for n in range(self.NZ):
1738-
self.saturation[-1].append([0, 255.])
1739-
print(self.saturation[2][self.currentZ])
1732+
for n in range(self.NZ):
1733+
self.saturation[-1].append([x01, x99])
1734+
else:
1735+
for z in range(self.NZ):
1736+
if self.NZ > 1:
1737+
x01 = np.percentile(img_norm[z, :, :, c], percentile[0])
1738+
x99 = np.percentile(img_norm[z, :, :, c], percentile[1])
1739+
else:
1740+
x01 = np.percentile(img_norm[..., c], percentile[0])
1741+
x99 = np.percentile(img_norm[..., c], percentile[1])
1742+
if invert:
1743+
x01i = 255. - x99
1744+
x99i = 255. - x01
1745+
x01, x99 = x01i, x99i
1746+
self.saturation[-1].append([x01, x99])
1747+
else:
1748+
for n in range(self.NZ):
1749+
self.saturation[-1].append([0, 255.])
1750+
print(self.saturation[2][self.currentZ])
17401751

1741-
if img_norm.shape[-1] == 1:
1742-
self.saturation.append(self.saturation[0])
1743-
self.saturation.append(self.saturation[0])
1752+
if img_norm.shape[-1] == 1:
1753+
self.saturation.append(self.saturation[0])
1754+
self.saturation.append(self.saturation[0])
17441755

1745-
self.autobtn.setChecked(True)
1756+
# self.autobtn.setChecked(True)
17461757
self.update_plot()
17471758

17481759

@@ -1986,7 +1997,7 @@ def compute_segmentation(self, custom=False, model_name=None, load_model=True):
19861997
self.masksOn = True
19871998
self.MCheckBox.setChecked(True)
19881999
self.progress.setValue(100)
1989-
if self.restore != "filter" and self.restore is not None:
2000+
if self.restore != "filter" and self.restore is not None and self.autobtn.isChecked():
19902001
self.compute_saturation()
19912002
if not do_3D and not stitch_threshold > 0:
19922003
self.recompute_masks = True

cellpose/gui/io.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,13 @@ def _initialize_images(parent, image, load_3D=False):
211211
"GUI_INFO: normalization checked: computing saturation levels (and optionally filtered image)"
212212
)
213213
parent.compute_saturation()
214-
elif len(parent.saturation) != parent.NZ:
215-
parent.saturation = []
216-
for r in range(3):
217-
parent.saturation.append([])
218-
for n in range(parent.NZ):
219-
parent.saturation[-1].append([0, 255])
220-
parent.sliders[r].setValue([0, 255])
214+
# elif len(parent.saturation) != parent.NZ:
215+
# parent.saturation = []
216+
# for r in range(3):
217+
# parent.saturation.append([])
218+
# for n in range(parent.NZ):
219+
# parent.saturation[-1].append([0, 255])
220+
# parent.sliders[r].setValue([0, 255])
221221
parent.compute_scale()
222222
parent.track_changes = []
223223

0 commit comments

Comments
 (0)