Windows将管道命名为EmguCV VideoCapture

时间:2019-12-21 17:59:29

标签: c# windows pipe emgucv named-pipes

我想用 emguCV C#中显示h264视频流。我使用 netcat raspberry pi 发送流,如果使用 netcat + vcl ,则可以看到该流,但不能从c#程序中看到(我是emgucv的新手。

NamedPipeServerStream ncatPipe = new NamedPipeServerStream(
    "ncatPipe",
    PipeDirection.InOut,
    NamedPipeServerStream.MaxAllowedServerInstances,
    PipeTransmissionMode.Message,
    PipeOptions.None,
    1024,
    1024
);

Process ncat = new Process();
ncat.StartInfo.FileName = "CMD.exe";
ncat.StartInfo.Arguments = "/C ncat -v -l 5000 >\\\\.\\pipe\\ncatPipe 2>nul";
ncat.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
ncat.Start();

ncatPipe.WaitForConnection();
VideoCapture videoCapture = new VideoCapture("\\\\.\\pipe\\ncatPipe");
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC, VideoWriter.Fourcc('h', '2', '6', '4'));
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 60);
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 480);
videoCapture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 640);

Timer playerTimer = new Timer();
playerTimer.Interval = 1000 / 60;
playerTimer.Tick += new EventHandler((object sender1, EventArgs e1) =>
{
    Bitmap captureBmp = videoCapture.QueryFrame().Bitmap;
    if (captureBmp != null)
    {
        picture.Image = captureBmp;
    }
    else
    {
        picture.Image = null;
        playerTimer.Stop();

        ncatPipe.Disconnect();
        ncatPipe.Dispose();

        ncat.Kill();
        ncat.Dispose();
        videoCapture.Dispose();
    }
});
playerTimer.Start();
  

抛出异常:Emgu.CV.World.dll中的'Emgu.CV.Util.CvException'

     

[ERROR:0]全局D:\ bb \ cv_x86 \ build \ opencv \ modules \ videoio \ src \ cap.cpp   (122)cv :: VideoCapture :: open VIDEOIO(CV_IMAGES):引发未知的C ++   例外!

1 个答案:

答案 0 :(得分:0)

编码器解决方案延迟:200ms〜, 结合了mplayer gpu acc的延迟更短。窗体中的窗口。 Windows Mplayer和Mencoder构建:http://mplayerwin.sourceforge.net/downloads.html

(在客户端上设置更高的fps可以自动修复延迟(使用Mplayer和Mencoder))

Raspberry PI命令:

raspivid -t 0 -n -o - -fl -md 4 -w 1296 -h 972 -fps 30 -ih | nc -u 192.168.137.1 5000

C#:

Process ncat = new Process();
ncat.StartInfo.FileName = "ncat\\ncat.exe";
ncat.StartInfo.Arguments = "-u -l 5000";
ncat.StartInfo.UseShellExecute = false;
ncat.StartInfo.CreateNoWindow = true;
ncat.StartInfo.RedirectStandardOutput = true;
ncat.Start();

Process mencoder = new Process();
mencoder.StartInfo.FileName = "mplayer\\mencoder.exe";
mencoder.StartInfo.Arguments = "-fps 60 -nosound - -ofps 30 -ovc raw -of rawvideo -vf format=bgr24 -really-quiet -o -";
mencoder.StartInfo.UseShellExecute = false;
mencoder.StartInfo.CreateNoWindow = true;
mencoder.StartInfo.RedirectStandardInput = true;
mencoder.StartInfo.RedirectStandardOutput = true;
mencoder.Start();

ncat.StandardOutput.BaseStream.CopyToAsync(mencoder.StandardInput.BaseStream);

byte[] raw = new byte[1296 * 972 * 3];
byte[,,] rawEmgu = new byte[972, 1296, 3];

while (true)
{
    mencoder.StandardOutput.BaseStream.Flush();
    mencoder.StandardOutput.BaseStream.Read(raw, 0, 1296 * 972 * 3);

    int n = 0;
    for (int y = 0; y < 972; y++)
    {
        for (int x = 0; x < 1296; x++)
        {
            rawEmgu[y, x, 0] = raw[n++];
            rawEmgu[y, x, 1] = raw[n++];
            rawEmgu[y, x, 2] = raw[n++];
        }
    }

    imageBox1.Image = new Image<Bgr, Byte>(rawEmgu);
}