# lakshyakhandelwal2901/dicom-to-3d-opus architecture

> Architecture diagram of the GitHub repository lakshyakhandelwal2901/dicom-to-3d-opus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/lakshyakhandelwal2901/dicom-to-3d-opus
- Repository: https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus

## Overview

#### Purpose and Entry Points
MedRecon Engine is an on-premises Python pipeline that transforms CT DICOM series into surgical-planning meshes, primarily STL or OBJ. The orchestration and Click CLI live in `medrecon_engine/main.py`; its Python API exposes single-case, anatomy batch, directory batch, pure-HU, and AI workflows. Root scripts such as `batch_run.py` and `triple_obj_pipeline.py` are alternate operational drivers.

#### Input, Validation, and Volume Preparation
The shared front end discovers/loads DICOM series, validates scan resolution constraints, converts pixels to Hounsfield Units, and prepares volumes for downstream processing.
- `medrecon_engine/core/volume_loader.py`
- `medrecon_engine/core/dicom_validator.py`
- `medrecon_engine/core/hu_converter.py`

Classic processing additionally performs configurable smoothing and isotropic resampling; precision limits and HU clipping are centralized in `medrecon_engine/config/precision_config.py`.

#### Segmentation Branches
`medrecon_engine/main.py` selects one of three paths:
- **Classic:** anatomy-specific segmenters registered through `medrecon_engine/anatomy/registry.py`, with implementations such as `medrecon_engine/anatomy/bone.py` and `medrecon_engine/anatomy/lung.py`.
- **Pure HU:** multi-structure threshold/morphology segmentation via `medrecon_engine/anatomy/hu_segmenter.py`, using threshold definitions in `medrecon_engine/config/hu_ranges.py`.
- **AI:** DICOM is converted to NIfTI, then TotalSegmentator labels are produced through `medrecon_engine/anatomy/ai_segmenter.py`; label/group mappings reside in `medrecon_engine/config/structure_groups.py`.

#### Adaptive HU and Clinical Analysis
Patient-specific HU histogram analysis can tune segmentation thresholds rather than relying only on fixed ranges. This boundary separates image-derived calibration and findings/report generation from core reconstruction.
- `medrecon_engine/hu_model/hu_estimator.py`
- `medrecon_engine/analysis/hu_analyzer.py`
- `medrecon_engine/analysis/medical_findings.py`

The findings path includes an AI-oriented analyzer at `medrecon_engine/analysis/medgemma_analyzer.py`, which is optional (`--no-ai`) and is the main potential external-AI/service boundary.

#### Mesh Extraction and Processing
Segmentation masks or AI labels become surfaces through VTK-based extraction. Classic/HU flows use volume-oriented marching cubes; AI labels use label-to-mesh conversion. Meshes are then smoothed, decimated, repaired, validated, and, for multi-organ outputs, categorized or merged.
- `medrecon_engine/mesh/vtk_generator.py`
- `medrecon_engine/mesh/mesh_from_labels.py`
- `medrecon_engine/mesh/mesh_postprocess.py`

#### Quality, Export, and Audit
Validated meshes receive topology and geometric metrics, combined into a confidence score and SURGICAL/USABLE/REJECT grade. Outputs are written as binary/ASCII STL or grouped OBJ, with optional rendered HTML/slice-overlay reports. Each run emits structured JSONL audit records, including failures and reconstruction parameters.
- `medrecon_engine/quality/confidence_score.py`
- `medrecon_engine/export/stl_writer.py`
- `medrecon_engine/audit/logger.py`

#### Runtime and External Boundaries
The core runtime is Python 3.10+ using SimpleITK, VTK, and NumPy; VTK system libraries are required for rendering. It is designed for local/on-prem execution and Docker deployment, although optional exporters integrate with Azure Blob, Google Cloud Storage, and Google Drive:
- `medrecon_engine/export/azure_uploader.py`
- `medrecon_engine/export/gcs_uploader.py`
- `medrecon_engine/export/drive_uploader.py`

Main entry points: [`medrecon_engine/main.py`](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/main.py) (CLI & workflow API), [`medrecon_engine/core/hu_converter.py`](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/hu_converter.py) (HU volume converter), [`medrecon_engine/anatomy/hu_segmenter.py`](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/hu_segmenter.py) (Pure-HU segmenter), [`medrecon_engine/hu_model/hu_estimator.py`](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/hu_model/hu_estimator.py) (Adaptive HU estimator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; orchestration"]
  node_cli["CLI &amp; workflow API<br/>Python entrypoint<br/>[main.py]"]
end

subgraph group_input["Volume preparation"]
  node_loader["DICOM series loader<br/>input loader<br/>[volume_loader.py]"]
  node_validator["Scan validator<br/>validation<br/>[dicom_validator.py]"]
  node_hu_converter["HU volume converter<br/>volume transform<br/>[hu_converter.py]"]
  node_precision["Precision settings<br/>processing policy"]
end

subgraph group_segmentation["Segmentation branches"]
  node_hu_estimator["Adaptive HU estimator<br/>image calibration<br/>[hu_estimator.py]"]
  node_classic_registry["Classic anatomy registry<br/>segmenter dispatch<br/>[registry.py]"]
  node_classic_segmenters["Classic anatomy segmenters<br/>segmentation<br/>[bone.py]"]
  node_hu_segmenter["Pure-HU segmenter<br/>segmentation<br/>[hu_segmenter.py]"]
  node_hu_ranges["HU threshold ranges<br/>segmentation policy<br/>[hu_ranges.py]"]
  node_ai_segmenter{{"TotalSegmentator path<br/>AI segmentation<br/>[ai_segmenter.py]"}}
  node_structure_groups["AI label mappings<br/>label policy"]
end

subgraph group_mesh["Mesh processing"]
  node_vtk_generator["Volume surface extraction<br/>VTK meshing<br/>[vtk_generator.py]"]
  node_label_mesh["Label-to-mesh conversion<br/>VTK meshing"]
  node_postprocess["Mesh post-processing<br/>mesh pipeline"]
end

subgraph group_delivery["Quality &amp; delivery"]
  node_quality["Confidence grading<br/>quality assessment"]
  node_exporter["STL / OBJ export<br/>artifact writer<br/>[stl_writer.py]"]
  node_findings["Clinical findings analysis<br/>report analysis"]
  node_medgemma{{"Optional AI findings<br/>external AI boundary"}}
  node_audit["JSONL audit log<br/>run audit<br/>[logger.py]"]
  node_cloud_uploaders{{"Optional cloud uploaders<br/>external storage<br/>[azure_uploader.py]"}}
end

node_cli -->|"starts case or batch"| node_loader
node_loader -->|"DICOM series"| node_validator
node_validator -->|"validated scan"| node_hu_converter
node_precision -.->|"clipping limits"| node_hu_converter
node_hu_converter -->|"HU volume"| node_hu_estimator
node_cli -->|"classic branch"| node_classic_registry
node_hu_converter -->|"prepared volume"| node_classic_registry
node_precision -.->|"smoothing and resampling"| node_classic_segmenters
node_hu_estimator -.->|"adaptive thresholds"| node_classic_segmenters
node_classic_registry -->|"anatomy selection"| node_classic_segmenters
node_cli -->|"pure-HU branch"| node_hu_segmenter
node_hu_converter -->|"prepared volume"| node_hu_segmenter
node_hu_estimator -.->|"adaptive thresholds"| node_hu_segmenter
node_hu_ranges -.->|"fixed thresholds"| node_hu_segmenter
node_cli -->|"AI branch"| node_ai_segmenter
node_loader -->|"DICOM series"| node_ai_segmenter
node_structure_groups -.->|"label mappings"| node_ai_segmenter
node_classic_segmenters -->|"segmentation masks"| node_vtk_generator
node_hu_segmenter -->|"structure masks"| node_vtk_generator
node_ai_segmenter -->|"segmentation labels"| node_label_mesh
node_vtk_generator -->|"surfaces"| node_postprocess
node_label_mesh -->|"organ meshes"| node_postprocess
node_postprocess -->|"validated meshes"| node_quality
node_quality -->|"graded meshes"| node_exporter
node_hu_estimator -->|"histogram analysis"| node_findings
node_findings -.->|"optional AI analysis"| node_medgemma
node_quality -->|"metrics and grade"| node_audit
node_exporter -->|"artifact outcome"| node_audit
node_exporter -.->|"optional upload"| node_cloud_uploaders
node_cli -->|"parameters and failures"| node_audit

click node_cli "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/main.py"
click node_loader "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/volume_loader.py"
click node_validator "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/dicom_validator.py"
click node_hu_converter "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/hu_converter.py"
click node_precision "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/precision_config.py"
click node_hu_estimator "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/hu_model/hu_estimator.py"
click node_classic_registry "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/registry.py"
click node_classic_segmenters "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/bone.py"
click node_hu_segmenter "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/hu_segmenter.py"
click node_hu_ranges "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/hu_ranges.py"
click node_ai_segmenter "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/ai_segmenter.py"
click node_structure_groups "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/structure_groups.py"
click node_vtk_generator "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/vtk_generator.py"
click node_label_mesh "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/mesh_from_labels.py"
click node_postprocess "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/mesh_postprocess.py"
click node_quality "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/quality/confidence_score.py"
click node_exporter "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/export/stl_writer.py"
click node_findings "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/analysis/medical_findings.py"
click node_medgemma "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/analysis/medgemma_analyzer.py"
click node_audit "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/audit/logger.py"
click node_cloud_uploaders "https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/export/azure_uploader.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli toneBlue
class node_loader,node_validator,node_hu_converter,node_precision toneAmber
class node_hu_estimator,node_classic_registry,node_classic_segmenters,node_hu_segmenter,node_hu_ranges,node_ai_segmenter,node_structure_groups toneMint
class node_vtk_generator,node_label_mesh,node_postprocess toneRose
class node_quality,node_exporter,node_findings,node_medgemma,node_audit,node_cloud_uploaders toneIndigo
```

## Components

### Entry & orchestration

- [CLI & workflow API](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/main.py): Python entrypoint · Selects single-case, batch, HU, and AI workflows. · `medrecon_engine/main.py`

### Volume preparation

- [DICOM series loader](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/volume_loader.py): input loader · `medrecon_engine/core/volume_loader.py`
- [Scan validator](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/dicom_validator.py): validation · Enforces scan resolution constraints. · `medrecon_engine/core/dicom_validator.py`
- [HU volume converter](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/core/hu_converter.py): volume transform · `medrecon_engine/core/hu_converter.py`
- [Precision settings](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/precision_config.py): processing policy · Controls smoothing, resampling, and HU clipping. · `medrecon_engine/config/precision_config.py`

### Segmentation branches

- [Adaptive HU estimator](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/hu_model/hu_estimator.py): image calibration · `medrecon_engine/hu_model/hu_estimator.py`
- [Classic anatomy registry](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/registry.py): segmenter dispatch · Routes anatomy requests to registered segmenters. · `medrecon_engine/anatomy/registry.py`
- [Classic anatomy segmenters](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/bone.py): segmentation · Includes bone and lung implementations. · `medrecon_engine/anatomy/bone.py`
- [Pure-HU segmenter](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/hu_segmenter.py): segmentation · Uses threshold and morphology rules for multiple structures. · `medrecon_engine/anatomy/hu_segmenter.py`
- [HU threshold ranges](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/hu_ranges.py): segmentation policy · `medrecon_engine/config/hu_ranges.py`
- [TotalSegmentator path](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/anatomy/ai_segmenter.py): AI segmentation · Converts DICOM to NIfTI and produces labels. · `medrecon_engine/anatomy/ai_segmenter.py`
- [AI label mappings](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/config/structure_groups.py): label policy · `medrecon_engine/config/structure_groups.py`

### Mesh processing

- [Volume surface extraction](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/vtk_generator.py): VTK meshing · Marching-cubes extraction for classic and HU masks. · `medrecon_engine/mesh/vtk_generator.py`
- [Label-to-mesh conversion](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/mesh_from_labels.py): VTK meshing · Creates surfaces from AI labels. · `medrecon_engine/mesh/mesh_from_labels.py`
- [Mesh post-processing](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/mesh/mesh_postprocess.py): mesh pipeline · Smooths, decimates, repairs, validates, and merges meshes. · `medrecon_engine/mesh/mesh_postprocess.py`

### Quality & delivery

- [Confidence grading](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/quality/confidence_score.py): quality assessment · Scores topology and geometry as SURGICAL, USABLE, or REJECT. · `medrecon_engine/quality/confidence_score.py`
- [STL / OBJ export](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/export/stl_writer.py): artifact writer · Writes binary or ASCII STL and grouped OBJ. · `medrecon_engine/export/stl_writer.py`
- [Clinical findings analysis](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/analysis/medical_findings.py): report analysis · Generates image-derived findings and reports. · `medrecon_engine/analysis/medical_findings.py`
- [Optional AI findings](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/analysis/medgemma_analyzer.py): external AI boundary · Optional analyzer disabled with --no-ai. · `medrecon_engine/analysis/medgemma_analyzer.py`
- [JSONL audit log](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/audit/logger.py): run audit · Records parameters, outcomes, and failures. · `medrecon_engine/audit/logger.py`
- [Optional cloud uploaders](https://github.com/lakshyakhandelwal2901/dicom-to-3d-opus/blob/main/medrecon_engine/export/azure_uploader.py): external storage · Azure Blob, GCS, and Google Drive integrations. · `medrecon_engine/export/azure_uploader.py`

## Connections

- CLI & workflow API → DICOM series loader: starts case or batch
- DICOM series loader → Scan validator: DICOM series
- Scan validator → HU volume converter: validated scan
- Precision settings → HU volume converter: clipping limits
- HU volume converter → Adaptive HU estimator: HU volume
- CLI & workflow API → Classic anatomy registry: classic branch
- HU volume converter → Classic anatomy registry: prepared volume
- Precision settings → Classic anatomy segmenters: smoothing and resampling
- Adaptive HU estimator → Classic anatomy segmenters: adaptive thresholds
- Classic anatomy registry → Classic anatomy segmenters: anatomy selection
- CLI & workflow API → Pure-HU segmenter: pure-HU branch
- HU volume converter → Pure-HU segmenter: prepared volume
- Adaptive HU estimator → Pure-HU segmenter: adaptive thresholds
- HU threshold ranges → Pure-HU segmenter: fixed thresholds
- CLI & workflow API → TotalSegmentator path: AI branch
- DICOM series loader → TotalSegmentator path: DICOM series
- AI label mappings → TotalSegmentator path: label mappings
- Classic anatomy segmenters → Volume surface extraction: segmentation masks
- Pure-HU segmenter → Volume surface extraction: structure masks
- TotalSegmentator path → Label-to-mesh conversion: segmentation labels
- Volume surface extraction → Mesh post-processing: surfaces
- Label-to-mesh conversion → Mesh post-processing: organ meshes
- Mesh post-processing → Confidence grading: validated meshes
- Confidence grading → STL / OBJ export: graded meshes
- Adaptive HU estimator → Clinical findings analysis: histogram analysis
- Clinical findings analysis → Optional AI findings: optional AI analysis
- Confidence grading → JSONL audit log: metrics and grade
- STL / OBJ export → JSONL audit log: artifact outcome
- STL / OBJ export → Optional cloud uploaders: optional upload
- CLI & workflow API → JSONL audit log: parameters and failures
