Weld ROI Finder automatically detects and crops the weld seam region (ROI) from radiographic images, with a preference for rotated bounding boxes aligned to the weld axis. The application is built on FastAPI and provides REST endpoints for processing single images, folders, or file paths.
In radiographic weld inspection, the spatial relationship between defects and the weld seam is a critical diagnostic factor. According to international standards (ISO 5817, ASME B31.3), the position of a discontinuity—whether at the weld root, toe, or within the heat-affected zone (HAZ)—determines its classification, root cause, and acceptance level.
While numerous studies have focused on weld defect segmentation and classification, the explicit localization and standardization of the weld seam region (ROI) have not been systematically addressed in recent literature. Most published works assume that the weld ROI has already been manually cropped or predefined, leaving a gap in fully automated and reproducible NDT pipelines.
Weld ROI Finder bridges this gap by introducing an automated ROI extraction stage before defect analysis. This preprocessing ensures that downstream AI models interpret discontinuities within the correct structural context—reducing false detections caused by background noise or non-weld areas.
By standardizing weld ROI extraction, this project lays the foundation for context-aware defect recognition and future research in automated radiographic weld inspection.
- Apply histogram stretching to enhance contrast.
- Perform adaptive denoising, mild vignette, and edge-softening to suppress fine noise.
- Use the Automatic Mask Generator from Meta SAM 2.1 to create candidate masks on the preprocessed image.
- Compute edge density from Canny (curves-only) filters (ignores straight edges).
- Compare luminance of each mask against its surroundings.
- Evaluate mask size & continuity — discard small or fragmented areas.
- Remove masks overlapping text regions detected by RapidOCR.
- Filter isolated micro-components (area < 200 px, distance > 1 px).
- Combine edge, luminance, and size scores → select Best (Geo) as the weld ROI candidate.
- Use OpenCV minAreaRect to find the minimum-area rotated rectangle.
- Apply perspective transform to align ROI along the weld axis.
- If rotation fails, revert to an upright bounding box.
The evaluation was conducted on the GDXray Welds dataset, which contains 68 radiographic images of welded joints with varying thickness, exposure, and background conditions. Out of 68 samples, 63 images were correctly segmented — achieving reliable ROI extraction performance across different weld geometries and brightness levels. Among the 5 unsuccessful cases:
- 3 images were misidentified due to circular pipe boundaries being falsely detected as weld seams.
- 1 image was too faint, making the weld region invisible even to manual inspection.
- 1 image contained a clearly visible weld, but SAM failed to segment.
All endpoints are documented at /docs (Swagger UI).
Method Endpoint Description POST /segment Accepts an uploaded image (multipart) and returns JSON with overlay and mask statistics. GET /segment/from_path Processes an image from a local path and returns results similar to /segment. GET /segment/batch Runs batch segmentation on a folder and returns summarized results. POST /segment/overlay Returns a PNG overlay instead of JSON.
Key fields in JSON response
- overlay_png_b64, overlay_best_geo_png_b64, overlay_roi_rotated_png_b64, etc.
- stats_rows: per-mask metrics (weld_score, geo_score, area, bbox …).
- rapidocr: number of detected / filtered text regions.
Requirements: Python 3.10 – 3.12 (Windows / Linux). GPU/CUDA optional.
<workspace_root>/
├─ sam2/ # facebookresearch/sam2 repository
└─ WeldROIFinder/
├─ weld_roi_finder.py
├─ README.md
└─ web/
Quick Setup (PowerShell) python -m venv .venv ..venv\Scripts\Activate.ps1
cd sam2 pip install -e . cd ..
pip install fastapi "uvicorn[standard]" opencv-python pillow numpy pip install rapidocr # optional OCR filter Optional GPU/CUDA: pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
Default SAM 2 config and checkpoint
- sam2/configs/sam2.1/sam2.1_hiera_l.yaml
- sam2/checkpoints/sam2.1_hiera_large.pt
Environment variables:
- SAM2_CONFIG=path/to/config.yaml
- SAM2_CKPT=path/to/checkpoint.pt
- SAM2_DEVICE=cuda|cpu # default: cuda if available
- uvicorn WeldROIFinder.weld_roi_finder:app --host 0.0.0.0 --port 8000 --reload
Open in browser:
- Web UI: http://localhost:8000/web/
- API Docs: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
Example calls:
- curl -X POST "http://localhost:8000/segment" -F "file=@images/sample.jpg" > result.json
- curl "http://localhost:8000/segment/from_path?path=images/sample.jpg"
- No GPU? → Set SAM2_DEVICE=cpu.
- RapidOCR not installed → text filtering disabled (by default safe).
- OpenCV missing → falls back to upright bbox instead of rotated.
- Ensure folder structure: sam2/ and WeldROIFinder/ are siblings.
- SAM 2 — Facebook Research (Meta); see license in sam2/.
- Weld ROI Finder — uses FastAPI, OpenCV, NumPy, Pillow (+ optional RapidOCR).
- Author: Long Phan
- Email: longpxt@gmail.com - longphan@ieee.org
- Main Contributor: Long Phan (algorithm design, development & documentation).
- Contributions and feedback are welcome via pull requests or GitHub issues.
If you use this work in academic research, please cite:
- Phan, L. (2025). Weld ROI Finder: Automatic Localization of Weld Seam Regions in Radiographic Images for NDT.
- GitHub Repository: https://github.com/longpxt/WeldROIFinder





