Macbook 相机无法关闭和一个小的 C# aforge 问题
Macbook camera won't turn off and a minor C# aforge issue
我在带有训练营的 Macbook pro 上使用 Aforge 框架使用 Visual Studio C# 创建了可以跟踪对象的项目。问题是当我 运行 代码并关闭它时。然后再次 运行 ,代码将不再显示我相机上的图像。此外,代码停止后,我的网络摄像头上的绿灯仍然亮着。
我的第二个问题是,当我单击 "track object button" 并将滤镜应用于图像时,由于某些原因,它将滤镜应用于两个图片框而不是一个。我需要帮助调试我的代码。我尝试重新安排应用过滤器的行,但我似乎无法正确安排。
这是我的代码
namespace VideoProcessing1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Graphics g;
Bitmap video;
bool OnOff;
int mode;
int thoigianddemnguoc;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
//FinalFrame.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
g = Graphics.FromImage(video2);
g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
g.Dispose();
if (mode == 1)
{
ColorFiltering colorfilter = new ColorFiltering();
colorfilter.Red = new AForge.IntRange(0, 255);
colorfilter.Green = new AForge.IntRange(0, 75);
colorfilter.Green = new AForge.IntRange(0, 75);
colorfilter.ApplyInPlace(video2);
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinHeight = 20;
blobcounter.MaxWidth = 20;
blobcounter.ObjectsOrder = ObjectsOrder.Size;
blobcounter.ProcessImage(video2);
Rectangle[] rect = blobcounter.GetObjectsRectangles();
if (rect.Length > 0)
{
Rectangle objec = rect[0];
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.White, 3))
{
graphic.DrawRectangle(pen, objec);
}
graphic.Dispose();
}
pictureBox2.Image = video2;
}
pictureBox1.Image = video2;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(FinalFrame.IsRunning==true)
{
FinalFrame.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void button3_Click(object sender, EventArgs e)
{
mode = 1;
}
}
}
不建议在您的视频捕获设备上调用 Stop
。根据docs:"The correct way of stopping camera is signaling it stop and then waiting for background thread's completion."
你应该尝试使用类似这样的东西来停止:
FinalFrame.SignalToStop();
FinalFrame.WaitForStop();
如果使用不当,线程行为会变得不可预测,我相信您的问题出在这里。
关于你的第二个问题,问题行是
pictureBox1.Image = video2;
您已经将 video2
存储在 pictureBox2.Image
中,所以我假设您不想对另一个 pictureBox 执行此操作。
我在带有训练营的 Macbook pro 上使用 Aforge 框架使用 Visual Studio C# 创建了可以跟踪对象的项目。问题是当我 运行 代码并关闭它时。然后再次 运行 ,代码将不再显示我相机上的图像。此外,代码停止后,我的网络摄像头上的绿灯仍然亮着。
我的第二个问题是,当我单击 "track object button" 并将滤镜应用于图像时,由于某些原因,它将滤镜应用于两个图片框而不是一个。我需要帮助调试我的代码。我尝试重新安排应用过滤器的行,但我似乎无法正确安排。
这是我的代码
namespace VideoProcessing1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Graphics g;
Bitmap video;
bool OnOff;
int mode;
int thoigianddemnguoc;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
//FinalFrame.Stop();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
Bitmap video2 = (Bitmap)eventArgs.Frame.Clone();
g = Graphics.FromImage(video2);
g.DrawString("Test", new Font("Arial", 20), new SolidBrush(Color.White), new PointF(2, 2));
g.Dispose();
if (mode == 1)
{
ColorFiltering colorfilter = new ColorFiltering();
colorfilter.Red = new AForge.IntRange(0, 255);
colorfilter.Green = new AForge.IntRange(0, 75);
colorfilter.Green = new AForge.IntRange(0, 75);
colorfilter.ApplyInPlace(video2);
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinHeight = 20;
blobcounter.MaxWidth = 20;
blobcounter.ObjectsOrder = ObjectsOrder.Size;
blobcounter.ProcessImage(video2);
Rectangle[] rect = blobcounter.GetObjectsRectangles();
if (rect.Length > 0)
{
Rectangle objec = rect[0];
Graphics graphic = Graphics.FromImage(video2);
using (Pen pen = new Pen(Color.White, 3))
{
graphic.DrawRectangle(pen, objec);
}
graphic.Dispose();
}
pictureBox2.Image = video2;
}
pictureBox1.Image = video2;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if(FinalFrame.IsRunning==true)
{
FinalFrame.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
private void button3_Click(object sender, EventArgs e)
{
mode = 1;
}
}
}
不建议在您的视频捕获设备上调用 Stop
。根据docs:"The correct way of stopping camera is signaling it stop and then waiting for background thread's completion."
你应该尝试使用类似这样的东西来停止:
FinalFrame.SignalToStop();
FinalFrame.WaitForStop();
如果使用不当,线程行为会变得不可预测,我相信您的问题出在这里。
关于你的第二个问题,问题行是
pictureBox1.Image = video2;
您已经将 video2
存储在 pictureBox2.Image
中,所以我假设您不想对另一个 pictureBox 执行此操作。