C# 如何使用CaForge在实时网络摄像头上检测canny边缘和积分投影?
我一直在尝试检测图像上的canny边缘,它已经成功了,但我不知道如何检测它的边缘,如果我想要实时,这是我的代码,它没有错误,但canny窗口无法处理C# 如何使用CaForge在实时网络摄像头上检测canny边缘和积分投影?,c#,real-time,webcam,projection,aforge,C#,Real Time,Webcam,Projection,Aforge,我一直在尝试检测图像上的canny边缘,它已经成功了,但我不知道如何检测它的边缘,如果我想要实时,这是我的代码,它没有错误,但canny窗口无法处理 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Thread
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using KomCit;
using System.Drawing.Imaging;
using System.IO;
using System.Timers;
using AForge;
using AForge.Imaging.Filters;
using AForge.Video.DirectShow;
using System.Threading;
namespace canny_video
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
void FinalFrame_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)
{
CameraBox.Image = (Bitmap)eventArgs.Frame.Clone();
}
private static Image resizeImage(Image imgToResize, Size size)
{
return (Image)(new Bitmap(imgToResize, size));
}
private void Form1_Load(object sender, EventArgs e)
{
//timer1.Enabled = true;
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CaptureDevice)
{
listDevice.Items.Add(Device.Name);
}
listDevice.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void start_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[listDevice.SelectedIndex].MonikerString);
FinalFrame.NewFrame += new AForge.Video.NewFrameEventHandler(FinalFrame_NewFrame);
FinalFrame.Start();
//CannyBox.Image = (Bitmap)CameraBox.Image.Clone(); //capture image bitmap
//Bitmap gambar = new Bitmap(CameraBox.Image);
Bitmap gambar = new Bitmap(CameraBox.Image);
Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
CannyEdgeDetector cany = new CannyEdgeDetector(0, 70);
Bitmap hasil = cany.Apply(gray.Apply(gambar));
// BlobsFiltering blob = new BlobsFiltering(0, 0, 20, 20);
//Bitmap hasil = blob.Apply(gray.Apply(gambar));
//CannyBox.Width = gambar.Width;
//CannyBox.Height = gambar.Height;
CannyBox.Image = hasil;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
}
}
我的上述问题已解决,谢谢:
但我还有一个积分投影的问题,我不知道怎么做。
请帮助,提前谢谢您一些评论:
您的两个私有变量的名称非常混乱。CaptureDevice是计算机上所有捕获设备的集合,而FinalFrame是捕获设备。
接收时,您不会在位图上寻找精明的边缘。
NewFrame事件处理程序在流线程中运行,因此您应该在其中使用.BeginInvoke使其可供UI线程使用。
你可以做什么:
在_NewFrame处理程序中,使用BeginInvoke将克隆的图像传输到ProcessCameraImage方法,该方法将因此在UI线程上运行。
在此方法中应用canny边缘检测。
然后将结果分配给图片框。
注意:我不知道canny边缘检测有多需要CPU。如果代价高昂,在UI线程上执行可能会阻止用户交互。在这种情况下,您可以在流线程中执行此操作,并且只传输处理后的图像以供显示。但这可能会迫使相机降低帧速率,具体取决于所需的时间 谢谢你的回答,刚才已经解决了,我修改了,不知怎么解决了 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Drawing.Imaging; //Save P2
using System.IO; //Save P2
namespace AForgeCamera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Bitmap video;
private FilterInfoCollection CaptureDevice;
private VideoCaptureDevice FinalFrame;
int mode;
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();
}
private void button1_Click(object sender, EventArgs e) //Tombol Start
{
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();
if (mode==1)
{
Grayscale gray = new Grayscale(0.2125, 0.7154, 0.0721);
Bitmap video3 = gray.Apply(video2);
CannyEdgeDetector canny = new CannyEdgeDetector(0, 70);
canny.ApplyInPlace(video3);
pictureBox2.Image = video3;
}
pictureBox1.Image = video;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
private void btnTrackingObject_Click(object sender, EventArgs e)
{
mode = 1;
}
}
}
但现在我正在研究下一个问题,我尝试使用积分投影来检测对象的位置,是否可以使用一个RGE来进行检测?谢谢你的回答,是的,检测延迟了大约5秒,你能告诉我如何减少延迟吗?这让我很烦恼