从 YUV 到 RGB 颜色的转换

问题描述 投票:0回答:1

我正在尝试将 FFMPEG 的强度颜色图从 yuv 格式转换为 rgb。它应该为您提供图像中颜色条所示的颜色。您可以使用命令生成频谱图:

ffmpeg -i words.wav -lavfi showspectrumpic=s=224x224:mode=separate:color=intensity spectrogram.png

这是代码:

import numpy as np

def rgb_from_yuv(Y, U, V):

    Y -= 16
    U -= 128
    V -= 128
    R = 1.164 * Y + 1.596 * V
    G = 1.164 * Y - 0.392 * U - 0.813 * V
    B = 1.164 * Y + 2.017 * U
  
    # Clip and normalize RGB values
    R = np.clip(R, 0, 255)
    G = np.clip(G, 0, 255)
    B = np.clip(B, 0, 255)
  
    return '#{:02X}{:02X}{:02X}'.format(int(R), int(G), int(B))

def yuv_to_rgb(Y, U,V):
    # Convert YUV to RGB
    R  = Y + (V - 128) *  1.40200
    G  = Y + (U - 128) * -0.34414 + (V - 128) * -0.71414
    B  = Y + (U - 128) *  1.77200

    # Clip and normalize RGB values
    R = np.clip(R, 0, 255)
    G = np.clip(G, 0, 255)
    B = np.clip(B, 0, 255)
    
    print(R,G,B)
    
    return '#{:02X}{:02X}{:02X}'.format(int(R), int(G), int(B))

# FFMPEG's intensity color map
colors = [
    [    0,                  0,                  0,                   0 ],
    [ 0.13, .03587126228984074,  .1573300977624594, -.02548747583751842 ],
    [ 0.30, .18572281794568020,  .1772436246393981,  .17475554840414750 ],
    [ 0.60, .28184980583656130, -.1593064119945782,  .47132074554608920 ],
    [ 0.73, .65830621175547810, -.3716070802232764,  .24352759331252930 ],
    [ 0.78, .76318535758242900, -.4307467689263783,  .16866496622310430 ],
    [ 0.91, .95336363636363640, -.2045454545454546,  .03313636363636363 ],
    [    1,                  1,                  0,                   0 ]]
    
cmaps = []
for i, c in enumerate(colors):
    Y = c[1]
    U = c[2]
    V = c[3]

    hex = yuv_to_rgb(Y,U,V)

    cmaps.append((c[0], hex))    
print(cmaps)

这两个函数都没有提供所需的输出。

python ffmpeg colors yuv color-conversion
1个回答
0
投票

看来U、V通道像素值是从127开始累加的。参见FFmpeg源码:

https://github.com/FFmpeg/FFmpeg/blob/7e59c4f90885a9ffffb0a3f1d385b4eae3530529/libavfilter/avf_showspectrum.c#L1416

所以,我怀疑如果您首先将 0.5 添加到

colors
的第三列和第四列,标准 YUV 到 RGB 的转换应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.