分割完了算面积。像素计数、μm²换算、像素标定——三步出数据。一、面积计算原理步骤公式说明像素计数pixel_count np.sum(mask 0)统计白色像素实际面积area_um2 pixel_count × (μm/像素)²像素×标定值的平方占比percentage (pixel_count / total) × 100%各相占总面积比例二、核心代码pythonimport numpy as np PIXEL_PER_MICRON {100: 1.0, 200: 0.5} def calculate_phase_area(masks, magnification100, pixel_per_umNone): results {} total_pixels 0 if pixel_per_um is None: pixel_per_um PIXEL_PER_MICRON.get(magnification, 1.0) for phase_name, mask in masks.items(): pixel_count np.sum(mask 0) if isinstance(pixel_count, (np.ndarray, list, tuple)): pixel_count int(pixel_count[0]) total_pixels pixel_count area_um2 pixel_count * (pixel_per_um ** 2) if isinstance(area_um2, (np.ndarray, list, tuple)): area_um2 float(area_um2[0]) results[phase_name] {pixel_count: pixel_count, area_um2: area_um2} for phase_name in results: if total_pixels 0: percentage (results[phase_name][pixel_count] / total_pixels) * 100 results[phase_name][percentage] round(percentage, 2) else: results[phase_name][percentage] 0.0 return results三、像素标定原理用户在图像上画一条已知长度的线段程序计算μm/像素。textμm/像素 线段实际长度(μm) / 线段像素长度标定流程text点击开始 → 画线段两点→ 输入实际长度(μm) → 完成 → 自动保存标定值优先级标定值 放大倍率默认值四、标定数据持久化pythondef save_calibration(self): cal_file os.path.join(os.path.dirname(__file__), calibration.json) data {pixel_per_um: self.pixel_per_um} with open(cal_file, w, encodingutf-8) as f: json.dump(data, f, indent2, ensure_asciiFalse) def load_calibration(self): cal_file os.path.join(os.path.dirname(__file__), calibration.json) if os.path.exists(cal_file): with open(cal_file, r, encodingutf-8) as f: data json.load(f) if pixel_per_um in data: self.pixel_per_um float(data[pixel_per_um])五、默认标定值倍率μm/像素100×1.0200×0.5局限性不同设备有差异建议用户自行标定。下篇预告下一篇写AOI感兴趣区域选择实现矩形、圆形、多边形三种AOI。