KJNodes/maskinggenerated

Batch Crop From Mask Advanced(BatchCropFromMaskAdvanced)

This node takes an image (IMAGE) and a mask (MASK) as input, determining the cropping area by calculating the boundaries of non-zero pixels within the mask.

Batch Crop From Mask Advanced

original_images
masks
original_images
cropped_images
cropped_masks
combined_crop_image
combined_crop_masks
bboxes
combined_bounding_box
bbox_width
bbox_height
crop_size_mult
1.00
bbox_smooth_alpha
0.50
KJNodes

The Batch Crop From Mask Advanced node takes images (IMAGE) and masks (MASK) as input and determines the cropping area by calculating the bounding box of non-zero pixels within the mask.

Batch Crop From Mask Advanced is an advanced image cropping node capable of automatically identifying and batch-cropping target regions from original images based on provided masks. Its core function is to precisely extract the regions of interest defined by the masks from a set of images, outputting the cropped images and masks along with relevant bounding box information.

Node Functionality

This node receives images (IMAGE) and masks (MASK) as input and determines the cropping area by calculating the bounding box of non-zero pixels within the mask. It supports batch processing and incorporates a bounding box smoothing mechanism. The bbox_smooth_alpha parameter adjusts the transition smoothness between the bounding box position and size of the current frame and the previous frame, which is particularly useful for maintaining cropping stability when processing video or image sequences. Additionally, the crop_size_mult parameter allows users to proportionally enlarge the cropping area, ensuring the target object is fully contained.

Node Parameter Description - Batch Crop From Mask Advanced

Connection Inputs (Inputs)

Parameter NameData TypeRequiredDefault ValueRange/OptionsDescription
original_imagesIMAGEYes--original_images (Input Parameter): The original images from which the target regions will be cropped. These are passed through unchanged to the output.
masksMASKYes--masks (Input Parameter): Inputs the mask sequence corresponding to the original images, used to define the target region to be cropped in each image.

Control Parameters (Parameters)

Parameter NameData TypeRequiredDefault ValueRange/OptionsDescription
crop_size_multFLOATYes1.00.0 - 10.0 (Step: 0.01)crop_size_mult (Input Parameter): Crop size multiplier, used to proportionally enlarge the original bounding box calculated from the mask. Range 0.0 to 10.0, step 0.01.
bbox_smooth_alphaFLOATYes0.50.0 - 1.0 (Step: 0.01)bbox_smooth_alpha (Input Parameter): Bounding box smoothing coefficient, used to smooth changes in bounding box position and size between consecutive image frames. Range 0.0 to 1.0, step 0.01.

Output

Parameter NameData TypeDescription
original_imagesIMAGEoriginal_images (Output Parameter): Outputs the input original images unchanged, facilitating workflow chaining.
cropped_imagesIMAGEcropped_images (Output Parameter): Outputs the images cropped according to each mask.
cropped_masksMASKMasks cropped to the same region.
combined_crop_imageIMAGEcombined_crop_image (Output Parameter): Outputs a single image formed by stitching together all cropped images.
combined_crop_masksMASKcombined_crop_masks (Output Parameter): Outputs a single mask formed by stitching together all cropped masks.
bboxesBBOXbboxes (Output Parameter): Outputs a list of bounding box coordinates calculated for each input image.
combined_bounding_boxBBOXcombined_bounding_box (Output Parameter): Outputs an overall bounding box that can enclose all individual bounding boxes.
bbox_widthINTbbox_width (Output Parameter): Outputs the width value of the combined_bounding_box.
bbox_heightINTbbox_height (Output Parameter): Outputs the height value of the combined_bounding_box.

Usage Scenarios

In practical applications, this node is commonly used in workflows that require extracting specific objects from a series of images for subsequent processing. For example, when creating character animations or product showcases, you can first use a segmentation model to generate masks of the target object, then feed the original images and masks into this node to obtain a set of uniformly sized cropped images containing only that object. These images can then be conveniently passed to other nodes for repainting, style transfer, or compositing.

Notes

Please note that the input images and masks must match in batch size. If a mask is completely empty (i.e., has no non-zero pixels), the node will return an empty cropping area. The smoothing parameter is only significantly effective when processing consecutive frames (e.g., video); its effect is limited for single or unordered images.

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

Comments

Sign in with GitHub to join the discussion.

Loading comments…