From klayoutclaw
Detects van der Waals heterostructure flake boundaries from microscope images and maps to KLayout polygons via 5-step agent-orchestrated pipeline (align, detect, combine, commit, review). Use for multi-image flake detection, alignment, or stack segmentation.
npx claudepluginhub caidish/klayoutclaw --plugin klayoutclawThis skill uses the workspace's default tool permissions.
Detect material boundaries in van der Waals heterostructure stacks from optical microscope images and commit them as polygons to KLayout.
Detects graphite, graphene, bottom hBN, and top hBN layers in van der Waals heterostructure microscope images using K-means sub-clustering per material from optimal source images.
Processes microscopy and bioimage images with scikit-image: read/write, filter (Gaussian, median, LoG), segment (thresholding, watershed, active contours), measure regions, detect features. NumPy/SciPy integration.
Processes whole slide pathology images (WSI): loads SVS/TIFF/NDPI formats, detects tissue regions, segments masks, extracts tiles for deep learning datasets.
Share bugs, ideas, or general feedback.
Detect material boundaries in van der Waals heterostructure stacks from optical microscope images and commit them as polygons to KLayout.
This is an orchestration skill. You run it by dispatching subagents for each step. Each step has its own sub-skill SKILL.md with full instructions — the subagent reads that skill and executes autonomously.
You need these before dispatching any subagent:
bottom_part — bottom hBN flake on SiO2 (before transfer)top_part — top flake on PDMS (before transfer)full_stack_raw — assembled stack on SiO2full_stack_lut — (optional) color-enhanced version of full_stack<stack_image_dir>/output/ (the directory containing the source images). Never use /tmp as the default output.<out>/
├── align/ ← warp matrices, footprint, alignment diagnostics
├── detect/ ← per-material masks, contours, detection diagnostics
└── combine/ ← traces.json, overlay images, combine_report.json
Commit and review don't write to disk — they use KLayout directly.
1. align ──→ 2. detect ──→ 3. combine ──→ 4. commit ──→ 5. review
│ │ │ │ │
│ subagent │ subagent │ subagent │ subagent │ subagent
│ reads align/ │ reads detect/│ reads combine/ │ reads commit/│ reads review/
│ SKILL.md │ SKILL.md │ SKILL.md │ SKILL.md │ SKILL.md
└────────────────┴──────────────┴───────────────┴──────────────┘
Each step is executed by a subagent. You (the orchestrator) dispatch subagents sequentially, passing the required context (image paths, pixel size, output dir) to each one. Wait for each subagent to complete before dispatching the next — each step depends on the previous step's outputs.
Goal: Register all source images to the full_stack coordinate system.
Dispatch a subagent with this prompt:
Read
skills/nanodevice_flakedetect_align/SKILL.mdand follow its workflow. Register the source images to the full_stack coordinate system.
- bottom_part:
<path>(same-substrate, use SIFT)- top_part:
<path>(cross-substrate, use Chamfer pipeline,--mirror)- full_stack_raw:
<path>- pixel_size:
<value>um/px- output_dir:
<out>/align
What the subagent does: Runs SIFT for bottom_part, runs the full Chamfer pipeline (source_contour → footprint → sweep → pick rotation → refine) for top_part. Makes its own rotation selection decision by viewing candidate images. IMPORTANT: refine.py takes 10-15 min — the subagent MUST run it as a foreground blocking Bash command with timeout=1200000. It must NOT use run_in_background or sleep/poll loops.
What it produces: warp_sift_bottom.npy, warp_top.npy, footprint_mask.png, footprint_contour.npy, alignment_report.json
Before moving on: Check alignment_report.json status is "complete". If SIFT inliers < 20 or Chamfer IoU < 0.5, the subagent should have flagged the issue.
Goal: Segment each material from its optimal source image.
Dispatch a subagent with this prompt:
Read
skills/nanodevice_flakedetect_detect/SKILL.mdand follow its workflow. Detect all 4 materials and assembledetections.json.
- bottom_part:
<path>(for graphite)- top_part:
<path>(for graphene,--mirror)- full_stack_raw:
<path>(for bottom_hBN)- footprint_mask:
<out>/align/footprint_mask.png(for top_hBN and bottom_hBN)- footprint_contour:
<out>/align/footprint_contour.npy(for top_hBN)- pixel_size:
<value>um/px- output_dir:
<out>/detect
What the subagent does: Runs all 4 detect scripts, reviews candidate images for graphite/graphene, re-runs with --cluster-id if needed, assembles detections.json.
What it produces: Per-material masks/contours/result.json files, detections.json
Before moving on: Verify detections.json exists and has entries for all 4 materials.
Goal: Transform all detections into full_stack coordinates and produce traces.json.
Dispatch a subagent with this prompt:
Read
skills/nanodevice_flakedetect_combine/SKILL.mdand follow its workflow. Transform detections and produce overlay images.
- full_stack_raw:
<path>- full_stack_lut:
<path>(or "not available")- detections:
<out>/detect/detections.json- align_dir:
<out>/align- pixel_size:
<value>um/px- output_dir:
<out>/combine
What the subagent does: Runs ecc_register (if LUT available), transform, and overlay scripts in order. Fully automatic — no agent decisions.
What it produces: traces.json, combine_report.json, overlay_raw.png, overlay_lut.png, mask_composite.png
Before moving on: Read overlay_raw.png to visually confirm all 4 materials are present and properly aligned.
Goal: Insert the detected material polygons into KLayout.
Dispatch a subagent with this prompt:
Read
skills/nanodevice_flakedetect_commit/SKILL.mdand follow its workflow. Commit the traces to KLayout.
- traces:
<out>/combine/traces.json- full_stack_raw:
<path>(for background image)- pixel_size:
<value>um/px
What the subagent does: Loads the background image and adds polygons using execute_script (NOT add_image.py or add_polygon.py — those scripts use MCP client which doesn't work from Docker containers). Reads traces.json, transforms coordinates (image-origin → KLayout centered with Y-flip), and inserts polygons directly via pya API. Takes a screenshot to verify.
What it produces: Polygons on layers 10/0-13/0 in KLayout, background image loaded.
Before moving on: View the screenshot. Polygons should be visible on the correct layers overlaid on the microscope image.
Goal: Validate that the committed polygons are correct.
Dispatch a subagent with this prompt:
Read
skills/nanodevice_flakedetect_review/SKILL.mdand follow its workflow. Review the committed polygons in KLayout.
- traces:
<out>/combine/traces.json- overlay_raw:
<out>/combine/overlay_raw.png- overlay_lut:
<out>/combine/overlay_lut.png(or "not available")- alignment_report:
<out>/align/alignment_report.json- combine_report:
<out>/combine/combine_report.json
What the subagent does: Takes screenshots, isolates layers (using the display skill), compares KLayout polygons against overlay images, answers structured assessment questions, checks quantitative metrics, and returns a PASS/FAIL verdict.
What it produces: A verdict with reasoning.
If PASS: Stack detection complete. Report results to the user.
If FAIL: The subagent will specify which step to retry and what to change. Dispatch a new subagent for that step with adjusted parameters, then re-run all subsequent steps.
| Problem | Retry from | What to change |
|---|---|---|
| Polygon boundaries don't match flake edges | Step 1 (align) | Try different rotation or wider scale range |
| Wrong material detected | Step 2 (detect) | Adjust cluster selection with --cluster-id |
| Polygons flipped or offset | Step 4 (commit) | Check coordinate transform formula |
Max 2 retries per stage. If still failing after 2 retries, report to the user for manual intervention.
instrMCPdev (has opencv, numpy, scipy, sklearn)--mirror flag for PDMS transfers; applies cv2.flip(image, 1) before processingERROR: printed to stderr