happyin.work / blog

English · Русский · Español · Deutsch · Português (Brasil) · 简体中文 · 日本語 · Français · Italiano · Polski · Nederlands · Srpski

ComfyUI Canny detail mask: denoise the background, keep the texture

By Anastasiia Butova, ComfyUI and diffusion-model engineer, Belgrade · published 30 October 2026

Basically, any photo editing boils down to one question: what in this frame can't be messed up? The hairs on a butterfly's wing, the precise edges of gemstones, feathers, wrinkles on a face, yada yada yada: denoise massacres them, diffusion redraws them, an upscaler blurs them. The background, however, is a legitimate free-for-all. Mess with it all you want.

The Cross of Lothair from the Aachen Cathedral Treasury, cut diagonally: the photo on one side, the node's heat map on the other. Gems, filigree and the setting run hot; the plain background stays cold.

Canny edge density as a detail mask

Usually this task goes to AI: segmentation, attention maps, detection. We don't need any of that. Detail in a photo gives itself away, because it has a lot of contours, so it is enough to find where the contours are dense.

Imagine you're looking for your keys in a big room. You wouldn't search the empty floor or the walls. The keys are much more likely to be on a cluttered table, because that's where the stuff is. So you ignore the blank walls and go straight for the table. That's exactly what our node does.

A Bronze Age bead necklace: the photo on the left; on the right only the detail zone stays lit. The zone covers 13% of the frame and holds 99.2% of the strong contours.

Edge density heat map: how the node works

First pass. We shrink large frames: above 1500 px to 70%, above 4000 px by half, above 5000 px to a third. On the reduced copy we run Canny with high thresholds, 100/200. High thresholds catch clear edges instead of sensor noise. Fragments smaller than 5 pixels are discarded.

Heat map. We scale the first-pass contours back to the original size and blur them with a 15×15 Gaussian. Where there are many lines, they merge into a hot spot; where there is a single line, only a faint trace remains. We normalise to 0–255, and that leaves a ready map of detail density.

A late 16th-century gold chain with gemstones and enamel from the Royal Armoury in Stockholm: the photo on the left, the heat map of contour density on the right.

Mask. Everything warmer than 30 counts as the detail area. We clean it with a 3×3 opening and remove islands smaller than 500 pixels. That is the "where to look" mask.

Second pass. The same Canny 100/200, but now at full resolution and only inside the mask. Here we get the real contours, with nothing lost to downscaling.

Third pass: the hottest zones. Where the map is warmer than 150, there is so much detail that standard thresholds are not enough. There we run Canny with thresholds of 5/20 and catch the finest lines. Outside the hot zones such thresholds would produce a lot of noise, so we never apply them there.

At the end all three passes are merged into one contour map. No models, no weights, just OpenCV.

Here is the whole thing step by step on the Essen crown from the Essen Cathedral Treasury: first Canny roughly shows the areas of interest, then we refine the contours inside those areas.

The filtering step by step on the Essen crown: photo, coarse look, heat map, zone of interest, refine, hot zones, result.

The same steps on a 1:1 crop of the crown.

What denoise does to texture, and how the mask keeps it

Denoising often smooths and erases detail. A strong denoise cleans up the background, but every fine detail evaporates along with the noise. A mild denoise preserves the detail, but the noise doesn't get properly cleaned up either. You want both at once: a strong denoise on the background, and as much mercy as possible for the areas with detail.

This is where the mask comes in. We build it from the noisy frame, apply a light denoise inside the mask and a strong one outside, with a soft edge between them. We added Gaussian noise σ = 12 to five photos and compared the results with the clean original:

Photo Texture: strong denoise weak denoise strong + mask Background: strong strong + mask
Crown 25.91 28.00 29.26 34.97 35.01
Clock mechanism 28.34 28.10 30.05 34.02 34.11
Owl 26.83 29.14 29.99 39.45 39.45
Owl, second photo 24.86 28.04 28.85 35.20 35.27
Butterfly 25.67 27.50 28.82 39.73 39.72

This is PSNR in decibels: higher means closer to the original. With the mask the texture is better in all five cases, better even than with the light denoise, and the background stays as clean as after the strong one. The "texture" and "background" areas for scoring are taken from the clean frame, so they don't depend on the mask.

The table above uses OpenCV's non-local means. For the pictures we ran our own NAFNet denoisers for product shots instead: the strong preset outside the mask and the delicate one inside it, the same σ = 12 noise. On metal the mask again keeps the texture that the strong denoise takes away: texture PSNR rises from 22.05 to 24.48 dB on the Cross of Lothair and from 21.29 to 23.33 dB on the Essen crown, with the background unchanged (36.6 and 36.5 dB). On the gold chain it did not help (25.93 against 25.61 dB), so we don't show it.

A 1:1 crop of the Cross of Lothair: clean, noisy, strong denoise, strong denoise with the mask.

The same comparison on the Essen crown.

How it turned out

We took seven freely available images from Wikimedia Commons and ran them through the node as they were, without any adjustments. For comparison we ran the standard Canny 100/200 over the whole frame and checked what share of its contours fell inside the mask.

Picture Share of frame in the mask Contours kept
Necklace made of rough diamonds 7.0% 97.7%
Crown of Margaret of York 17.9% 97.9%
Owl 28.7% 97.3%
Butterfly 12.6% 94.8%
Clockwork mechanism 20.4% 93.4%
West front of Wells Cathedral 39.3% 89.1%
Portrait of an old woman 6.9% 73.9%

On the necklace, seven percent of the frame holds almost all the contours. The remaining 93% can be processed roughly, and no one will notice.

The portrait shows the limit of the method. Fine wrinkles on skin produce sparse single lines; on the heat map they never build up density, so part of the skin stays outside the mask. Keep in mind that for skin, contour density is not the thing that matters.

Are we reinventing the wheel? CannyEdgeMask, Edge Density Analysis, Sobel

We really did check. The individual parts have been known for a long time: blurred Canny as a density map with a threshold, Canny masks in ComfyUI, frequency separation, loss functions weighted by edges, routing of complex patches in ClassSR. ComfyUI already has neighbours too: CannyEdgeMask turns Canny edges into a mask, and Edge Density Analysis draws an edge-density heat map. But we like to think this particular combination is ours: a density map that decides where to look, full-resolution contours inside it, and a fine pass only in the hottest zones. At least we haven't found it put together like that online.

We also compared our map with the Sobel operator and the Laplacian variance on what matters to us: which map better covers the places where a strong denoise would lose detail. At the same area, about 22% of the frame, our mask covers 44.9% of the losses, Sobel 46.2% and the Laplacian 46.4%. So as a zone map we're on par with them, slightly behind, but that's not the point. Our node immediately gives a clean, ready-made mask with no threshold to pick, full-resolution contours, and a separate fine pass. Sobel and the Laplacian give a raw map, and everything after that is done by hand.

Why bother?

This bad boy helps with more than a good denoise. It can, for example, bring detail back from the original: the mask governs where to return it, the contours define what gets returned. The background stays clean and the edges of the gems stay sharp.

It also does a good job as a dataset sifter: from a large frame we crop where it's hot, and no training is wasted on empty sky.

By the way, we turned the thresholds into a slider. The mask grows from the hot spots and gradually spreads to the neighbouring contours. Instead of a rigid "heat 30" and "heat 150", you just slide it to the right, and pixels are added without losses.

Install the Canny detail mask node in ComfyUI

It is one of the nodes we run in production; how 100 custom ComfyUI nodes get there and the rest of our ComfyUI work are on their own pages. We released this one as a separate set of nodes: ComfyUI-Happyin-Open-Nodes. It installs like any other set, through ComfyUI-Manager by link. We checked that it works in ComfyUI 0.3.75, 0.13.0 and 0.16.4. The node is called Happyin Canny.

Happyin Canny in ComfyUI with its six outputs on the crown of Margaret of York.

It has six outputs: the combined contour result, the second and third passes, the cleaned first pass, the detail mask, and a coloured heat map. It comes preconfigured, so there's nothing to tinker with. Try it and see for yourself.

Image credits

The pictures show museum and treasury objects photographed for Wikimedia Commons. Each figure is a derivative processed by the node and keeps the licence of its source:

More posts

Elsewhere on this site

Anastasiia Butova - ML engineer, Belgrade, Serbia. Email [email protected] · LinkedIn · GitHub · Telegram · Hugging Face