2013-06-01 28 views
2

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

我做访问图像作为OpenCV的IplImage结构是什么以下(是的,这是低效的,但是我没有/不希望有写新的转换工具,我自己):

  1. YUV图像保存到一个文件中(some_image.yuv)
  2. subprocess.call(插入ffmpeg的来电来访
  3. 读取结果文件中使用cv.LoadImage(BMP,PNG,没关系)回

我现在的问题是一个非常明显的颜色转变。这些照片中我挥动着红色的毡子。第一个显示出沉重的黄色色调。第二个并不坏,但非常罕见 - 大多数情况下,当我有红色的床单时,它的色彩很浓。

我想知道这两种东西:

  1. ,如果有任何一个更好的方式来做到这一点
  2. 如果色彩问题可以解决

我的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并使用阈值选择适合我的颜色范围。

+0

您还应该包含完整的ffmpeg控制台输出。 – LordNeckbeard

回答

1

这种做法似乎为我工作:

$ 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% 

输出:

Famous forman as png

另一种方法是使用强大的Imagemagick

$ convert -size 352x288 -depth 8 foreman_cif_frame_0.yuv f2.png 

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

$ compare -compose src f.png f2.png diff.png 

结果:

enter image description here

更新 太糟糕了。那么唯一合理的解释就是PIL是borked(它涉及到YCbCr处理时有一些特殊性;这里有很多关于这个的Q)。正如你从我的帖子中看到的那样,如果输入是正确的YCbCr,输出就OK!

如果我读到您的Q正确,您已经收到YV12格式的数据。 输入为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]) 

希望它有帮助!如果没有,请发表评论...

+0

感谢Fredrik的详细回复。不幸的是你的ffmpeg和convert的参数几乎和我的ffmpeg所做的一样。颜色转换仍然发生,而且非常奇怪。红色变成淡黄色,其他一切变成绿色:/ – Sam

+0

看到更新,希望能帮助你:-) –

+0

感谢Fred,所以我确定问题实际上是相机本身。出于某种原因,即使使用AR Drone提供的具有YUV到RGB转换器的C固件,强烈的红色或蓝色也会对图片着色。 我的解决方案是在摄像头上使用中性密度滤镜来归一化入射光。它极大地缓解了这个问题! – Sam