Skip to content

Commit 4d20019

Browse files
Add resizing back
Seems to help for certain segmentation tasks.
1 parent 649a6d6 commit 4d20019

12 files changed

+191
-41
lines changed

debug.png

282 KB
Loading

docs/analysis-segmentation.md

+17
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ Results will be saved in the specified folder. For each image the following file
3333

3434
![segmentation__nuclei](img/segmentation__nuclei.png)
3535

36+
### Resizing can speed up prediction & yield better results
37+
38+
We found that resizing images before segmentation can yield better results for certain images.
39+
Also, it will lead to faster computation time. In case you resize the images, we implemented a post-processing
40+
routine that will resize the predicted masks back to the original image size.
41+
42+
Resizing can be specified in two ways
43+
44+
1. **Scaling factor** recommended]: simply add an integer value. The actual image size will then be divided by this factor
45+
to obtain the new size, i.e. a value of 2 will resize an image 512x512 to 256x256.
46+
2. **New size**: you can directly define the new size of the image, e.g. 256x256. Please note that this size
47+
will be applied to all images, independly of their size. This option is hence not suitable if your data-sets
48+
contain differently sized images.
49+
3650
## Recommended workflow
3751

3852
The default settings of the plugins allow to quickly perform the recommended workflow. You only have
@@ -59,10 +73,12 @@ to paste your data folder.
5973
`String channel` | str | `dapi` | Unique identifier to identify channel.
6074
`Object name` | str | `nuclei` | How the object is called.
6175
`Cellpose model` | str | `nuclei` | Cellpose model for segmentation: `cyto` or `nuclei`. Note that for dense nuclei, the cytoplasmic model might work better.
76+
`New size` | int | 2 | Numbers to specify resizing (see above). No resizing if empty.
6277
`Object diameter` | int | 50 | Typical diameter of the object. Better to be set a bit to small.
6378
`Net Average` | Bool | False | Can improve segmentation accuracy, but is slower (Runs the 4 built-in networks and averages them).
6479
`Resample` | Bool | False | Gives more accurate boundaries, but can be very slow (Runs dynamics at original image size).
6580
`String img ext` | str | `.png` | File extension of images that should be segmented.
81+
`New size` | int | 2 | Numbers to specify resizing (see abov. No resizing if empty.
6682

6783
2. Pressing on the plugin name `SegmentObjects` will start the segmentation.
6884
When using CellPose for the first time, the models for nuclear and cytoplasmic segmentations are downloaded.
@@ -91,6 +107,7 @@ to paste your data folder.
91107
`String CELLS` | str | `cy3` | Unique identifier for images of cytoplasmic stain.
92108
`String NUCLEI` | str | `dapi` | Unique identifier for images of nuclear stain.
93109
`String img ext` | str | `.png` | File extension of images that should be segmented.
110+
`New size` | int | 2 | Numbers to specify resizing (see above). No resizing if empty.
94111
`Size CELLS` | int | 100 | Typical size of a cell (in resized image).
95112
`Size NUCLEI` | int | 50 | Typical size of a nucleus (in resized image).
96113
`Net Average` | Bool | False | Can improve segmentation accuracy, but is slower (Runs the 4 built-in networks and averages them).
0 Bytes
Loading

docs/img/imjoy-segment-objects-ui.png

0 Bytes
Loading

docs/imjoy-installation.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ conda activate fq-segmentation
2727
**Install code Jupyter optimized for ImJoy**:
2828

2929
``` bash
30-
pip install -U imjoy
30+
pip install -U imjoy[jupyter]
3131
```
3232

3333
## Connect to Jupyter engine

imjoy-plugins/SegmentCellsNuclei.imjoy.html

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<docs lang="markdown">
2-
ImJoy plugin to perform deep-learning based cell segmentation.
3-
4-
- For more information, see the detailed documentation [**here**](https://fq-segmentation.readthedocs.io/en/latest/).
5-
- Used the the generalist cell/nuclei segmentation package [**Cellpose**](https://github.com/mouseland/cellpose).
6-
2+
ImJoy plugin to perform cell segmentation with
3+
the generalist cell/nuclei segmentation package [**Cellpose**](https://github.com/mouseland/cellpose).
74
</docs>
85

96
<config lang="json">
107
{
118
"name": "SegmentCellsNuclei",
129
"type": "native-python",
13-
"version": "0.2.0",
10+
"version": "0.2.1",
1411
"description": "Segment cells AND nuclei.",
1512
"tags": ["stable","dev"],
1613
"ui": [
@@ -19,6 +16,7 @@
1916
"<span style='font-weight: 550; color:#666362'>Path SAVE</span>: {id: 'path_save', type: 'string', placeholder: 'segmentation-input˃˃segmentation-results'}",
2017
"<span style='font-weight: 550; color:#666362'>String CELLS</span>: {id: 'str_cyto', type: 'string', placeholder: 'cy3'}",
2118
"<span style='font-weight: 550; color:#666362'>String NUCLEI</span>: {id: 'str_nuclei', type: 'string', placeholder: 'dapi'}",
19+
"<span style='font-weight: 550; color:#666362'>Scaling factor/New size: </span>: {id: 'new_size', type: 'string', placeholder: '2'}",
2220
"<span style='font-weight: 550; color:#666362'>Size CELLS</span>: {id: 'size_cells', type: 'number', placeholder: 200}",
2321
"<span style='font-weight: 550; color:#666362'>Size NUCLEI</span>: {id: 'size_nuclei', type: 'number', placeholder: 100}",
2422
"<span style='font-weight: 550; color:#666362'>Net Average</span>: {id: 'net_avg', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
@@ -38,7 +36,7 @@
3836
"stable":["pip: -U git+https://github.com/fish-quant/fq-segmentation@master"]
3937
},
4038
"dependencies": [],
41-
"cover": "https://raw.githubusercontent.com/fish-quant/fq-segmentation/master/docs/img/segmentation__cells.png"
39+
"cover": "https://fish-quant.github.io/fq-segmentation/img/segmentation__cells.png"
4240
}
4341
</config>
4442

@@ -114,13 +112,32 @@
114112
else:
115113
net_avg = False
116114

115+
# >> Check new size
116+
new_size_str = ctx.config.new_size
117+
118+
if len(new_size_str) == 0:
119+
new_size = ()
120+
121+
else:
122+
if new_size_str.count(',') == 0:
123+
new_size = (int(new_size_str),)
124+
125+
elif new_size_str.count(',') == 1:
126+
new_size = tuple(int(x) for x in new_size_str.split(','))
127+
128+
else:
129+
api.alert('String to define new size has to contain one or zero \",\" to separate the new dimensions in XY.')
130+
return
131+
132+
api.log(f'New size: {new_size}')
117133

118134
# >>>> Call pre-processing function
119135
api.showStatus('Performing segmentation ... see log for details')
120136
utils_cellpose.segment_cells_nuclei_indiv(
121137
path_scan=path_scan,
122138
str_channels=(ctx.config.str_cyto, ctx.config.str_nuclei),
123139
img_ext=ctx.config.img_ext,
140+
new_size=new_size,
124141
model_types=('cyto','nuclei'),
125142
diameters=(ctx.config.size_cells, ctx.config.size_nuclei),
126143
net_avg=net_avg,

imjoy-plugins/SegmentObjects.imjoy.html

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
<docs lang="markdown">
2-
ImJoy plugin to perform deep-learning based cell segmentation.
3-
4-
- For more information, see the detailed documentation [**here**](https://fq-segmentation.readthedocs.io/en/latest/).
5-
- Used the the generalist cell/nuclei segmentation package [**Cellpose**](https://github.com/mouseland/cellpose).
6-
2+
ImJoy plugin to perform object segmentation with
3+
the generalist cell/nuclei segmentation package [**Cellpose**](https://github.com/mouseland/cellpose).
74
</docs>
85

96
<config lang="json">
107
{
118
"name": "SegmentObjects",
129
"type": "native-python",
13-
"version": "0.2.0",
10+
"version": "0.2.1",
1411
"description": "Segment cells OR nuclei.",
1512
"tags": ["stable","dev"],
1613
"ui": [
@@ -20,6 +17,7 @@
2017
"<span style='font-weight: 550; color:#666362'>String channel</span>: {id: 'str_channel', type: 'string', placeholder: 'dapi'}",
2118
"<span style='font-weight: 550; color:#666362'>Object name</span>: {id: 'obj_name', type: 'string', placeholder: 'nuclei'}",
2219
"<span style='font-weight: 550; color:#666362'>Cellpose model</span>: {id: 'model_type', type: 'choose', options: ['nuclei', 'cyto'], placeholder: 'nuclei'}",
20+
"<span style='font-weight: 550; color:#666362'>Scaling factor/New size:</span>: {id: 'new_size', type: 'string', placeholder: '1'}",
2321
"<span style='font-weight: 550; color:#666362'>Object diameter</span>: {id: 'diameter', type: 'number', placeholder: 100}",
2422
"<span style='font-weight: 550; color:#666362'>Net Average</span>: {id: 'net_avg', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
2523
"<span style='font-weight: 550; color:#666362'>Resample</span>: {id: 'resample', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
@@ -38,7 +36,7 @@
3836
"stable":["pip: -U git+https://github.com/fish-quant/fq-segmentation@master"]
3937
},
4038
"dependencies": [],
41-
"cover": "https://raw.githubusercontent.com/fish-quant/fq-segmentation/master/docs/img/segmentation__nuclei.png"
39+
"cover": "https://fish-quant.github.io/fq-segmentation/img/segmentation__cells.png"
4240
}
4341
</config>
4442

@@ -113,6 +111,24 @@
113111
else:
114112
net_avg = False
115113

114+
# >> Check new size
115+
new_size_str = ctx.config.new_size
116+
117+
if len(new_size_str) == 0:
118+
new_size = ()
119+
120+
else:
121+
if new_size_str.count(',') == 0:
122+
new_size = (int(new_size_str),)
123+
124+
elif new_size_str.count(',') == 1:
125+
new_size = tuple(int(x) for x in new_size_str.split(','))
126+
127+
else:
128+
api.alert('String to define new size has to contain one or zero \",\" to separate the new dimensions in XY.')
129+
return
130+
131+
api.log(f'New size: {new_size}')
116132

117133
# >>> Call segmentation function
118134
api.showStatus('Performing segmentation ... see log for details')
@@ -122,6 +138,7 @@
122138
obj_name=ctx.config.obj_name,
123139
str_channel=ctx.config.str_channel,
124140
img_ext=ctx.config.img_ext,
141+
new_size=new_size,
125142
model_type=ctx.config.model_type,
126143
diameter= ctx.config.diameter,
127144
net_avg=net_avg,

manifest.imjoy.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
{
8282
"name": "SegmentCellsNuclei",
8383
"type": "native-python",
84-
"version": "0.2.0",
84+
"version": "0.2.1",
8585
"description": "Segment cells AND nuclei.",
8686
"tags": [
8787
"stable",
@@ -93,13 +93,14 @@
9393
"<span style='font-weight: 550; color:#666362'>Path SAVE</span>: {id: 'path_save', type: 'string', placeholder: 'segmentation-input˃˃segmentation-results'}",
9494
"<span style='font-weight: 550; color:#666362'>String CELLS</span>: {id: 'str_cyto', type: 'string', placeholder: 'cy3'}",
9595
"<span style='font-weight: 550; color:#666362'>String NUCLEI</span>: {id: 'str_nuclei', type: 'string', placeholder: 'dapi'}",
96+
"<span style='font-weight: 550; color:#666362'>Scaling factor/New size: </span>: {id: 'new_size', type: 'string', placeholder: '2'}",
9697
"<span style='font-weight: 550; color:#666362'>Size CELLS</span>: {id: 'size_cells', type: 'number', placeholder: 200}",
9798
"<span style='font-weight: 550; color:#666362'>Size NUCLEI</span>: {id: 'size_nuclei', type: 'number', placeholder: 100}",
9899
"<span style='font-weight: 550; color:#666362'>Net Average</span>: {id: 'net_avg', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
99100
"<span style='font-weight: 550; color:#666362'>Resample</span>: {id: 'resample', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
100101
"<span style='font-weight: 550; color:#666362'>String img ext</span>: {id: 'img_ext', type: 'string', placeholder: '.png'}"
101102
],
102-
"cover": "https://raw.githubusercontent.com/fish-quant/fq-segmentation/master/docs/img/segmentation__cells.png",
103+
"cover": "https://fish-quant.github.io/fq-segmentation/img/segmentation__cells.png",
103104
"inputs": null,
104105
"outputs": null,
105106
"flags": [],
@@ -121,7 +122,7 @@
121122
{
122123
"name": "SegmentObjects",
123124
"type": "native-python",
124-
"version": "0.2.0",
125+
"version": "0.2.1",
125126
"description": "Segment cells OR nuclei.",
126127
"tags": [
127128
"stable",
@@ -134,12 +135,13 @@
134135
"<span style='font-weight: 550; color:#666362'>String channel</span>: {id: 'str_channel', type: 'string', placeholder: 'dapi'}",
135136
"<span style='font-weight: 550; color:#666362'>Object name</span>: {id: 'obj_name', type: 'string', placeholder: 'nuclei'}",
136137
"<span style='font-weight: 550; color:#666362'>Cellpose model</span>: {id: 'model_type', type: 'choose', options: ['nuclei', 'cyto'], placeholder: 'nuclei'}",
138+
"<span style='font-weight: 550; color:#666362'>Scaling factor/New size:</span>: {id: 'new_size', type: 'string', placeholder: '1'}",
137139
"<span style='font-weight: 550; color:#666362'>Object diameter</span>: {id: 'diameter', type: 'number', placeholder: 100}",
138140
"<span style='font-weight: 550; color:#666362'>Net Average</span>: {id: 'net_avg', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
139141
"<span style='font-weight: 550; color:#666362'>Resample</span>: {id: 'resample', type: 'choose', options: ['false', 'true'], placeholder: 'false'}",
140142
"<span style='font-weight: 550; color:#666362'>String img ext</span>: {id: 'img_ext', type: 'string', placeholder: '.png'}"
141143
],
142-
"cover": "https://raw.githubusercontent.com/fish-quant/fq-segmentation/master/docs/img/segmentation__nuclei.png",
144+
"cover": "https://fish-quant.github.io/fq-segmentation/img/segmentation__cells.png",
143145
"inputs": null,
144146
"outputs": null,
145147
"flags": [],

notebooks/segment-cells-nuclei.ipynb

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242
"str_cyto = 'cy3' # Identifier of channel for cytoplasmic segmentation\n",
4343
"str_nuclei = 'dapi' # Identifier of channel for nuclear segmentation\n",
4444
"img_ext = '.png' # Extension of images to be segmented\n",
45-
"new_size = (2,) # Size of images (when resize should be applied, empty tuple otherwise)\n",
4645
"\n",
46+
"new_size = (2,) # Size of images (when resize should be applied, empty tuple otherwise)\n",
4747
"diameter_cells = 100 # Typical size (diameter) of cells\n",
4848
"diameter_nuclei = 50 # Typical size (diameter) of nuclei\n",
4949
"\n",
@@ -55,6 +55,7 @@
5555
" path_scan=path_scan, \n",
5656
" str_channels=str_channels, \n",
5757
" img_ext=img_ext, \n",
58+
" new_size=new_size,\n",
5859
" model_types=('cyto','nuclei'), \n",
5960
" diameters = (diameter_cells, diameter_nuclei), \n",
6061
" net_avg=net_avg, \n",

notebooks/segment-nuclei.ipynb

+5-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"obj_name='nuclei', # Name of object that should be segmented\n",
4343
"str_channel = 'dapi' # Identifier of channel for nuclear segmentation\n",
4444
"img_ext = '.png' # Extension of images to be segmented\n",
45+
"\n",
46+
"new_size = (2,) # Size of images (when resize should be applied, empty tuple otherwise)\n",
4547
"diameter = 50 # Typical size (diameter) of nuclei\n",
48+
"\n",
4649
"model_type = 'nuclei' # Can be 'nuclei' or 'cyto', for densely packed nuclei 'cyto' might work well. \n",
4750
"net_avg=False # runs the 4 built-in networks and averages them if True, runs one network if False\n",
4851
"resample=False # run dynamics at original image size (will be (much) slower but create more accurate boundaries)\n",
@@ -52,7 +55,8 @@
5255
" path_scan=path_scan, \n",
5356
" obj_name=obj_name, \n",
5457
" str_channel=str_channel, \n",
55-
" img_ext=img_ext, \n",
58+
" img_ext=img_ext,\n",
59+
" new_size=new_size, \n",
5660
" model_type=model_type, \n",
5761
" diameter = diameter, \n",
5862
" net_avg=net_avg, \n",

segwrap/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
# MINOR: new features
1313
# PATCH: backwards compatible bug fixes
1414
# MAJOR.MINOR.PATCHdev means a version under development
15-
__version__ = "0.3.1"
15+
__version__ = "0.3.2"

0 commit comments

Comments
 (0)