Log in

View Full Version : YUV to RGB via numpy?


PRAGMA
1st January 2021, 00:37
Is there a lossless way to convert YUV to RGB (with support for both sub-sampled YUV and not).

I'm trying to add YUV support to VSGAN (https://github.com/rlaPHOENiX/VSGAN), with no success. The main issue I'm having is that with sub-sampled YUV, (e.g. 4:2:0).

The Y channel is obviously full resolution, however U and V channels arent. This causes np.dstack to freak out about it, and I haven't been able to find a way to losslessly convert from sub-sampled 4:2:0 to 4:4:4 in a no-interpolation way.

Any advice would be great, as I'd love to be able to use VSGAN on YUV input's (which would be the majority of data I would be sending through VapourSynth, e.g. DVD's, Blu-ray's).

feisty2
1st January 2021, 01:04
Is there a lossless way to convert YUV to RGB (with support for both sub-sampled YUV and not).
a lossless conversion is possible only if the chroma has a top-left placement. in such case you simply interpolate the chroma with a resampling filter that preserves the original pixel at the top-left corner of each 2x2 grid, and interpolates the other 3 spots, you can do that with something like nnedi3(dh=True) or eedi3(dh=True).
but seriously, it makes absolutely no sense to request a lossless conversion since you will be performing some lossy resampling (SRGAN) afterwards anyways. the composite function SRGAN(YUVtoRGB(x)) is no longer lossless even if YUVtoRGB(x) is lossless

PRAGMA
1st January 2021, 01:33
a lossless conversion is possible only if the chroma has a top-left placement. in such case you simply interpolate the chroma with a resampling filter that preserves the original pixel at the top-left corner of each 2x2 grid, and interpolates the other 3 spots, you can do that with something like nnedi3(dh=True) or eedi3(dh=True).
but seriously, it makes absolutely no sense to request a lossless conversion since you will be performing some lossy resampling (SRGAN) afterwards anyways. the composite function SRGAN(YUVtoRGB(x)) is no longer lossless even if YUVtoRGB(x) is lossless

That's totally correct assumption, however my point is to try and provide the most untouched data to the SRGAN, otherwise it may interpolate differently.

feisty2
1st January 2021, 01:43
It makes no difference, SRGAN is unaware of the untouched data and has no specialization for data that went thru a lossless YUVtoRGB conversion

Soulvomit
1st January 2021, 04:32
Not sure how proper this is but you can map and resize each component separately in RGB (UtoY8; VtoY8) then put them back together in YV24 (YtoUV).

_Al_
1st March 2021, 20:42
I come back to this, because I thought you asked about something else. You want just simple YUV to RGB to fill RGB planes?
https://forum.videohelp.com/threads/396140-10-Bit-Pixels/page2#post2575815
rgb was obtained manually to show it is the same as using vapoursynth Point resize function (rgb2). Is it a problem for SRGAN to extrapolate from values obtained like that (YUV420V to RGB)?
So simple: rgb = core.resize.Point(YUV420clip, format = vs.RGB24, matrix_in_s = '709')would not work?