waifu2x-ncnn-vulkan icon indicating copy to clipboard operation
waifu2x-ncnn-vulkan copied to clipboard

get fuzzy borders if an input image is of alpha channel

Open Rongronggg9 opened this issue 3 years ago • 3 comments

Original Image waifu2x-ncnn-vulkan waifu2x-caffe
1x / /
2x [cunet, denoise-level 3, TTA] /
2x*2x [cunet, denoise-level 3, TTA] /
Original Image (alpha channel deleted) waifu2x-ncnn-vulkan
1x /
2x [cunet, denoise-level 3, TTA] /
2x*2x [cunet, denoise-level 3, TTA] /

Have tested [cunet, denoise-level -1, no TTA] and got the same fuzzy borders.

Rongronggg9 avatar Apr 29 '21 10:04 Rongronggg9

Still reproducible on release 20220419:

-s 2 -n 3 -x: 2x3n

-s 2 -n 3 -x*2: 2x3n2x3n

Rongronggg9 avatar Jul 24 '22 17:07 Rongronggg9

This is not a request, just information.

waifu2x(web) and waifu2x-caffe use a preprocessing method for padding the borders of the alpha channel. alpha2_preprocess

https://github.com/nagadomi/nunif/blob/eab6952d93e85951ed4e4cff30cd26c09e1dbb63/nunif/utils/render.py#L35 https://github.com/lltcggie/waifu2x-caffe/blob/3812b90f68b33256f040e6c8eafaacebd51490a0/common/stImage.cpp#L167

Also the alpha channel is 2x by the scale2.0 model. This takes more processing time, but it gives better alpha border results, such as for a sprite image with alpha channel in game-dev.

nagadomi avatar Jul 25 '22 16:07 nagadomi

To work around this issue, I've been compositing the original image over two differently-coloured solid backgrounds, upscaling those two alpha-free images separately, then doing some pixel maths on the upscaled images to recover the alpha channel and invert the composition to obtain an upscaled image with alpha. E.g. with imagemagick:

# Compose on two solid backgrounds:
convert orig.png -background green1 -flatten green.png
convert orig.png -background magenta -flatten magenta.png

# Upscale:
waifu2x-ncnn-vulkan -s 2 -n 3 -x -i green.png -o green-2x.png
waifu2x-ncnn-vulkan -s 2 -n 3 -x -i magenta.png -o magenta-2x.png

# Extract alpha
magick green-2x.png magenta-2x.png -compose difference -composite -separate \
  -evaluate-sequence max -auto-level -negate alpha-2x.png

# Invert componsition of both 2x images:
magick green-2x.png alpha-2x.png -alpha Off \
  -fx "v==0 ? 0 : u/v - green1/v + green1" alpha-2x.png -compose Copy_Opacity \
  -composite green-2x-decomposed.png

magick magenta-2x.png alpha-2x.png -alpha Off \
  -fx "v==0 ? 0 : u/v - magenta/v + magenta" alpha-2x.png -compose Copy_Opacity \
  -composite magenta-2x-decomposed.png

# Average the two decomposed images together
convert -average green-2x-decomposed.png magenta-2x-decomposed.png result.png

There is the slightest magenta and green tinge around some of the semitransparent edges, but averaging together the two decomposed images instead of just using one of them improves it, and it's not too bad.

Obviously this takes about twice as long since it has to upscale the image twice, but it's what I'm using for now.

orig.png orig

green.png green

magenta.png magenta

green-2x.png green-2x

magenta-2x.png magenta-2x

green-2x-decomposed.png green-2x-decomposed

magenta-2x-decomposed.png magenta-2x-decomposed

result.png result

chrisjbillington avatar Oct 06 '22 04:10 chrisjbillington