Skip to content

Commit 7cbebc2

Browse files
author
Corentin
committed
blend image of label and original image
1 parent 01dd944 commit 7cbebc2

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

myoquant/__main__.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def sdh_analysis(
8484
load_cellpose,
8585
load_sdh_model,
8686
run_cellpose,
87+
label2rgb,
88+
blend_image_with_label,
8789
)
8890
from .SDH_analysis import run_sdh_analysis
8991
import numpy as np
@@ -183,6 +185,13 @@ def sdh_analysis(
183185
result_df, full_label_map, df_cellpose_details = run_sdh_analysis(
184186
image_ndarray_sdh, model_SDH, mask_cellpose
185187
)
188+
if export_map:
189+
console.print("Blending label and original image together ! ", style="blue")
190+
labelRGB_map = label2rgb(image_ndarray_sdh, full_label_map)
191+
overlay_img = blend_image_with_label(image_ndarray_sdh, labelRGB_map)
192+
overlay_filename = image_path.stem + "_label_blend.tiff"
193+
overlay_img.save(output_path / overlay_filename)
194+
186195
console.print("Analysis completed ! ", style="green")
187196
table.add_column("Feature", justify="left", style="cyan")
188197
table.add_column("Raw Count", justify="center", style="magenta")
@@ -217,7 +226,6 @@ def sdh_analysis(
217226
console.print(
218227
f"Labelled image saved as {output_path/label_map_name}", style="green"
219228
)
220-
painted_img_name = image_path.stem + "_painted.tiff"
221229
console.print("--- %s seconds ---" % (time.time() - start_time))
222230

223231

@@ -308,6 +316,8 @@ def he_analysis(
308316
load_stardist,
309317
run_cellpose,
310318
run_stardist,
319+
label2rgb,
320+
blend_image_with_label,
311321
)
312322
from .HE_analysis import run_he_analysis
313323
import numpy as np
@@ -358,6 +368,7 @@ def he_analysis(
358368
console.print("Reading image...", style="blue")
359369

360370
image_ndarray = imread(image_path)
371+
361372
if fluo_nuc is not None:
362373
fluo_nuc_ndarray = imread(fluo_nuc)
363374

@@ -379,6 +390,9 @@ def he_analysis(
379390
image_ndarray = image_ndarray * mask_ndarray
380391
if fluo_nuc is not None:
381392
fluo_nuc_ndarray = fluo_nuc_ndarray * mask_ndarray
393+
if fluo_nuc is not None:
394+
mix_cyto_nuc = np.maximum(image_ndarray, fluo_nuc_ndarray)
395+
382396
console.print(f"Masking done.", style="blue")
383397
console.print("Image loaded.", style="blue")
384398
console.print("Starting the Analysis. This may take a while...", style="blue")
@@ -425,6 +439,17 @@ def he_analysis(
425439
result_df, full_label_map, df_nuc_analysis, all_nuc_df_stats = run_he_analysis(
426440
image_ndarray, mask_cellpose, mask_stardist, eccentricity_thresh
427441
)
442+
if export_map:
443+
console.print("Blending label and original image together ! ", style="blue")
444+
if fluo_nuc is not None:
445+
labelRGB_map = label2rgb(mix_cyto_nuc, full_label_map)
446+
overlay_img = blend_image_with_label(mix_cyto_nuc, labelRGB_map, fluo=True)
447+
else:
448+
labelRGB_map = label2rgb(image_ndarray, full_label_map)
449+
overlay_img = blend_image_with_label(image_ndarray, labelRGB_map)
450+
overlay_filename = image_path.stem + "_label_blend.tiff"
451+
overlay_img.save(output_path / overlay_filename)
452+
428453
console.print("Analysis completed ! ", style="green")
429454
table.add_column("Feature", justify="left", style="cyan")
430455
table.add_column("Raw Count", justify="center", style="magenta")
@@ -447,6 +472,11 @@ def he_analysis(
447472
f"Summary Table saved as a .csv file named {output_path/csv_name}",
448473
style="green",
449474
)
475+
if export_map:
476+
console.print(
477+
f"Overlay image saved as a .tiff file named {output_path/overlay_filename}",
478+
style="green",
479+
)
450480
if export_stats:
451481
df_nuc_analysis.drop("image", axis=1).to_csv(
452482
output_path / cell_details_name,
@@ -469,7 +499,6 @@ def he_analysis(
469499
console.print(
470500
f"Labelled image saved as {output_path/label_map_name}", style="green"
471501
)
472-
painted_img_name = image_path.stem + "_painted.tiff"
473502
console.print("--- %s seconds ---" % (time.time() - start_time))
474503

475504

myoquant/common_func.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from stardist.models import StarDist2D
99
from tensorflow import keras
1010
from tensorflow.config import list_physical_devices
11-
11+
import numpy as np
12+
from PIL import Image
1213
from .gradcam import *
1314
from .random_brightness import *
1415

@@ -64,3 +65,24 @@ def run_stardist(image, model_stardist, nms_thresh=0.4, prob_thresh=0.5):
6465
img_norm, nms_thresh=nms_thresh, prob_thresh=prob_thresh
6566
)
6667
return mask_stardist
68+
69+
70+
def label2rgb(img_ndarray, label_map):
71+
label_to_color = {
72+
0: [255, 255, 255],
73+
1: [15, 157, 88],
74+
2: [219, 68, 55],
75+
}
76+
img_rgb = np.zeros((img_ndarray.shape[0], img_ndarray.shape[1], 3), dtype=np.uint8)
77+
78+
for gray, rgb in label_to_color.items():
79+
img_rgb[label_map == gray, :] = rgb
80+
return img_rgb
81+
82+
83+
def blend_image_with_label(image, label_rgb, fluo=False):
84+
image = Image.fromarray(image)
85+
label_rgb = Image.fromarray(label_rgb)
86+
if fluo:
87+
image = image.convert("RGB")
88+
return Image.blend(image, label_rgb, 0.5)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "myoquant"
3-
version = "0.1.10"
3+
version = "0.2.0"
44
description = "MyoQuant🔬: a tool to automatically quantify pathological features in muscle fiber histology images."
55
authors = ["Corentin Meyer <[email protected]>"]
66
maintainers = ["Corentin Meyer <[email protected]>"]

0 commit comments

Comments
 (0)