最近接觸的項目,使用了大量圖片和視頻操作,其中wpf aforge的使用遇到很多問題。本站將慢慢和大家分享
1、擷取邊緣點方法:
/// <summary>
/// 邊緣點越多,映像越清晰
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public int Get邊緣點數量(System.Drawing.Bitmap source)
{
try
{
AForge.Imaging.SusanCornersDetector mcd = new AForge.Imaging.SusanCornersDetector();
List<AForge.IntPoint> corners = mcd.ProcessImage(source);
return corners.Count();
}
catch
{
AForge.Imaging.MoravecCornersDetector mcd = new AForge.Imaging.MoravecCornersDetector();
List<AForge.IntPoint> corners = mcd.ProcessImage(source);
return corners.Count();
}
}
2、使用aforge內建的動態視頻展示視頻
/// <summary>
/// 載入預設裝置
/// </summary>
public void cameraOpen()
{
CloseCaptureDevice();
// 設定初始視頻裝置
FilterInfoCollection videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count > 0)
{
for (int i = 0; i < videoDevices.Count; i++)
{
if (videoDevices[i].Name.IndexOf("YW") > -1)
{
DeviceMoniker = videoDevices[i].MonikerString;
break;
}
}
//DeviceMoniker = videoDevices[0]..Name.Contains("YW").MonikerString;
// UsbVideo = new VideoCaptureDevice(videoDevices[0].MonikerString);
// camera_Chemistry.vp.videoMonkeyName = videoDevices[0].MonikerString;
UsbVideo = new VideoCaptureDevice(DeviceMoniker);
camera_Chemistry.vp.videoMonkeyName = DeviceMoniker;
//sourcePlayer.NewFrame -= new AForge.Controls.VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
//sourcePlayer.NewFrame += new AForge.Controls.VideoSourcePlayer.NewFrameHandler(videoSourcePlayer_NewFrame);
UsbVideo.VideoResolution = UsbVideo.VideoCapabilities.Where(a => a.FrameSize.Width == 640 && a.FrameSize.Height == 480).FirstOrDefault();
//UsbVideo.SnapshotFrame += UsbVideo_SnapshotFrame;
UsbVideo.NewFrame += UsbVideo_NewFrame; //截圖
sourcePlayer.VideoSource = UsbVideo;
// sourcePlayer.Start();
}
else
{
//button_Play.IsEnabled = false;
//button_Capture.IsEnabled = false;
}
//設定相機攝像機的屬性,亮度 對比
GaobeijingCamera();
this電機狀態 = 1; //5
}
private void CloseCaptureDevice()
{
if (sourcePlayer != null && sourcePlayer.VideoSource != null)
{
if (sourcePlayer.IsRunning)
{
sourcePlayer.SignalToStop();
sourcePlayer.WaitForStop();
sourcePlayer.Stop();
}
sourcePlayer.VideoSource = null;
if (UsbVideo.ProvideSnapshots)
{
// UsbVideo.SnapshotFrame -= new NewFrameEventHandler(UsbVideo_SnapshotFrame);
}
}
}
得到圖片:
private void UsbVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
// ShowSnapshot((System.Drawing.Bitmap)eventArgs.Frame); //.Clone()
ShowSnapshot(eventArgs);
}