ComfyUI > Nodes > ComfyUI-Segformer_Ultra_Fast > Mask To Bbox (SAM2)

ComfyUI Node: Mask To Bbox (SAM2)

Class Name

Mask_To_Bbox_SAM2

Category
Segformer Ultra-Fast/Crop
Author
lihaoyun6 (Account age: 3650days)
Extension
ComfyUI-Segformer_Ultra_Fast
Latest Updated
2025-10-17
Github Stars
0.04K

How to Install ComfyUI-Segformer_Ultra_Fast

Install this extension via the ComfyUI Manager by searching for ComfyUI-Segformer_Ultra_Fast
  • 1. Click the Manager button in the main menu
  • 2. Select Custom Nodes Manager button
  • 3. Enter ComfyUI-Segformer_Ultra_Fast in the search bar
After installation, click the Restart button to restart ComfyUI. Then, manually refresh your browser to clear the cache and access the updated list of nodes.

Visit ComfyUI Online for ready-to-use ComfyUI environment

  • Free trial available
  • 16GB VRAM to 80GB VRAM GPU machines
  • 400+ preloaded models/nodes
  • Freedom to upload custom models/nodes
  • 200+ ready-to-run workflows
  • 100% private workspace with up to 200GB storage
  • Dedicated Support

Run ComfyUI Online

Mask To Bbox (SAM2) Description

Efficiently converts image masks to bounding boxes for object spatial extent identification in image processing tasks.

Mask To Bbox (SAM2):

The Mask_To_Bbox_SAM2 node is designed to efficiently convert a mask, which is a binary or multi-channel image representation, into a bounding box format. This node is particularly useful in image processing and computer vision tasks where identifying the spatial extent of objects within an image is necessary. By transforming masks into bounding boxes, this node allows for a more compact and manageable representation of object locations, which can be beneficial for further processing or analysis. The node is capable of handling both single and batch processing of masks, making it versatile for various applications. It ensures that even if the mask is empty, it returns a minimal bounding box, thus maintaining efficiency and preventing errors in downstream tasks.

Mask To Bbox (SAM2) Input Parameters:

mask

The mask parameter is a tensor representing the binary or multi-channel image mask that you want to convert into bounding boxes. This parameter is crucial as it defines the area of interest within the image. The mask should be a tensor of appropriate dimensions, typically with a shape that matches the image dimensions. The mask can be inverted if needed, which means that the areas of interest are represented by zeros instead of ones. This parameter directly impacts the resulting bounding boxes, as it determines which areas of the image are considered for bounding box extraction.

invert

The invert parameter is a boolean flag that determines whether the mask should be inverted before processing. When set to True, the mask is inverted, meaning that the areas of interest are switched from zeros to ones and vice versa. This can be useful in scenarios where the mask is initially defined in a way that the background is marked with ones and the objects of interest with zeros. The default value is False, meaning no inversion is applied unless specified.

image

The image parameter is an optional tensor that represents the original image from which the mask was derived. This parameter is used to crop the image based on the bounding boxes extracted from the mask. If provided, the node will return a cropped version of the image corresponding to the bounding box of the mask. This can be particularly useful for visualizing the results or for further processing of the specific region of interest within the image.

Mask To Bbox (SAM2) Output Parameters:

bboxes_list

The bboxes_list output is a list of lists, where each inner list represents a bounding box in the format [x1, y1, x2, y2]. These coordinates define the top-left and bottom-right corners of the bounding box, respectively. This output is essential for identifying the spatial extent of objects within the image, allowing for further analysis or processing. The bounding boxes are derived from the non-zero regions of the mask, providing a compact representation of the areas of interest.

cropped_image

The cropped_image output is an optional tensor that represents the portion of the original image that corresponds to the bounding box extracted from the mask. This output is only provided if the image parameter is supplied. It allows for easy visualization and further processing of the specific region of interest within the image. If no bounding box is found, or if the image parameter is not provided, this output will be None.

Mask To Bbox (SAM2) Usage Tips:

  • Ensure that the mask is correctly formatted and aligned with the image dimensions to avoid unexpected results.
  • Use the invert parameter if your mask is defined with the background as ones and objects of interest as zeros.
  • Provide the image parameter if you need to visualize or process the specific region of interest within the image.

Mask To Bbox (SAM2) Common Errors and Solutions:

"Mask is empty, returning empty bbox list."

  • Explanation: This error occurs when the provided mask does not contain any non-zero values, indicating that there are no areas of interest to convert into bounding boxes.
  • Solution: Verify that the mask is correctly generated and aligned with the image. Ensure that the mask accurately represents the areas of interest within the image.

"Dimension mismatch between mask and image."

  • Explanation: This error arises when the dimensions of the mask do not match those of the provided image, which can lead to incorrect bounding box extraction or cropping.
  • Solution: Check that the mask and image have compatible dimensions. If necessary, resize or reshape the mask to match the image dimensions before processing.

Mask To Bbox (SAM2) Related Nodes

Go back to the extension to check out more related nodes.
ComfyUI-Segformer_Ultra_Fast
RunComfy
Copyright 2025 RunComfy. All Rights Reserved.

RunComfy is the premier ComfyUI platform, offering ComfyUI online environment and services, along with ComfyUI workflows featuring stunning visuals. RunComfy also provides AI Models, enabling artists to harness the latest AI tools to create incredible art.