用opencv和ffmpeg制作视频。如何找到正确的颜色格式? [英] Making a video with opencv and ffmpeg. How to find the right color format?

查看:500
本文介绍了用opencv和ffmpeg制作视频。如何找到正确的颜色格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用python,opencv和ffmpeg构建的网络摄像头录像机程序



它可以正常工作,只是视频的颜色比现实更蓝。问题似乎来自图像的颜色格式。



似乎OpenCv正在给出BGR图像,ffmpeg + libx264正在期待YUV420p。我读过YUV420p对应于YCbCr。



opencv没有从BGR到YCbCr的转换。它只转换为YCrCb。



我已经做了一些搜索,尝试了不同的替代方法来尝试将opencv映像转换为ffmpeg + libx264可能是可以的。没有工作。在这一点上,我有点迷失,我会感激任何可以帮助我修复这个颜色问题的指针。

解决方案

你是对的,OpenCV的默认像素格式是 BGR



ffmpeg端的等效格式为 BGR24 ,因此如果不想要,则不需要将其转换为YUV420p到。



这篇文章显示了如何使用python应用程序从网络摄像头捕获帧,并将帧写入 stdout 。目的是在cmd行调用此应用程序,并将结果直接传递到ffmpeg应用程序,该应用程序将帧存储在磁盘上。确实非常聪明!



capture.py

  import cv,sys 

cap = cv.CaptureFromCAM(0)
如果不是cap:
sys.stdout.write(CaptureFromCAM失败)

while True:
如果不是cv.GrabFrame(cap):
break

frame = cv.RetrieveFrame(cap)
sys.stdout。 write(frame.tostring())

要在shell上执行的命令是: p>

  python capture.py | ffmpeg -f rawvideo -pix_fmt bgr24 -s 640x480 -r 30 -i  -  -an -f avi -r 30 foo.avi 




其中




  • -r给出帧率下载相机

  • - 说不编码音频


我使用OpenCV 2.4.2在Mac OS X上测试了这个解决方案。



编辑:



如果您没有尝试从摄像机进行记录,并使用OpenCV将视频写入磁盘上的mp4文件,我们在此继续:

  import cv,sys 

cap = cv.CaptureFromCAM(0)#0用于/ dev / video0
如果不是cap:
sys.stdout.write(!!! Failed CaptureFromCAM)
sys.exit(1)

frame = cv.RetrieveFrame(
如果不是框架:
sys.stdout.write(!!!无法检索第一帧)
sys.exit(1)

#不幸的是,以下指令转到0
#fps = cv.GetCaptureProperty(cap,cv.CV_CAP_PROP_FPS)
fps = 25.0#所以我们需要硬编码FPS
打印录制在:,fps,fps

frame_size = cv.GetSize(frame)
打印视频大小:,frame_size

writer = cv.CreateVideoWriter(out.mp4,cv。 CV_FOURCC('F','M','P','4'),fps,frame_size,True)
如果不是写者:
sys.stdout.write(!!!创建视频编辑器时出错)
sys.exit(1)


while True:
如果不是cv.GrabFrame(cap):
break
frame = cv.RetrieveFrame(cap)
cv.WriteFrame(writer,frame)

cv.ReleaseVideoWriter(writer)
cv.ReleaseCapture(cap)

我已经在Mac OS X和OpenCV 2.4.2上使用Python 2.7进行了测试。


I have a webcam video recorder program built with python, opencv and ffmpeg

It works ok except that the color of the video is more blue than the reality. The problem seems to come from color format of images.

It seems that OpenCv is giving BGR images and ffmpeg+libx264 is expecting YUV420p. I've read that YUV420p correspond to YCbCr.

opencv has no conversion from BGR to YCbCr. It only has a conversion to YCrCb.

I have made some searchs and tried different alternatives to try converting opencv image to something that could be ok for ffmpeg+libx264. None is working. At this point, I am a bit lost and I would appreciate any pointer that could help me to fix this color issue.

解决方案

You are right, the default pixel format of OpenCV is BGR.

The equivalent format on the ffmpeg side would be BGR24, so you don't need to convert it to YUV420p if you don't want to.

This post shows how to use a python application to capture frames from the webcam and write the frames to stdout. The purpose is to invoke this app on the cmd-line and pipe the result directly to the ffmpeg application, which stores the frames on the disk. Quite clever indeed!

capture.py:

import cv, sys

cap = cv.CaptureFromCAM(0)
if not cap:
    sys.stdout.write("failed CaptureFromCAM")

while True :
    if not cv.GrabFrame(cap) : 
        break

    frame = cv.RetrieveFrame(cap)
    sys.stdout.write( frame.tostring() )

And the command to be executed on the shell is:

python capture.py | ffmpeg -f rawvideo -pix_fmt bgr24 -s 640x480 -r 30 -i - -an -f avi -r 30 foo.avi

Where:

  • -r gives the frame rate coming off the camera
  • -an says "don't encode audio"

I tested this solution on my Mac OS X with OpenCV 2.4.2.

EDIT:

In case you haven't tried to record from the camera and use OpenCV to write the video to an mp4 file on the disk, here we go:

import cv, sys

cap = cv.CaptureFromCAM(0)   # 0 is for /dev/video0
if not cap:
    sys.stdout.write("!!! Failed CaptureFromCAM")
    sys.exit(1)

frame = cv.RetrieveFrame(cap)
if not frame: 
    sys.stdout.write("!!! Failed to retrieve first frame")
    sys.exit(1)

# Unfortunately, the following instruction returns 0
#fps = cv.GetCaptureProperty(cap, cv.CV_CAP_PROP_FPS)
fps = 25.0      # so we need to hardcode the FPS
print "Recording at: ", fps, " fps"  

frame_size = cv.GetSize(frame)
print "Video size: ", frame_size  

writer = cv.CreateVideoWriter("out.mp4", cv.CV_FOURCC('F', 'M', 'P', '4'), fps, frame_size, True)
if not writer:
    sys.stdout.write("!!! Error in creating video writer")
    sys.exit(1)


while True :
    if not cv.GrabFrame(cap) : 
        break
    frame = cv.RetrieveFrame(cap)
    cv.WriteFrame(writer, frame)

cv.ReleaseVideoWriter(writer)
cv.ReleaseCapture(cap)

I've tested this with Python 2.7 on Mac OS X and OpenCV 2.4.2.

这篇关于用opencv和ffmpeg制作视频。如何找到正确的颜色格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆