using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 System.Xml;
using Emgu.CV;
using RichCreator.Editor.Utils;
using ZTImage;
namespace RichCreator.Editor.Tools.CV
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainCV : Window
{
public string XmlFile { get; set; }
public string ImageFile { get; set; }
public Int32 WinWidth { get; set; }
public Int32 WinHeight { get; set; }
public Int32 BlockWidth { get; set; }
public Int32 BlockHeight { get; set; }
public Int32 StrideWidth { get; set; }
public Int32 StrideHeight { get; set; }
public Int32 CellWidth { get; set; }
public Int32 CellHeight { get; set; }
public Int32 ChannelNumber { get; set; }
public MainCV()
{
InitializeComponent();
//detector = new HOGDetector();
}
///
/// 开始识别
///
///
///
private void Detector_Click(object sender, RoutedEventArgs e)
{
if (!CheckAndFillAllParameter())
{
return;
}
HOGDetector detector = new HOGDetector(
new System.Drawing.Size(this.WinWidth, this.WinHeight),
new System.Drawing.Size(this.BlockWidth, this.BlockHeight),
new System.Drawing.Size(this.StrideWidth, this.StrideHeight),
new System.Drawing.Size(this.CellWidth, this.CellHeight),
this.ChannelNumber,
this.XmlFile
);
//ZTRect[] regions = HOGDetector.DetectorPeople(this.ImageFile);
ZTRect[] regions = detector.DetectorImage(this.ImageFile);
if (regions.Length <= 0)
{
MessageBox.Show("没有识别到");
return;
}
DrawImageAndRegion(this.ImageFile, regions);
}
///
/// 检查并填充参数
///
///
private Boolean CheckAndFillAllParameter()
{
this.XmlFile = this.txtXmlFile.Text.Trim();
if (string.IsNullOrEmpty(this.XmlFile))
{
ShowMessage("请填写结果Xml名");
return false;
}
if (!System.IO.File.Exists(this.XmlFile))
{
MessageBox.Show("xml不存在", "提示");
return false;
}
this.ImageFile = this.txtImgFile.Text.Trim();
if (string.IsNullOrEmpty(this.ImageFile))
{
ShowMessage("请选择要识别的图像");
return false;
}
if (!System.IO.File.Exists(this.ImageFile))
{
MessageBox.Show("Image不存在", "提示");
return false;
}
this.WinWidth = TypeConverter.StringToInt(this.txtWinWidth.Text, 0);
if (this.WinWidth <= 0)
{
ShowMessage("取样宽不合格");
return false;
}
this.WinHeight = TypeConverter.StringToInt(this.txtWinHeight.Text, 0);
if (this.WinHeight <= 0)
{
ShowMessage("取样高不合格");
return false;
}
this.BlockWidth = TypeConverter.StringToInt(this.txtBlockWidth.Text, 0);
if (this.BlockWidth <= 0 || (this.WinWidth % this.BlockWidth) != 0)
{
ShowMessage("Block宽不合格");
return false;
}
this.BlockHeight = TypeConverter.StringToInt(this.txtBlockHeight.Text, 0);
if (this.BlockHeight <= 0 || (this.WinHeight % this.BlockHeight) != 0)
{
ShowMessage("Block高不合格");
return false;
}
this.StrideWidth = TypeConverter.StringToInt(this.txtStrideWidth.Text, 0);
if (this.StrideWidth <= 0)
{
ShowMessage("Stride宽不合格");
return false;
}
this.StrideHeight = TypeConverter.StringToInt(this.txtStrideHeight.Text, 0);
if (this.StrideHeight <= 0)
{
ShowMessage("Stride高不合格");
return false;
}
this.CellWidth = TypeConverter.StringToInt(this.txtCellWidth.Text, 0);
if (this.CellWidth <= 0 || this.CellWidth * 2 != this.BlockWidth)
{
ShowMessage("Cell宽不合格");
return false;
}
this.CellHeight = TypeConverter.StringToInt(this.txtCellHeight.Text, 0);
if (this.CellHeight <= 0 || this.CellHeight * 2 != this.BlockHeight)
{
ShowMessage("Cell高不合格");
return false;
}
this.ChannelNumber = TypeConverter.StringToInt(this.txtChannelNumber.Text, 0);
if (this.ChannelNumber <= 0)
{
ShowMessage("channel不合格");
return false;
}
return true;
}
private void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
///
/// 打开训练工具
///
///
///
private void Train_Click(object sender, RoutedEventArgs e)
{
Trainor trainor = new Trainor();
trainor.ShowDialog();
}
///
/// 绘制图像和区域
///
///
///
private void DrawImageAndRegion(string imagePath, ZTRect[] regions)
{
byte[] imageData = File.ReadAllBytes(imagePath);
MemoryStream ms = new MemoryStream(imageData);
this.Dispatcher.BeginInvoke((Action)(() =>
{
this.container.Children.Clear();
////画图
Image imgControl = new Image();
imgControl.Source = BitmapFrame.Create(ms);
this.container.Children.Add(imgControl);
//画区域
for (int i = 0; i < regions.Length; i++)
{
CreatePathControl(this.container, regions[i]);
}
}));
}
///
/// 生成矩形框
///
///
///
private System.Windows.Shapes.Rectangle CreatePathControl(Canvas container, ZTRect rect)
{
Grid grid = new Grid();
System.Windows.Shapes.Rectangle rectControl = new System.Windows.Shapes.Rectangle();
grid.Children.Add(rectControl);
rectControl.StrokeThickness = 1;
rectControl.Stroke = new SolidColorBrush(Colors.Red);
rectControl.Width = rect.Rect.Width;
rectControl.Height = rect.Rect.Height;
TextBlock tb = new TextBlock();
grid.Children.Add(tb);
tb.Text = "top:" + rect.Rect.Top + "\r\nleft:" + rect.Rect.Left + "\r\nwidth:" + rect.Rect.Width + "\r\nheight:" + rect.Rect.Height + "\r\nscore:" + rect.Score;
tb.HorizontalAlignment = HorizontalAlignment.Left;
tb.Foreground = new SolidColorBrush(Colors.White);
container.Children.Add(grid);
Canvas.SetTop(grid, rect.Rect.Top);
Canvas.SetLeft(grid, rect.Rect.Left);
return rectControl;
}
private void Split_Click(object sender, RoutedEventArgs e)
{
Splitor splitor = new Splitor();
splitor.ShowDialog();
}
///
/// 选择Xml文件
///
///
///
private void SelXmlFile_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".xml";
ofd.Filter = "xml file|*.xml";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
this.txtXmlFile.Text = ofd.FileName;
}
}
///
/// 选择图片
///
///
///
private void SelImgFile_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.DefaultExt = ".jpg";
ofd.Filter = "jpg file|*.jpg";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
this.txtImgFile.Text = ofd.FileName;
}
}
}
}