{
  "source_file": "0930",
  "source_platform": "技术平台",
  "author": "Roboflow Blog",
  "title_listed": "How to Use the Segment Anything Model (SAM)",
  "keywords": "SAM,图像分割,Meta",
  "domain": "图像处理",
  "content_type": "实战教程",
  "is_paid_flag": "否",
  "url": "https://blog.roboflow.com/how-to-use-segment-anything-model-sam/",
  "publish_date": "2023-04",
  "corpus_stage": "图像处理",
  "notes": "SAM 分割模型教程",
  "id": "c83a69bbed7b",
  "collected_at": "2026-10-02T15:16:46",
  "title": "How to Use the Segment Anything Model (SAM) | Roboflow",
  "body_markdown": "How to Use the Segment Anything Model (SAM)\nPiotr Skalski\n|\nPublished\nJan 22, 2026\n7\nmin read\nSUMMARY\nMeta AI's Segment Anything Model (SAM) is a zero-shot image segmentation model trained on over 1 billion masks that can identify precise object boundaries without task-specific fine-tuning. Meta AI also released SAM 2 for video and SAM 3 for text-prompted segmentation and tracking. You can run SAM at scale using Roboflow Inference, and Roboflow Annotate's Smart Polygon tool uses SAM to accelerate polygon labeling directly in the browser.\nMeta AI's Segment Anything Model (SAM) is a promptable image segmentation\nmodel\ntrained on over 1 billion masks across 11 million licensed, privacy-respecting images, and its zero-shot performance is often competitive with, or better than, fully supervised models.\nIn this tutorial, I will show you how to use SAM three ways: generating masks for every object in an image automatically, creating segmentation masks from bounding box prompts, and converting an object detection dataset into segmentation masks.\nIf your goal is labeling data rather than writing code,\nRoboflow Annotate uses SAM to power automated polygon labeling\nin the browser, free to try.\nWhat Is Segment Anything?\nSegment Anything (SAM) is an image segmentation model released by Meta AI in April 2023 under an Apache 2.0 license. Given a prompt (a point, a box, or nothing at all), it returns pixel-precise masks for objects it has never been trained to recognize, zero-shot performance that is often competitive with fully supervised models. For the architecture details, read our\nSAM technical deep dive\n.\nSAM is now a family.\nSAM 2\n(July 2024) extends segmentation to video with a streaming memory that tracks objects across frames, and is roughly 6x more accurate than the original on image segmentation.\nSAM 3\n(November 2025) adds concept prompts: give it plain text like helmet or forklift and it detects, segments, and tracks every matching instance across images and video.\nEager to try SAM 3? Drag and drop an image into our interactive playground below with your own text prompt:\nFor more information on how SAM works and the model architecture, read\nour SAM technical deep dive\n.\nBackground: Object Detection vs. Segmentation\nIn object detection tasks, objects are represented by bounding boxes, which are like drawing a rectangle around the object. These rectangles give a general idea of the object's location, but they don't show the exact shape of the object. They may also include parts of the background or other objects inside the rectangle, making it difficult to separate objects from their surroundings.\nSegmentation masks, on the other hand, are like drawing a detailed outline around the object, following its exact shape. This allows for a more\nprecise understanding of the object's shape\n, size, and position.\nTo use Segment Anything on a local machine, we'll follow these steps:\nSet up a Python environment\nLoad the Segment Anything Model (SAM)\nGenerate masks automatically with SAM\nPlot masks onto an image with Supervision\nGenerate bounding boxes from the SAM results\nWhich SAM Should You Use?\nUse the original SAM (this tutorial's code) for local scripting on single images: automatic mask generation, box-prompted masks, and dataset conversion.\nUse SAM 2 when you need masks tracked across video frames.\nUse SAM 3 when you want to find objects by name: text prompts replace manual clicks, and it handles detection, segmentation, and tracking in one model. SAM 3 is\nintegrated across the Roboflow platform\n, including zero-shot use in Workflows and dataset auto-labeling.\nSetting up Your Python Environment\nTo get started, open the Roboflow notebook in\nGoogle Colab\nand ensure you have access to a GPU for faster processing. Next, install the required project dependencies and download the necessary files, including SAM weights.\nShell\nCopy\npip\ninstall\n\\\n'git+https://github.com/facebookresearch/segment-anything.git'\npip\ninstall\n-q\nroboflow\nsupervision\nwget\n-q\n\\\n'https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth'\nLoading the Segment Anything Model\nOnce your environment is set up, load the SAM model into the memory. With multiple modes available for inference, you can use the model to generate masks in various ways. We will explore automated mask generation, generating segmentation masks with bounding boxes, and converting object detection datasets into segmentation masks.\nThe SAM model can be loaded with 3 different encoders: ViT-B, ViT-L, and ViT-H. ViT-H improves substantially over ViT-B but has only marginal gains over ViT-L. These encoders have different parameter counts, with ViT-B having 91M, ViT-L having 308M, and ViT-H having 636M parameters. This difference in size also influences the speed of inference, so keep that in mind when choosing the encoder for your specific use case.\nPython\nCopy\nimport\ntorch\nfrom\nsegment_anything\nimport\nsam_model_registry\nDEVICE\n=\ntorch.device(\n'cuda:0'\nif\ntorch.cuda.is_available()\nelse\n'cpu'\n)\nMODEL_TYPE\n=\n\"vit_h\"\nsam\n=\nsam_model_registry[\nMODEL_TYPE\n](\ncheckpoint\n=\nCHECKPOINT_PATH\n)\nsam.to(\ndevice\n=\nDEVICE\n)\nAutomated Mask (Instance Segmentation) Generation with SAM\nTo generate masks automatically, use the\nSamAutomaticMaskGenerator\n. This utility generates a list of dictionaries describing individual segmentations. Each\ndict\nin the result list has the following format:\nsegmentation\n-\n[np.ndarray]\n- the mask with\n(W, H)\nshape, and\nbool\ntype, where\nW\nand\nH\nare the width and height of the original image, respectively\narea\n-\n[int]\n- the area of the mask in pixels\nbbox\n-\n[List[int]]\n- the boundary box detection in\nxywh\nformat\npredicted_iou\n-\n[float]\n- the model's own prediction for the quality of the mask\npoint_coords\n-\n[List[List[float]]]\n- the sampled input point that generated this mask\nstability_score\n-\n[float]\n- an additional measure of mask quality\ncrop_box\n-\nList[int]\n- the crop of the image used to generate this mask in\nxywh\nformat\nTo run the code below you will need images. You can use your own,\nprogrammatically pull them in from Roboflow\n, or download one of the over 200k datasets available on\nRoboflow Universe\n.\nPython\nCopy\nimport\ncv2\nfrom\nsegment_anything\nimport\nSamAutomaticMaskGenerator\nmask_generator\n=\nSamAutomaticMaskGenerator(sam)\nimage_bgr\n=\ncv2.imread(\nIMAGE_PATH\n)\nimage_rgb\n=\ncv2.cvtColor(image_bgr, cv2.\nCOLOR_BGR2RGB\n)\nresult\n=\nmask_generator.generate(image_rgb)\nThe\nsupervision\npackage (starting from version\n0.5.0\n) provides native support for SAM, making it easier to annotate segmentations on an image.\nPython\nCopy\nimport\nsupervision\nas\nsv\nmask_annotator\n=\nsv.MaskAnnotator(\ncolor_map\n=\n\"index\"\n)\ndetections\n=\nsv.Detections.from_sam(result)\nannotated_image\n=\nmask_annotator.annotate(image_bgr, detections)\nFigure showing the original (left) and segmented (right) image.\nFigure showing all obtained segmentations separately.\nGenerate Segmentation Mask with Bounding Box\nNow that you know how to generate a mask for all objects in an image, let’s see how you can use a bounding box to focus SAM on a specific portion of your image.\nTo extract masks related to specific areas of an image, import the\nSamPredictor\nand pass your bounding box through the mask predictor’s\npredict\nmethod. Note that the mask predictor has a different output format than the automated mask generator. The bounding box format for the SAM model should be in the form of\n[x_min, y_min, x_max, y_max]\nnp.array\n.\nPython\nCopy\nimport\ncv2\nfrom\nsegment_anything\nimport\nSamPredictor\nmask_predictor\n=\nSamPredictor(sam)\nimage_bgr\n=\ncv2.imread(\nIMAGE_PATH\n)\nimage_rgb\n=\ncv2.cvtColor(image_bgr, cv2.\nCOLOR_BGR2RGB\n)\nmask_predictor.set_image(image_rgb)\nbox\n=\nnp.array([\n70\n,\n247\n,\n626\n,\n926\n])\nmasks, scores, logits\n=\nmask_predictor.predict(\nbox\n=\nbox,\nmultimask_output\n=\nTrue\n)\nFigure showing the original image with bounding box (left) and segmented (right) image.\nConvert a Detection Dataset into a Segmentation Dataset\nIf you have an object detection dataset, SAM can upgrade every bounding box into a pixel-precise mask: load the COCO-format annotations, run each box through the predictor, and save the resulting masks.\nThe\ncompanion notebook\nincludes the full conversion code. This turns labeling work you have already done into training data for segmentation models without redrawing a single polygon.\nLabel Data with SAM in the Browser\nFor dataset labeling, you do not need to run SAM yourself.\nRoboflow Annotate's Smart Polygon\nuses SAM to convert clicks into precise polygons in the browser, and\nSAM 3 auto-labeling\ngoes further: describe your classes in text and it labels entire datasets, with your review replacing manual drawing.\nRun SAM at Scale\nRoboflow Inference\n, the open source inference server, serves the whole family on your own hardware or in the cloud:\nSAM\n,\nSAM 2\n, and\nSAM 3\n. In\nRoboflow Workflows\n, SAM 3 runs zero-shot from a text prompt and chains with detection models, filters, tracking, and notifications into full applications.\nFrom SAM Masks to a Production Model\nSAM models are large and general; production lines usually want small and specific. The standard pattern pairs them: use SAM 3 to auto-label a segmentation dataset for your classes, then train\nRF-DETR Seg\n, Roboflow's real-time segmentation architecture, on the result. You get SAM-quality masks at a fraction of the inference cost, in a model you own under a permissive license, deployable to the edge.\nDeploy Segment Anything at Scale\nYou can run Segment Anything (SAM) on your own hardware, at scale, using Roboflow Inference. Roboflow Inference is an inference server through which you can run fine-tuned models such as\nRF-DETR\n, as well as foundation models like SAM and CLIP.\nLearn how to get started with the Inference Segment Anything quickstart.\nIs Segment Anything free for commercial use?\nYes. SAM's code and weights are released under the Apache 2.0 license. Check the license terms of SAM 2 and SAM 3 individually, as Meta's release terms vary by model.\nWhat is the difference between SAM, SAM 2, and SAM 3?\nSAM segments images from point and box prompts. SAM 2 adds video with streaming memory that tracks masks across frames. SAM 3 adds text prompts, finding, segmenting, and tracking every instance of a named concept, which removes the need to click each object.\nDoes SAM tell me what the objects are?\nNo. SAM produces masks without class labels. Pair it with a detection model that supplies labeled boxes as prompts, or use SAM 3, where the text prompt itself carries the class.\nCan I fine-tune Segment Anything?\nSAM 3 can be fine-tuned on your own data in Roboflow (available on every plan, including the Free Tier). For most tasks, the faster route is using SAM 3 to label data and training a compact model like RF-DETR Seg on the output.\nDo I need a GPU to run SAM?\nFor interactive local use, a GPU is strongly recommended, especially with the ViT-H encoder. Without one, use the browser-based Smart Polygon labeling or the hosted endpoints through Roboflow Inference.",
  "metadata": {
    "title": "How to Use the Segment Anything Model (SAM) | Roboflow",
    "description": "How to use the Segment Anything Model: auto-generate masks, segment from box prompts, convert detection datasets, and scale with SAM 2 and SAM 3.",
    "keywords": null,
    "author": null,
    "og:title": "How to Use the Segment Anything Model (SAM)",
    "og:description": "How to use the Segment Anything Model: auto-generate masks, segment from box prompts, convert detection datasets, and scale with SAM 2 and SAM 3.",
    "og:url": "https://roboflow.com/blog/how-to-use-segment-anything-model-sam",
    "og:site_name": "Roboflow",
    "og:image": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/2024/04/image-444.webp",
    "og:type": "article",
    "twitter:card": "summary_large_image",
    "twitter:title": "How to Use the Segment Anything Model (SAM)",
    "twitter:description": "How to use the Segment Anything Model: auto-generate masks, segment from box prompts, convert detection datasets, and scale with SAM 2 and SAM 3.",
    "twitter:image": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/2024/04/image-444.webp",
    "article:published_time": "2026-01-22T16:40:00.000+00:00",
    "article:modified_time": "2026-09-24T17:21:46.000+00:00"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/c83a69bbed7b/code/body-000.txt",
      "chars": 193,
      "language": "text"
    },
    {
      "file": "attachments/c83a69bbed7b/code/body-001.txt",
      "chars": 242,
      "language": "text"
    },
    {
      "file": "attachments/c83a69bbed7b/code/body-002.txt",
      "chars": 249,
      "language": "text"
    },
    {
      "file": "attachments/c83a69bbed7b/code/body-003.txt",
      "chars": 189,
      "language": "text"
    },
    {
      "file": "attachments/c83a69bbed7b/code/body-004.txt",
      "chars": 341,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w3840/format/webp/2022/11/photo_2022-11-17-18.22.41.jpeg",
      "bytes": 15300,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/000.jpeg",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w3840/format/webp/2022/11/photo_2022-11-17-18.22.41.jpeg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w3840/2024/04/image-444.webp",
      "bytes": 98334,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/015.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w3840/2024/04/image-444.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-450.webp",
      "bytes": 62974,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/023.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-450.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-456.webp",
      "bytes": 45604,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/026.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-456.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-459.webp",
      "bytes": 49230,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/029.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-459.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-464.webp",
      "bytes": 60994,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/032.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-464.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-469.webp",
      "bytes": 20856,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/035.webp",
      "src": "https://storage.ghost.io/c/2c/8d/2c8d8c0d-1c15-4b6d-825e-02b78d61d40a/content/images/size/w1000/2024/04/image-469.webp",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://i.ytimg.com/vi/oEQYStnF2l8/maxresdefault.jpg",
      "bytes": 122003,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/038.jpg",
      "src": "https://i.ytimg.com/vi/oEQYStnF2l8/maxresdefault.jpg",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://i.ytimg.com/vi/D-D6ZmadzPE/maxresdefault.jpg",
      "bytes": 142337,
      "reused": true,
      "local_path": "attachments/c83a69bbed7b/images/039.jpg",
      "src": "https://i.ytimg.com/vi/D-D6ZmadzPE/maxresdefault.jpg",
      "alt": "",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [
    {
      "url": "https://www.youtube.com/watch?v=oEQYStnF2l8",
      "src": "https://www.youtube.com/watch?v=oEQYStnF2l8",
      "status": "ok",
      "kind": "single_video",
      "local_path": "attachments/c83a69bbed7b/videos/repair-oEQYStnF2l8.mp4",
      "bytes": 372627811,
      "valid": true,
      "height": 1080,
      "width": 1920,
      "duration": "1482.741000",
      "reused": true,
      "reused_from": "xinxing-zimeiti-corpus/attachments/e40f797fe91d/videos/oEQYStnF2l8.mp4"
    },
    {
      "url": "https://www.youtube.com/watch?v=D-D6ZmadzPE",
      "src": "https://www.youtube.com/watch?v=D-D6ZmadzPE",
      "status": "ok",
      "kind": "single_video",
      "local_path": "attachments/c83a69bbed7b/videos/repair-D-D6ZmadzPE.mp4",
      "bytes": 124203924,
      "valid": true,
      "height": 1080,
      "width": 1920,
      "duration": "774.721000",
      "reused": true,
      "reused_from": "xinxing-zimeiti-corpus/attachments/e40f797fe91d/videos/D-D6ZmadzPE.mp4"
    }
  ],
  "links": [
    {
      "href": "https://roboflow.com/blog/author/skalskip",
      "label": "Piotr Skalski",
      "type": "a"
    },
    {
      "href": "https://github.com/facebookresearch/segment-anything?ref=blog.roboflow.com",
      "label": "model",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/label-data-segment-anything-model-sam/",
      "label": "Roboflow Annotate uses SAM to power automated polygon labeling",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/segment-anything-breakdown/",
      "label": "SAM technical deep dive",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/what-is-segment-anything-2/",
      "label": "SAM 2",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/what-is-sam3/",
      "label": "SAM 3",
      "type": "a"
    },
    {
      "href": "https://rf-model-embed.vercel.app/what-is-sam3",
      "label": "",
      "type": "iframe"
    },
    {
      "href": "https://roboflow.com/ai/stent-inspection?ref=blog.roboflow.com",
      "label": "precise understanding of the object's shape",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/sam3/",
      "label": "integrated across the Roboflow platform",
      "type": "a"
    },
    {
      "href": "https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/how-to-segment-anything-with-sam.ipynb?ref=blog.roboflow.com",
      "label": "Google Colab",
      "type": "a"
    },
    {
      "href": "https://universe.roboflow.com/?ref=blog.roboflow.com",
      "label": "Roboflow Universe",
      "type": "a"
    },
    {
      "href": "https://github.com/roboflow/supervision?ref=blog.roboflow.com",
      "label": "supervision",
      "type": "a"
    },
    {
      "href": "https://www.youtube.com/watch?v=oEQYStnF2l8",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/grow-vision-datasets-with-sam-3/",
      "label": "SAM 3 auto-labeling",
      "type": "a"
    },
    {
      "href": "https://github.com/roboflow/inference?ref=blog.roboflow.com",
      "label": "Roboflow Inference",
      "type": "a"
    },
    {
      "href": "https://docs.roboflow.com/models/supported-models/sam?ref=blog.roboflow.com",
      "label": "SAM",
      "type": "a"
    },
    {
      "href": "https://docs.roboflow.com/models/supported-models/sam2?ref=blog.roboflow.com",
      "label": "SAM 2",
      "type": "a"
    },
    {
      "href": "https://docs.roboflow.com/models/supported-models/sam3?ref=blog.roboflow.com",
      "label": "SAM 3",
      "type": "a"
    },
    {
      "href": "https://roboflow.com/workflows/build?ref=blog.roboflow.com",
      "label": "Roboflow Workflows",
      "type": "a"
    },
    {
      "href": "https://blog.roboflow.com/rf-detr-segmentation-preview/",
      "label": "RF-DETR Seg",
      "type": "a"
    },
    {
      "href": "https://rfdetr.roboflow.com/latest/?ref=blog.roboflow.com",
      "label": "RF-DETR",
      "type": "a"
    },
    {
      "href": "https://www.youtube.com/watch?v=D-D6ZmadzPE",
      "label": "",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:53:11Z",
  "source_url": "https://blog.roboflow.com/how-to-use-segment-anything-model-sam/",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 11018,
  "expected_body_images": 9,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/c83a69bbed7b/pages/source.html",
  "local_html": "attachments/c83a69bbed7b/pages/article.html",
  "body_base_path": "attachments/c83a69bbed7b/pages/",
  "body_markdown_path": "attachments/c83a69bbed7b/pages/article.md"
}