KJNodes/maskinggenerated

Batch Crop From Mask(BatchCropFromMask)

From a technical implementation perspective, this node takes an original image of type `IMAGE` and a mask of type `MASK` as inputs.

Batch Crop From Mask

original_images
masks
original_images
cropped_images
bboxes
width
height
crop_size_mult
1.000
bbox_smooth_alpha
0.50
KJNodes

From a technical implementation perspective, the Batch Crop From Mask node takes the original image of type IMAGE and a mask of type MASK as input.

The Batch Crop From Mask node is a tool for batch image processing. It can automatically identify and crop target regions within images based on provided masks. The core functionality of this node is to analyze the non-zero pixel areas of each mask, calculate the corresponding bounding boxes, and use these bounding boxes to crop the corresponding sub-images from the original images.

Node Functionality

From a technical implementation perspective, this node takes the original image of type IMAGE and a mask of type MASK as input. It iterates through each pair of image and mask, calculating the minimum bounding rectangle based on the coordinates of non-zero pixels in the mask. The node supports smoothing the size of the bounding box. The bbox_smooth_alpha parameter adjusts the blending degree between the current frame's bounding box size and the historical frame's bounding box size, which helps achieve more stable cropping results in sequence processing. The calculated bounding box size is also scaled by multiplying it with the crop_size_mult coefficient to flexibly adjust the final cropping area size.

Node Parameter Description - Batch Crop From Mask

Connection Inputs (Inputs)

Parameter NameData TypeRequiredDefault ValueValue Range/OptionsDescription
original_imagesIMAGEYes--**: The output original image sequence, identical to the input. Type is IMAGE.
masksMASKYes--**: The input mask sequence, used to determine the target area to be cropped from the original image. Non-zero pixel areas in the mask will be calculated as the bounding box. Type is MASK.

Control Parameters (Parameters)

Parameter NameData TypeRequiredDefault ValueValue Range/OptionsDescription
crop_size_multFLOATYes1.00.0 - 10.0 (Step: 0.001)**: Crop size multiplier, used to scale the calculated bounding box size. Value range 0.0 to 10.0, step 0.001.
bbox_smooth_alphaFLOATYes0.50.0 - 1.0 (Step: 0.01)**: Bounding box smoothing coefficient, used to blend the current frame's bounding box size with the previous frame's for smooth transitions. Value range 0.0 to 1.0, step 0.01.

Output

Parameter NameData TypeDescription
original_imagesIMAGE**: The output original image sequence, identical to the input. Type is IMAGE.
cropped_imagesIMAGE**: The output cropped image sequence. Type is IMAGE.
bboxesBBOX**: The output bounding box coordinate sequence, formatted as (x1, y1, x2, y2). Type is BBOX.
widthINT**: The unified width of the output cropped images. Type is INT.
heightINT**: The unified height of the output cropped images. Type is INT.

Usage Scenarios

In practical workflows, this node is commonly used in scenarios requiring focus on specific parts of an image. For example, when creating animations or processing video sequences, you can first use a mask generation node (such as SAM or GroundingDino) to identify the main object in each frame. Then, connect the generated mask sequence and the original image sequence to this node. The node will output the cropped image sequence and the corresponding bounding box coordinates. These outputs can be further used for local inpainting, super-resolution, or as input for other image processing nodes.

The Batch Crop From Mask node is from the ComfyUI-KJNodes node package.

Comments

Sign in with GitHub to join the discussion.

Loading comments…