如何使用鼠标在视频流上的未填充框上绘图

发布于 2024-09-06 19:20:28 字数 1070 浏览 7 评论 0原文

我正在使用 dshownet(第一次)和 C#。我有一个示例来获取网络摄像头输入并将其显示在表单上。我现在需要使用鼠标在视频流顶部绘制一个矩形。 (目的是从那里开始跟踪盒子里面的东西)。

听说有一种东西叫VMR。所以我查阅了 dshownet 样本并仔细研究了它们。我没有找到任何使用鼠标在视频流上叠加形状的示例。这里有人建议使用 colorkey。另一个人说使用 GDI+ 和鼠标处理。我尝试编译 DXLogo 示例,但收到此错误:


错误 1 ​​无法创建抽象类或接口 'System.Drawing.Image' C:\Documents and Settings\TLNA\Desktop\Final Year Project\Libraries\DirectShow 库的实例2\DirectShowSamples-2010-February\Samples\Capture\DxLogo\Capture.cs 128 32 DxLogo-2008


代码部分:


   if (fileName.Length > 0)
           {
               m_Bitmap = new Image(fileName); // error happened here

               Rectangle r = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
               m_bmdLogo = m_Bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
           }

我知道我必须通过 Bitmap9 接口。但我真的不知道从哪里开始。我应该阅读 Directshow API 文档吗?

顺便说一句,我还有《用于数字视频和电视的 Microsoft Directshow 编程》一书。我开始读那本书,读了几页就停了下来,因为代码主要是 C++ 的。我应该继续阅读这本书吗?如何在 C# 中完成上述某些任务?

关于如何在视频上绘图的任何建议。一些有用的链接(教程)会有所帮助。

非常感谢 特纳

I am using dshownet(first time) and C#. I have got a sample to take the web cam input and display it on a form. I now need to draw a rectangle on top of the video stream using the mouse. (the intent is to track what is inside the box from there onwards).

I heard that there is something called VMR. So I went to the dshownet samples and went through them. I didnt find any samples that use the mouse to overlay a shape on the video stream. Someone on here suggested to use colorkey. Another person said to use GDI+ and mouse handling. I tried to compile the DXLogo sample but got this error :


Error 1 Cannot create an instance of the abstract class or interface 'System.Drawing.Image' C:\Documents and Settings\TLNA\Desktop\Final Year Project\Libraries\DirectShow library 2\DirectShowSamples-2010-February\Samples\Capture\DxLogo\Capture.cs 128 32 DxLogo-2008


for the code section:


   if (fileName.Length > 0)
           {
               m_Bitmap = new Image(fileName); // error happened here

               Rectangle r = new Rectangle(0, 0, m_Bitmap.Width, m_Bitmap.Height);
               m_bmdLogo = m_Bitmap.LockBits(r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
           }

I know that I must go through the Bitmap9 Interface. But I really dont know where to start. Should I read the Directshow API docs.

btw I also have the book Programming Microsoft Directshow for digital video and television. I started reading that book and stopped after a few pages since the code is mainly in C++. Should I continue to read this is book ? How can I accomplish the certain mentioned tasks in C# ?

Any suggestions as how to draw on the video. Some useful links(tutorials) would be helpful.

Many Thanks
Tlna

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强辩 2024-09-13 19:20:29

我不确定为什么 DirectShow 示例无法编译,但您也许可以将问题行:更改

m_Bitmap = new Image(fileName);

为:

m_Bitmap = new Bitmap(fileName);

并使其正常工作。

您实际上在这里面临着一个非常困难的问题。 DirectShow 通过每秒多次将一系列静止帧绘制到设备上下文(如 PictureBox 或 Form,甚至屏幕)来渲染视频(取决于帧速率)。您(作为程序员)还可以(轻松)将图形直接渲染到同一设备上下文。

但是,为了使绘制的框出现在正在运行的视频上方,您的代码需要在 DirectShow 绘制视频的每一帧后立即绘制矩形;否则,下一帧将消除您的矩形。 DirectShow 对象可能有某种可以处理的帧渲染事件,然后在事件处理程序中,您只需重新绘制框(基于初始和当前鼠标坐标,您可以从任何对象的 MouseDown 和 MouseMove 事件中获取该坐标)控制您正在绘制的内容)。

更新:我刚刚查看了我使用 DirectShow.NET 时的代码,看起来有一个事件 (DsEvCode.Repaint)可以钩住并用来绘制您的盒子。

我还没有查看您正在使用的代码示例,但是进行搜索并查看是否可以在代码中找到 OnGraphNotify() 方法,您应该能够添加类似这样的内容:

if (code == DsEvCode.Repaint)
{
    // draw the box here
}

据推测,此事件会在视频的每一帧渲染后触发,因此如果您每次在此处重新绘制框,它看起来就像框一直存在一样。

I'm not sure why the DirectShow sample doesn't compile, but you may be able to change the problem line:

m_Bitmap = new Image(fileName);

to this:

m_Bitmap = new Bitmap(fileName);

and get it to work.

You're actually facing a pretty difficult problem here. DirectShow renders a video by drawing a series of still frames to a device context (like a PictureBox or a Form, or even the screen) many times a second (depending on whatever the frame rate is). You (as a programmer) can also (easily) render graphics directly to this same device context.

However, in order to make your drawn box appear over top of a running video, your code needs to draw the rectangle immediately after DirectShow draws each frame of the video; otherwise, the next frame will obliterate your rectangle. DirectShow objects probably have some sort of frame rendering event that you can handle, and then inside the event handler you just re-draw your box (based on the initial and current mouse coordinates, which you can get from the MouseDown and MouseMove events of whatever control you're drawing on).

Update: I just took a look at my code from when I was playing around with DirectShow.NET, and it looks like there is an event (DsEvCode.Repaint) that you could hook into and use to draw your box.

I haven't looked at the code sample you're working with, but do a search and see if you can find an OnGraphNotify() method in your code, you should be able to add something like this:

if (code == DsEvCode.Repaint)
{
    // draw the box here
}

Presumably, this event is fired after each frame of the video is rendered, so if you redraw your box here every time it will appear as if the box is persisting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文