Python YUV420p到其他格式,颜色偏移问题

Python YUV420p到其他格式,颜色偏移问题,python,opencv,image-processing,ffmpeg,yuv,Python,Opencv,Image Processing,Ffmpeg,Yuv,因此,我正在使用AR无人机编写一个颜色检测应用程序。无人机以YUV420p格式从其相机向我的python/opencv套接字服务器发送图像 作为opencv IPLImage访问图像的方法如下(是的,这效率很低,但我不想/不想自己编写新的转换工具): 将yuv图像保存到文件(some_image.yuv) subprocess.call(在此处插入ffmpeg调用) 使用cv.LoadImage将生成的文件(bmp、png,没关系)读回 我现在的问题是一个非常明显的颜色变化。我在这些照片里挥舞着

因此,我正在使用AR无人机编写一个颜色检测应用程序。无人机以YUV420p格式从其相机向我的python/opencv套接字服务器发送图像

作为opencv IPLImage访问图像的方法如下(是的,这效率很低,但我不想/不想自己编写新的转换工具):

  • 将yuv图像保存到文件(some_image.yuv)
  • subprocess.call(在此处插入ffmpeg调用)
  • 使用cv.LoadImage将生成的文件(bmp、png,没关系)读回
  • 我现在的问题是一个非常明显的颜色变化。我在这些照片里挥舞着一张红色的毡纸。第一个呈深黄色。第二张没有那么糟糕,但非常罕见——大多数情况下,当我有红色床单时,它的颜色很深

    • 着色图像:
    • 色彩较少的图像:
    我想知道这两件事:

  • 如果有更好的方法
  • 如果颜色着色问题可以解决
  • 我的ffmpeg转换行看起来像

    ffmpeg -s 640x480 -vcodec rawvideo -f rawvideo -pix_fmt yuv420p -i image.yuv -vcodec bmp -f image2 output.bmp
    
    我也试过:

    ffmpeg -f rawvideo -s 640x480 -pix_fmt yuv420p -vf colormatrix=bt709:bt601 -i image.yuv -f image -vcodec png output.png
    
    不幸的是,颜色变化总是存在的


    颜色偏移是我现在的大问题,因为我稍后将图像转换为HSV,并使用阈值选择适合我的颜色范围。

    这种方法似乎适合我:

    $ ffmpeg -s 352x288 -i foreman_cif_frame_0.yuv f.png
    ffmpeg version N-46810-g7750c48 Copyright (c) 2000-2012 the FFmpeg developers
      built on Apr 21 2013 11:12:24 with gcc 4.6 (Ubuntu/Linaro 4.6.3-1ubuntu5)
      configuration: --enable-gpl --enable-libx264 --enable-libmp3lame
      libavutil      52.  7.100 / 52.  7.100
      libavcodec     54. 71.100 / 54. 71.100
      libavformat    54. 36.100 / 54. 36.100
      libavdevice    54.  3.100 / 54.  3.100
      libavfilter     3. 23.100 /  3. 23.100
      libswscale      2.  1.102 /  2.  1.102
      libswresample   0. 16.100 /  0. 16.100
      libpostproc    52.  1.100 / 52.  1.100
    [rawvideo @ 0x18a1320] Estimating duration from bitrate, this may be inaccurate
    Input #0, rawvideo, from 'foreman_cif_frame_0.yuv':
      Duration: N/A, start: 0.000000, bitrate: N/A
        Stream #0:0: Video: rawvideo (I420 / 0x30323449), yuv420p, 352x288, 25 tbr, 25 tbn, 25 tbc
    Output #0, image2, to 'f.png':
      Metadata:
        encoder         : Lavf54.36.100
        Stream #0:0: Video: png, rgb24, 352x288, q=2-31, 200 kb/s, 90k tbn, 25 tbc
    Stream mapping:
      Stream #0:0 -> #0:0 (rawvideo -> png)
    Press [q] to stop, [?] for help
    frame=    1 fps=0.0 q=0.0 Lsize=       0kB time=00:00:00.04 bitrate=   0.0kbits/s    
    video:201kB audio:0kB subtitle:0 global headers:0kB muxing overhead -100.000000%
    
    输出:

    另一种方法是使用强大的

    有趣的是,ffmpeg和imagemagick不会返回相同的结果:

    $ compare -compose src f.png f2.png diff.png
    
    结果:

    更新 太糟糕了。唯一合理的解释是,PIL是borked(当涉及YCbCr处理时,它有一些特点;这里的许多Q都是如此)。正如你从我的帖子中看到的,如果输入是正确的YCbCr,那么输出是正确的

    如果我读对了你的Q,你已经收到了格式的数据。 输入为VGA格式,因此以下代码将单独的平面(Y、Cb、Cr)拆分为自己的变量:

    # Here I'm assuming you get the data from the drone into parameter raw
    # 1 frame contains 640*480*3/2 = 460800 bytes
    import numpy as np
    
    # turn raw into a numpy array
    raw = np.array(raw)
    
    # calculate where each plane starts and stops
    wh = 640 * 480
    p = (0, wh, wh, wh/4*5, wh/4*5, wh/2*3)
    
    # Now use slizing to extract the different planes
    yy = np.empty(640*480, dtype=np.uint8)
    cb = np.empty(640*480/4, dtype=np.uint8)
    cb = np.empty(640*480/4, dtype=np.uint8)
    
    yy = raw[p[0]:p[1]]
    cb = raw[p[2]:p[3]]
    cr = raw[p[4]:p[5]]
    
    现在你有了漂亮的numpy数组中的数据!要转换为矩阵,请执行以下操作:

    yy.reshape([480, 640])
    cb.reshape([480 / 2, 640 / 2])
    cr.reshape([480 / 2, 640 / 2])
    

    希望有帮助!如果没有,请给我留言…

    谢谢弗雷德里克的详细回复。不幸的是,ffmpeg和convert的参数与我的ffmpeg行的功能几乎相同。颜色变化仍然在发生,这很奇怪。红色变成黄色,其他东西都变成绿色:/谢谢弗雷德,所以我确定问题其实是相机本身。出于某种原因,即使使用AR无人机提供的带有YUV到RGB转换器的C固件,强烈的红色或蓝色也会使图片着色。我的解决方案是在相机上使用中性密度滤光片来规范入射光。这大大缓解了问题!您还应该包括完整的ffmpeg控制台输出。
    yy.reshape([480, 640])
    cb.reshape([480 / 2, 640 / 2])
    cr.reshape([480 / 2, 640 / 2])