diff --git a/.gitignore b/.gitignore index aab52d9..3b1b6d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.png \ No newline at end of file +*.png +test_output +test_output/* \ No newline at end of file diff --git a/cp_run.py b/cp_run.py index 75c6dd6..49d6e43 100644 --- a/cp_run.py +++ b/cp_run.py @@ -1,5 +1,8 @@ -from cellpose import models +from cellpose import models, plot from cellpose.io import imread, save_masks +from PIL import Image +import numpy as np +import os, datetime def test(): model = models.CellposeModel(gpu=True) @@ -8,8 +11,21 @@ def test(): masks, flows, styles = model.eval( imgs, flow_threshold=0.4, cellprob_threshold=0.0 ) + for i, m in enumerate(masks): + print( + f"[{i}] mask max={int(getattr(m, 'max', lambda: 0)()) if hasattr(m, 'max') else int(np.max(m))}, unique={np.unique(m)[:5]} ..." + ) - + ts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + outdir = os.path.join(os.path.dirname(__file__), "test_output", ts) + os.makedirs(outdir, exist_ok=True) # 自动创建目录 for img, mask, flow, name in zip(imgs, masks, flows, files): - out = name.rsplit('.', 1)[0] + "_output" - save_masks(imgs, mask, flow, out, png=True) + base = os.path.join(outdir, os.path.splitext(os.path.basename(name))[0]) + #使用内置绘图生成蒙版 + out = base + "_output" + save_masks(imgs, mask, flow, out, tif=True) + + # 用 plot 生成彩色叠加图(不依赖 skimage) + rgb = plot.image_to_rgb(img, channels=[0, 0]) # 原图转 RGB + over = plot.mask_overlay(rgb, masks=mask, colors=None) # 叠加彩色实例 + Image.fromarray(over).save(base + "_overlay.png")