{"id":40098834,"url":"https://github.com/ymgw55/segment-anything-edge-detection","last_synced_at":"2026-01-19T10:35:59.383Z","repository":{"id":178529668,"uuid":"661960180","full_name":"ymgw55/segment-anything-edge-detection","owner":"ymgw55","description":"Unofficial edge detection implementation using the Automatic Mask Generation (AMG) of the Segment Anything Model (SAM).","archived":false,"fork":false,"pushed_at":"2025-05-26T11:50:34.000Z","size":851,"stargazers_count":64,"open_issues_count":2,"forks_count":6,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-26T12:52:51.995Z","etag":null,"topics":["edge-detection","python","sam","segmentation","zero-shot","zero-shot-edge-detection"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ymgw55.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-07-04T04:12:20.000Z","updated_at":"2025-05-26T11:50:38.000Z","dependencies_parsed_at":"2025-01-30T08:24:40.147Z","dependency_job_id":"f53473c1-d412-410b-a6c7-e47114ea796c","html_url":"https://github.com/ymgw55/segment-anything-edge-detection","commit_stats":null,"previous_names":["ymgw55/segment-anything-edge-detection"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ymgw55/segment-anything-edge-detection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymgw55%2Fsegment-anything-edge-detection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymgw55%2Fsegment-anything-edge-detection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymgw55%2Fsegment-anything-edge-detection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymgw55%2Fsegment-anything-edge-detection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ymgw55","download_url":"https://codeload.github.com/ymgw55/segment-anything-edge-detection/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ymgw55%2Fsegment-anything-edge-detection/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28565590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-19T08:53:44.001Z","status":"ssl_error","status_checked_at":"2026-01-19T08:52:40.245Z","response_time":67,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["edge-detection","python","sam","segmentation","zero-shot","zero-shot-edge-detection"],"created_at":"2026-01-19T10:35:59.307Z","updated_at":"2026-01-19T10:35:59.372Z","avatar_url":"https://github.com/ymgw55.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# segment-anything-edge-detection\n\n\u003cp\u003e\n\u003cimg src='assets/fish.jpg' height=150px /\u003e \u003cimg src='assets/fish_gt.png' height=150px /\u003e \u003cimg src='assets/edge.png' height=150px /\u003e\n\u003c/p\u003e\n\nThis repository provides code for performing edge detection using the Automatic Mask Generation (AMG) of the Segment Anything Model (SAM) [1]. Since the code used in the paper is not currently available to the public, this implementation is based on the descriptions provided in the paper.\n\nThe image on the left is taken from the BSDS. The middle is the ground truth edge. The image on the right is the result of applying edge detection.\n\n---\n\n## Docker\n\nThis repository is intended to be run in a Docker environment. If you are not familiar with Docker, please install the packages listed in [requirements.txt](requirements.txt).\n\n### Docker build\n\nCreate a Docker image as follows:\n\n```bash\n$ bash scripts/docker/build.sh\n```\n\n### Docker run\nRun the Docker container by passing the GPU ID as an argument:\n```bash\n$ bash scripts/docker/run.sh 0\n```\n\n---\n\n## Data\n\n### BSDS500\ndownload BSDS500 [2] dataset from [official site](https://www2.eecs.berkeley.edu/Research/Projects/CS/vision/grouping/resources.html).\n\nIf you cannot download it, the following mirror repositories may be helpful.\n- https://github.com/BIDS/BSDS500\n\nThen prepare the following directory structure:\n\n```bash\ndata/BSDS500/\n    ├── groundTruth\n    │   └── test\n    │       ├── 100007.mat\n    │       ├── 100039.mat\n    │       ...\n    │       \n    └── images\n        ├── test\n        │   ├── 100007.jpg\n        │   ├── 100039.jpg\n        │   ...\n        │\n        ├── train\n        └── val\n```\n\n### NYUDv2\n\ndownload NYUDv2 [3] test dataset from [EDTER](https://github.com/MengyangPu/EDTER).\nThen prepare the following directory structure:\n\n```bash\ndata/NYUDv2/\n    ├── groundTruth\n    │   └── test\n    │       ├── img_5001.mat\n    │       ├── img_5002.mat\n    │       ...\n    │       \n    └── images\n        ├── test\n        │   ├── img_5001.png\n        │   ├── img_5002.png\n        │   ...\n        │\n        ├── train\n        └── val\n```\n\n---\n\n## Model\n\nCreate a directory to download the model as follows:\n\n```bash\nmkdir model\n```\n\n### SAM\n\nDownload the SAM model as follows:\n\n```bash\nwget -P model https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth\n```\n\n### Edge-NMS\n\nIn the original paper [1], Canny edge NMS [4] was used for edge NMS.\nHowever, in our environment, it did not produce the edges reported in the paper. \nTherefore, we temporarily used OpenCV's Structured Forests [5] model for edge NMS.\n\nDownload the Structured Forests model as follows:\n\n```bash\nwget -P model https://cdn.rawgit.com/opencv/opencv_extra/3.3.0/testdata/cv/ximgproc/model.yml.gz\n```\n\n## Prediction\n\nTo generate the image above, do the following:\n```\npython example.py\n```\nThe output result is generated in `output/example`.\n\n\n\nPredict edges as follows:\n\n```bash\npython pipeline.py --dataset BSDS500 --data_split test\n```\n\nOther arguments for initializing `SamAutomaticMaskAndProbabilityGenerator` can be passed as follows.\n\n```bash\n  -h, --help            show this help message and exit\n  --dataset DATASET     BSDS500 or NYUDv2\n  --data_split DATA_SPLIT\n                        train, val, or test\n  --points_per_side POINTS_PER_SIDE\n                        Number of points per side.\n  --points_per_batch POINTS_PER_BATCH\n                        Number of points per batch\n  --pred_iou_thresh PRED_IOU_THRESH\n                        Prediction IOU threshold\n  --stability_score_thresh STABILITY_SCORE_THRESH\n                        Stability score threshold\n  --stability_score_offset STABILITY_SCORE_OFFSET\n                        Stability score offset\n  --box_nms_thresh BOX_NMS_THRESH\n                        NMS threshold for box suppression\n  --crop_n_layers CROP_N_LAYERS\n                        Number of layers to crop\n  --crop_nms_thresh CROP_NMS_THRESH\n                        NMS threshold for cropping\n  --crop_overlap_ratio CROP_OVERLAP_RATIO\n                        Overlap ratio for cropping\n  --crop_n_points_downscale_factor CROP_N_POINTS_DOWNSCALE_FACTOR\n                        Downscale factor for number of points in crop\n  --min_mask_region_area MIN_MASK_REGION_AREA\n                        Minimum mask region area\n  --output_mode OUTPUT_MODE\n                        Output mode of the mask generator\n  --nms_threshold NMS_THRESHOLD\n                        NMS threshold\n  --bzp BZP             boundary zero padding\n  --pred_iou_thresh_filtering\n                        filter by pred_iou_thresh\n  --stability_score_thresh_filtering\n                        filter by stability_score_thresh\n  --kernel_size KERNEL_SIZE\n                        kernel size\n```\n\nSee [6] for more details about boundary zero padding.\n\nThe output result is generated in `output_${dataset}/exp${exp_num}/${data_split}`.\n\n# Evaluation\nWe use [py-bsds500](https://github.com/Britefury/py-bsds500/tree/master) for edge detection. Some bugs have been fixed and ported to the `py-bsds500` directory.\nCompile the extension module with:\n\n```bash\ncd py-bsds500\npython setup.py build_ext --inplace\n```\n\nThen evaluate ODS, OIS, and AP as follows:\n\n```bash\ncd py-bsds500/\npython evaluate_parallel.py ../data/BSDS500 ../output/BSDS500/exp${exp}/ test --max_dist 0.0075\npython evaluate_parallel.py ../data/NYUDv2 ../output/NYUDv2/exp${exp}/ test --max_dist 0.011\n```\n\nNote that following previous works, the localization tolerance is set to 0.0075 for BSDS500 and 0.011 for NYUDv2.\n\n# Todo\n- Since there is a large gap with the original paper in terms of performance, we would like to be able to reproduce the results.\n- A high-performance Cany Edge NMS needs to be implemented to reproduce the settings of the original paper.\n\n## Reference\n\n### Code\n\nThe code in this repository mainly uses code from the following repositories. Thank you.\n- [segment-anything](https://github.com/facebookresearch/segment-anything)\n- [py-bsds500](https://github.com/Britefury/py-bsds500/tree/master)\n- [opencv_contrib](https://github.com/opencv/opencv_contrib)\n\n### Paper\n\n[1] Alexander Kirillov, Eric Mintun, Nikhila Ravi, Hanzi Mao, Chloe Rolland, Laura Gustafson, Tete Xiao, Spencer Whitehead, Alexander C. Berg, Wan-Yen Lo, Piotr Dollar, Ross Girshick. Segment Anything. ICCV 2023.\n\n[2] Pablo Arbelaez, Michael Maire, Charless C. Fowlkes, and Jitendra Malik. Contour detection and hierarchical image segmentation. IEEE Trans. Pattern Anal. Mach. Intell 2011.\n\n[3] Nathan Silberman, Derek Hoiem, Pushmeet Kohli, and Rob Fergus. Indoor segmentation and support inference from RGBD images. ECCV 2012.\n\n[4] John F. Canny. A computational approach to edge detection. IEEE Trans. Pattern Anal. Mach. Intell 1986.\n\n[5] Piotr Dollar and C. Lawrence Zitnick. Fast edge detection using structured forests. IEEE Trans. Pattern Anal. Mach. Intell 2015.\n\n[6] Hiroaki Yamagiwa, Yusuke Takase, Hiroyuki Kambe, and Ryosuke Nakamoto. Zero-Shot Edge Detection With SCESAME: Spectral Clustering-Based Ensemble for Segment Anything Model Estimation. WACV Workshop 2024.\n\n---\n## Related Work\n\nThe following is a list of studies on SAM and edge detection. Please let me know if you would like to add new research.\n\n- Wenya Yang, Xiao-Diao Chen, Wen Wu, Hongshuai Qin, Kangming Yan, Xiaoyang Mao, and Haichuan Song. [Boosting Deep Unsupervised Edge Detection via Segment Anything Model](https://ieeexplore.ieee.org/abstract/document/10490131). IEEE Transactions on Industrial Informatics 2024.\n- Xingchen Li, Yifan Duan, Beibei Wang, Haojie Ren, Guoliang You, Yu Sheng,\nJianmin Ji, and Yanyong Zhang. [EdgeCalib: Multi-Frame Weighted Edge Features for AutomaticTargetless LiDAR-Camera Calibration](https://arxiv.org/abs/2310.16629). arXiv 2023.\n- Hiroaki Yamagiwa, Yusuke Takase, Hiroyuki Kambe, and Ryosuke Nakamoto. [Zero-Shot Edge Detection With SCESAME: Spectral Clustering-Based Ensemble for Segment Anything Model Estimation](https://openaccess.thecvf.com/content/WACV2024W/Pretrain/html/Yamagiwa_Zero-Shot_Edge_Detection_With_SCESAME_Spectral_Clustering-Based_Ensemble_for_Segment_WACVW_2024_paper.html). WACV Workshop 2024.\n\n# Contribution\n\nI may be slow to respond, but everyone is welcome to contribute. ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymgw55%2Fsegment-anything-edge-detection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fymgw55%2Fsegment-anything-edge-detection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fymgw55%2Fsegment-anything-edge-detection/lists"}