[英]Windows named pipe to EmguCV VideoCapture
我想用emguCV在C# 中顯示 h264 視頻流。 我從帶有netcat的樹莓派發送流,如果我使用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”
[錯誤:0] 全局 D:\\bb\\cv_x86\\build\\opencv\\modules\\videoio\\src\\cap.cpp (122) cv::VideoCapture::open VIDEOIO(CV_IMAGES):引發未知的 C++ 異常!
編輯 1:現在我正在嘗試使用 FFMPEG 從管道中獲取位圖幀。
編輯 2:FFMPEG 有效,但我的延遲約為 4000 毫秒。
Mencoder 解決方案延遲:200ms~ ,包含 mplayer gpu acc 的延遲甚至更少。 窗體中的窗口。 windows Mplayer & Mencoder 構建: http ://mplayerwin.sourceforge.net/downloads.html
(在客戶端上設置更高的 fps 自動修復延遲(使用 Mplayer 和 Mencoder))
樹莓派命令:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.