Mask Spike Remover V2
The spike remover that's really an ellipse drawer
- mask
- mask
The name undersells this node in a way that'll confuse you if you don't see it coming. "Mask Spike Remover" sounds like a despeckle utility - a node that smooths a mask's rough edges while keeping its shape. It is not that. What MaskSpikeRemoverV2 actually does is look at your mask, figure out roughly where it lives, throw the mask away, and hand you a clean, feathered ellipse drawn around that spot. It's a shape-fit node wearing a despeckler's trench coat.
Once you know that, it's genuinely handy - for a specific job. Masks from SAM, GroundingDINO, matting models, or frame-by-frame rotoscoping come out jagged, spiky, and jittery, with stray specks and holes. When you don't care about the mask's exact shape - just the region it points at - a feathered ellipse that sits in the right place beats a truthful-but-ugly mask every time. Compositing a subject into a scene, making a clean cutout, or driving a video edit are exactly the jobs where that trade is a good one.
How it works
Pure classic CV, no machine learning involved. It erodes, detects edges, and fits a box:
- Binarize the mask (anything above 0.5) and erode it with a square kernel sized from
erode_radius- this chews off the spiky noise. - Run Canny edge detection on the result, then a Hough transform to find straight lines.
- Cluster the lines into near-horizontal and near-vertical groups, keep the dominant ones, and compute their intersections. Those are your corners.
- Draw a filled ellipse inside the bounding box.
- Gaussian-blur it with sigma =
edge_blurfor a feathered edge.
If Hough finds no lines, it falls back to the eroded mask's bounding box, so you still get an ellipse. Either way: one clean ellipse per frame, out.
The inputs that matter
You'll actually touch three of the five:
- erode_radius (default 3.0) - how aggressively spikes get chewed off before the edge search. Raise it for legitimately messy masks; leave it alone for clean ones. At 0, erosion is skipped and spikes can survive into the corner detection.
- min_area (default 100) - regions whose fitted box is smaller than this come back as an empty mask. Raise it to filter out stray detection specks.
- edge_blur (default 3.0) - feathering sigma on the output ellipse. The usual inpainting feather range is 4–12 pixels, so the default is on the conservative side.
The other two exist but you'll rarely touch them. frame_smooth (0 = off) averages the bounding boxes of neighboring frames so the ellipse doesn't jump around in video - genuinely nice for smoothing rotoscoped keyframes. And a small README lie: it claims edge_blur caps at 50, but the code allows 200. Trust the code.
Output is a single MASK at the input resolution, ready to plug into an inpainting/VAE encode, a compositor, or anything else that eats masks.
Install
The easiest install in ComfyUI: one file, no requirements.txt, no model downloads. Copy mask_spike_remover_v2.py into ComfyUI/custom_nodes/ (that's literally the README's instruction), or clone the whole repo there, or grab it via ComfyUI Manager by searching "ComfyUI-MaskSpikeRemover-V2". Restart, then find "Mask Spike Remover V2" in the node search under the mask category. Its dependencies - torch, numpy, cv2 - are all already in a stock ComfyUI install, which ships opencv-python-headless, so there's nothing extra to pip.
Where people get burned
- It will not preserve your mask's shape. Feed it a hand mask for detailed inpainting and you get a blob. Reach for this when a clean ellipse is fine - not when you need fidelity. The name lies.
min_areatoo high silently kills small masks. It emits a zero mask rather than an error, so if your subject vanishes, lower it.- Expect to debug it yourself. It's a tiny single-file node from an individual author with basically no community footprint (zero impressions, no discussion threads to speak of). It's MIT-licensed and readable in under a hundred lines, so if something's off, the code is right there.
For the niche it fills - turning a spiky segmentation mask into a stable, feathered ellipse - this does the job with zero bloat. Just don't expect it to be a general-purpose mask cleaner.
Inputs (5)
| Name | Type | Default | Description |
|---|---|---|---|
| mask | MASK | — | |
| erode_radius | FLOAT | 3.00–20 | Erosion radius to remove spikes |
| min_area | INT | 1000–10000 | Minimum mask area to be considered valid |
| edge_blur | FLOAT | 3.00–200 | Edge blur radius for feathering |
| frame_smooth | INT | 00–30 | Frame smoothing (0 = off) |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| mask | MASK | — |