Emgucv3.1_在WPF上的靜態影像呈現_到Live視訊處理
第一階段.先觀察 Emgucv 提供的 WPF 範例
Q1. 怎麼呈現文字跟控制視窗寬高的????
觀察功能程式區塊
Window1.xaml.cs
http://www.emgu.com/wiki/files/3.1.0/document/html/37ada42e-a5c3-caa1-dfa7-a4a64251a059.htm
參數內容
The Hershey fonts are a collection of vector fonts developed c. 1967 by Dr. Allen V. Hershey at the Naval Weapons Laboratory.
起始左上角點座標
字體
字大小
fontscale = 1
fontscale = 2
字的顏色
字的粗細
thickness = 1
thickness = 2
thickness = 3
餵進去的Mat資料型態
using ( 資源擷取 ) 內嵌陳述式
BitmapSourceConverter.cs 程式區塊中
BitmapSourceConvert 類別 的 程式碼分析
Q2. Image物件預設為 Fill 佔全視窗畫面
去 觀察 介面部分程式
Window1.xaml
第二階段. 試著改成開視訊
加個按鈕
開視訊畫面
程式碼:
Window1.xaml.cs
Window1.xaml
Q1. 怎麼呈現文字跟控制視窗寬高的????
觀察功能程式區塊
Window1.xaml.cs
http://www.emgu.com/wiki/files/3.1.0/document/html/37ada42e-a5c3-caa1-dfa7-a4a64251a059.htm
參數內容
public static void PutText(
IInputOutputArray img, // 欲放置文字的目標影像矩陣
string text, //文字內容
Point org, //起始左上角點座標
FontFace fontFace, //字體
double fontScale, //字大小
MCvScalar color, //字的顏色
int thickness = 1, //字的粗細(預設為1 正常粗細)
LineType lineType = LineType.EightConnected,
bool bottomLeftOrigin = false
)
The Hershey fonts are a collection of vector fonts developed c. 1967 by Dr. Allen V. Hershey at the Naval Weapons Laboratory.
起始左上角點座標
字體
Emgu.CV.CvEnum.FontFace.HersheyDuplex
Duplex(英文字体)
Emgu.CV.CvEnum.FontFace.HersheyTriplex
Triplex 字體
字大小
fontscale = 1
fontscale = 2
字的顏色
字的粗細
thickness = 1
thickness = 2
thickness = 3
餵進去的Mat資料型態
using ( 資源擷取 ) 內嵌陳述式
BitmapSourceConverter.cs 程式區塊中
BitmapSourceConvert 類別 的 程式碼分析
//----------------------------------------------------------------------------
// Copyright (C) 2004-2016 by EMGU Corporation. All rights reserved.
//----------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Emgu.CV;
namespace Emgu.CV.WPF
{
public static class BitmapSourceConvert
{
/// <summary>
/// Delete a GDI object
/// </summary>
/// <param name="o">The poniter to the GDI object to be deleted</param>
/// <returns></returns>
[DllImport("gdi32")]
private static extern int DeleteObject(IntPtr o);
/// <summary>
/// Convert an IImage to a WPF BitmapSource. The result can be used in the Set Property of Image.Source
/// </summary>
/// <param name="image">The Emgu CV Image</param>
/// <returns>The equivalent BitmapSource</returns>
public static BitmapSource ToBitmapSource(IImage image)
{
using (System.Drawing.Bitmap source = image.Bitmap)
{
IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
ptr,
IntPtr.Zero,
Int32Rect.Empty,
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
DeleteObject(ptr); //release the HBitmap
return bs;
}
}
}
}
Q2. Image物件預設為 Fill 佔全視窗畫面
去 觀察 介面部分程式
Window1.xaml
第二階段. 試著改成開視訊
加個按鈕
開視訊畫面
程式碼:
Window1.xaml.cs
//----------------------------------------------------------------------------
// 冠羽
//----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using System.Threading;
namespace Emgu.CV.WPF
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private Capture _capture;
private bool _captureInProgress;
public delegate void delUpdateImageCallback(Mat img);
public Window1()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_capture == null)
{
try
{
_capture = new Capture(0);
}
catch (NullReferenceException excpt)
{
MessageBox.Show(excpt.Message);
}
}
if (_capture != null)
{
Thread thrdWebcam = new Thread(new ThreadStart(ProcessFrame));
_captureInProgress = true;
thrdWebcam.Start();
}
}
private void ProcessFrame()
{
while (_captureInProgress == true)
{
Mat frame = _capture.QueryFrame();
delUpdateImageCallback dUIC = new delUpdateImageCallback(UpdateWebcamImage);
this.Dispatcher.Invoke(dUIC, frame);
}
}
private void UpdateWebcamImage(Mat img)
{
image1.Source = BitmapSourceConvert.ToBitmapSource(img);
}
}
}
Window1.xaml
<Window x:Class="Emgu.CV.WPF.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="303" Width="564">
<Grid>
<Image Name="image1" Stretch="Fill" Margin="22.388,23.88,161.194,26.866" />
<Button Content="Button" HorizontalAlignment="Left" Margin="430.328,23.88,0,0" VerticalAlignment="Top" Width="98.881" Height="38.403" Click="Button_Click"/>
</Grid>
</Window>
留言
張貼留言