#!/usr/bin/env python3 """Generate three synthetic particle images with ground truth (SIMULATED data, fixed seeds). Dark particles on a bright background, 8-bit, 512x512, nominal scale 0.5 um/px (the scale is a synthetic assumption, not a real optical calibration).""" import numpy as np, pandas as pd, tifffile from scipy.ndimage import gaussian_filter from skimage.measure import regionprops N = 512 SCALE_UM = 0.5 MIN_AREA = 20 def disk_labels(specs, shape=(N, N)): """specs: list of (cy,cx,ry,rx,theta). Overlap pixels go to the nearest centre (normalised distance).""" yy, xx = np.mgrid[:shape[0], :shape[1]] best = np.full(shape, np.inf) lab = np.zeros(shape, np.int32) for i, (cy, cx, ry, rx, th) in enumerate(specs, 1): c, s = np.cos(th), np.sin(th) u = ((xx - cx) * c + (yy - cy) * s) / rx v = (-(xx - cx) * s + (yy - cy) * c) / ry d = np.sqrt(u * u + v * v) m = (d <= 1) & (d < best) lab[m] = i best[m] = d[m] return lab def place(rng, n, rmin, rmax, gap, allow_edge=0): specs = [] tries = 0 while len(specs) < n and tries < 20000: tries += 1 r = rng.uniform(rmin, rmax) e = rng.uniform(1.0, 1.35) th = rng.uniform(0, np.pi) ry, rx = r, r * e m = max(ry, rx) + 2 cy, cx = rng.uniform(m, N - m, 2) if all(np.hypot(cy - a, cx - b) > max(ry, rx) + max(c, d) + gap for a, b, c, d, _ in specs): specs.append((cy, cx, ry, rx, th)) for _ in range(allow_edge): # particles deliberately cut by the image border r = rng.uniform(rmin, rmax) side = rng.integers(4) p = rng.uniform(40, N - 40) cy, cx = {0: (2, p), 1: (N - 3, p), 2: (p, 2), 3: (p, N - 3)}[side] if all(np.hypot(cy - a, cx - b) > r + max(c, d) + gap for a, b, c, d, _ in specs): specs.append((cy, cx, r, r, 0.0)) return specs def render(lab, bg=205, fg=70, blur=1.2): return gaussian_filter(np.where(lab > 0, fg, bg).astype(float), blur) def save(name, img, lab): img8 = np.clip(np.round(img), 0, 255).astype(np.uint8) tifffile.imwrite(f'images/{name}.tif', img8) tifffile.imwrite(f'ground_truth/{name}_gt_labels.tif', lab.astype(np.uint16)) rows = [] for rp in regionprops(lab): mnr, mnc, mxr, mxc = rp.bbox edge = mnr == 0 or mnc == 0 or mxr == N or mxc == N rows.append(dict(image=name, gt_id=rp.label, area_px=int(rp.area), area_um2=rp.area * SCALE_UM ** 2, centroid_x_px=rp.centroid[1], centroid_y_px=rp.centroid[0], touches_edge=bool(edge), evaluable=bool((not edge) and rp.area >= MIN_AREA))) df = pd.DataFrame(rows) df.to_csv(f'ground_truth/{name}_gt_particles.csv', index=False) return df rng = np.random.default_rng(20261120) # A: clear, well separated labA = disk_labels(place(rng, 45, 5, 13, 10, allow_edge=3)) imgA = render(labA) # B: noise + uneven illumination + 5 low-contrast particles labB = disk_labels(place(rng, 45, 5, 13, 10, allow_edge=3)) yy, xx = np.mgrid[:N, :N] bgB = 215 - 35 * (xx / N) - 15 * (yy / N) fgB = np.full((N, N), 85.0) for d in rng.choice(np.arange(1, labB.max() + 1), 5, replace=False): fgB[labB == d] = 150 imgB = gaussian_filter(np.where(labB > 0, fgB, bgB), 1.5) + rng.normal(0, 14, (N, N)) # C: touching / overlapping clusters + isolated controls r2 = np.random.default_rng(7) centres = [] while len(centres) < 12: cy, cx = r2.uniform(45, N - 45, 2) if all(np.hypot(cy - a, cx - b) > 95 for a, b in centres): centres.append((cy, cx)) specC = [] for cy, cx in centres: k = r2.choice([2, 2, 3, 3, 4]) ang0 = r2.uniform(0, 2 * np.pi) for j in range(k): r = r2.uniform(9, 13) a = ang0 + j * 2 * np.pi / k d = r * 1.55 specC.append((cy + d / 1.3 * np.sin(a), cx + d / 1.3 * np.cos(a), r, r, 0.0)) for _ in range(6): r = r2.uniform(6, 11) cy, cx = r2.uniform(30, N - 30, 2) if all(np.hypot(cy - a, cx - b) > r + max(c, d) + 30 for a, b, c, d, _ in specC): specC.append((cy, cx, r, r, 0.0)) labC = disk_labels(specC) imgC = render(labC) + r2.normal(0, 3, (N, N)) for n, i, l in (('A_clear', imgA, labA), ('B_noisy', imgB, labB), ('C_touching', imgC, labC)): df = save(n, i, l) print(n, 'GT particles', len(df), 'evaluable', int(df.evaluable.sum()), 'edge', int(df.touches_edge.sum()), 'total evaluable area px', int(df[df.evaluable].area_px.sum()))