SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

Image Editing with ComfyUI Standard Nodes + α! How to Use BiRefNet, Depth-Anything-V3, and Lama Remover [Image Generation AI]

Introduction

Hello, I'm Kimama / Easygoing.

In this video, I will introduce image editing using ComfyUI standard nodes with a few custom nodes added.

Illustration of an anime girl with brown hair against a background of harbor fireworks
Harbor fireworks

SAM 3.1 is a general-purpose model

Last time, I introduced background removal using the SAM 3.1 model.

The SAM 3.1 model is a general-purpose model that can detect anything if you provide text input.

How to use the SAM 3.1 model

General-purpose models are very convenient because they can recognize any subject, but when limited to the purpose of foreground and background segmentation, using a specialized model provides better accuracy.

So first, I will introduce how to use the BiRefNet model, which can be used with ComfyUI standard nodes.

BiRefNet is a specialized model for background removal!

BiRefNet is an AI model specialized for binary image segmentation (separating the foreground from the background).

There are several types in the BiRefNet model series, but the orthodox BiRefNet model is officially distributed by Comfy Org , so we will use that one this time.

Workflow

ComfyUI workflow diagram for background removal using BiRefNet

Model

Installation location for birefnet.safetensors

  • ComfyUI\models\background_removal (create a new folder)

Custom Nodes

Screenshot of the screen searching for easygoing in ComfyUI Nodes Manager
Search for easygoing in Nodes Manager

Background removal with BiRefNet

Now, let's compare background removal between SAM 3.1 and BiRefNet.

Input image

Input image of an anime girl with fireworks in the background

Background removal with SAM 3.1

Background removal result by SAM 3.1 (mask and cutout)

Background removal with BiRefNet

Background removal result by BiRefNet (accurate details)

Compared to SAM 3.1, you can see that BiRefNet's background removal accurately classifies details such as hair.

BiRefNet boundaries are jagged

Next, let's zoom in further on the boundary area of BiRefNet.

Enlarged view of BiRefNet

Enlarged view of the BiRefNet mask boundary (jagged edges visible)

When you zoom in on the BiRefNet detection, the thin hair parts appear jagged and look broken.

In the ComfyUI implementation, the BiRefNet model processes at a fixed resolution of 1024 x 1024, so for high-resolution illustrations, the resolution is insufficient, resulting in this appearance.

Include semi-transparent parts in the mask area

Now, to ensure the hair is reliably included in the foreground without being cut off, I will include semi-transparent parts determined by BiRefNet between the foreground and background entirely in the foreground.

Connect the mask created by BiRefNet to the Threshold Mask node and set the Threshold to the minimum value of 0.01.

Threshold Mask = 0.01

Mask with Threshold set to 0.01 and background removal result

Compared to before, the hair is now firmly included in the foreground, but now thin background has crept into the boundary area.

Adjust the mask area with the Mask Refine node!

From here, I will further adjust the mask area using a custom node.

ComfyUI-AutoMask's Mask Refine node is a custom node that compares the original illustration with the mask using a method called pymatting to adjust the mask area.

Adjusting boundaries with the Mask Refine node

Clean mask boundaries and cropped image after applying the Mask Refine node

Using the Mask Refine node, I was able to correct the mask boundaries quite cleanly.

On the other hand, since the Mask Refine node adjusts the boundaries after applying a blur, it has the drawback that thin parts of the hair become faint and the "corners" surrounded by the mask become rounded.

Every node that automatically adjusts masks has its strengths and weaknesses, so it is best to use them selectively depending on the case.

There are also subjects that BiRefNet is not good at

BiRefNet mainly separates the foreground and background based on color and texture, but if there is a similar structure in the boundary area, the separation accuracy decreases.

I will try the previous workflow with a different illustration.

Input Image 2

Input image 2 of an anime girl with fireworks and the sea in the background

BiRefNet → Mask Threshold → Mask Refine

Mask result where a small area of the sea surrounded by fireworks and hair was incorrectly detected

In this illustration, part of the fireworks that are similar in color and brightness to the hair were incorrectly identified as the foreground.

Also, the area of the small patch of sea surrounded by hair in the bottom right was not recognized as the background and was included in the foreground.

Therefore, I will consider a method to improve the separation accuracy using a different model.

Depth-Anything-V3 estimates depth.

Depth-Anything-V3 is a model released by ByteDance in China that performs monocular depth estimation (estimating depth from a single illustration).

To perform depth estimation accurately, multiple lenses or more are typically required as input, but the Depth-Anything-V3 model can perform depth estimation with considerable accuracy even with a single lens by training on a large number of images.

The Depth-Anything-V3 model can also be used with ComfyUI standard nodes, just like the BiRefNet mentioned earlier.

Let's try using the Depth-Anything-V3 model!

Now, let's actually perform depth estimation using the Depth-Anything-V3 model.

Workflow

ComfyUI workflow diagram for Depth-Anything-V3 depth estimation

Model

Installation folder

  • ComfyUI\models\geometry_estimation (create a new folder)

Monocular depth estimation with Depth-Anything-V3

Original image and the depth map obtained with Depth-Anything-V3
Original image | Depth map (white is foreground, black is background)

When you create a depth map using the Depth-Anything-V3 model, you can see that the character comes to the foreground, and the fireworks and the sea area surrounded by hair are correctly recognized as the background.

Determining the foreground from the depth map

Now, let's actually extract the foreground from the depth map.

Workflow

Foreground extraction workflow combining BiRefNet and Depth

Model

Custom Nodes

First, we remove the area detected by BiRefNet from the depth map created by Depth-Anything-V3, masking the unnecessary parts in black and cropping it.

Image with unnecessary parts cropped out in black from the Depth map
Depth-Anything-V3 | Cropping unnecessary parts in black

Next, since the depth map exists as a gradient from black to white, we look for the threshold to separate the foreground and background.

Convert Image to Mask After converting the depth map to a mask, connect it to the Auto Mask Threshold node.

Mask images of the 8 threshold candidates proposed by Auto Mask Threshold and the cropped illustration
Automatically proposing 8 candidates

The Auto Mask Threshold node analyzes the mask's histogram (frequency distribution) and proposes 8 appropriate threshold candidates.

This time, the 5th output in the center looked good, so I set the threshold to 5 and executed it.

Comparison of cropped images between BiRefNet alone and the combination with Depth
BiRefNet | BiRefNet + Depth-Anything-V3

Finally, adjust the boundaries through Mask Refine.

In this way, by combining BiRefNet and Depth-Anything-V3, I was able to correctly separate the background from the area surrounded by fireworks and hair.

Remove unwanted objects with Lama Remover!

Finally, I will introduce how to use Lama Remover.

Lama Remover is a custom node that allows you to use LaMa, an image inpainting model I introduced previously, in ComfyUI.

About image inpainting with the LaMa model

Workflow

Workflow for removing unwanted objects using SAM 3.1 and Lama Remover

Models

Installation folder for sam3.1_multiplex

  • ComfyUI\models\checkpoints

Installation folder for big-lama.pt

  • ComfyUI\models\lama (create a new folder)

Custom Nodes

Screenshot of the Lama Remover search screen in ComfyUI's Nodes Manager

Detect the target to be removed with SAM 3.1

First, detect the target you want to remove using the SAM 3.1 model.

This time, I will try removing the fireworks and bokeh lighting from the following illustration.

Input Image 3

Input image 3 of an anime girl with silver hair in a blue dress with fireworks and bokeh lighting

Prompt

fireworks:10, bokeh lights:30

When you want to detect multiple objects simultaneously with the SAM 3.1 model, you can write the number of items after the colon, and it will detect up to that many at once.

Mask of fireworks and bokeh lighting detected by SAM 3.1 and the cropped image
Fireworks and bokeh lighting detected by SAM 3.1

Remove unwanted objects with Lama Remover!

Next, connect the original illustration and the mask area to the Big Lama Remover node to remove the target.

Comparison before and after removing fireworks and bokeh lighting with Lama Remover
Original | After Removal

In this way, by combining the two AI models, SAM 3.1 and LaMa, we were able to easily remove unwanted objects.

Since both SAM 3.1 and LaMa are lightweight and fast models, I think they can be very useful in the pre-processing of illustration editing.

Summary: Using AI in combination!

  • BiRefNet separates the background

  • Depth-Anything-V3 estimates depth

  • Combine SAM 3.1 and LaMa to remove unwanted objects

This time, I introduced how to use the SAM 3.1, BiRefNet, Depth-Anything-V3, and LaMa models in ComfyUI.

Today, many AI models are released with open weights, and even just within the technology of segmentation for detecting regions from images, it is impossible to simply determine the superiority of performance.

Comparison video of segmentation models by Bracer Jack

AI models each have their strengths and weaknesses, but by combining multiple models, you can use them to cover each other's shortcomings.

Recently, ComfyUI has officially supported many more models, making it easier to try out new ones, which has opened up infinite possibilities.

Anime illustration of a woman in white clothes smiling at the viewer with fireworks in the background
The possibilities of ComfyUI are endless

Why not take this opportunity to try out some new models yourself?

Thank you for reading until the end!


Update History

2026.8.10

  • Partially updated the workflow


English Article


いいなと思ったら応援しよう!

この記事が参加している募集