You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FFmpeg Intensity色彩映射YUV转RGB转换失败求助

转换FFmpeg Intensity色彩映射从YUV到RGB的问题

我正尝试将FFmpeg的Intensity色彩映射从YUV格式转换为RGB,期望得到对应色条所示的颜色。可通过以下命令生成频谱图:

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

FFmpeg频谱图

以下是相关代码:

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)

但上述两个转换函数均无法输出预期结果,恳请协助解决此问题。

内容的提问来源于stack exchange,提问作者Prashant

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 22:10:57