using System; using System.Collections.Generic; 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.Shapes; using Emgu.CV; using Emgu.CV.Structure; using ZTImage; using forms = System.Windows.Forms; namespace RichCreator.Editor.Tools.CV { /// /// Splitor.xaml 的交互逻辑 /// public partial class Splitor : Window { public Splitor() { InitializeComponent(); } public string ExportDir { get; set; } public int WinSizeWidth { get; set; } public int WinSizeHeight { get; set; } public int StrideSizeWidth { get; set; } public int StrideSizeHeight { get; set; } /// /// 打开背景图 /// /// /// private void OpenPicture_Click(object sender, RoutedEventArgs e) { imagePath = string.Empty; Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.DefaultExt = ".jpg"; ofd.Filter = "jpg file|*.jpg"; ofd.Multiselect = false; if (ofd.ShowDialog() == true) { imagePath = ofd.FileName; } else { return; } this.container.Children.Clear(); byte[] imageData = System.IO.File.ReadAllBytes(imagePath); var ms = new System.IO.MemoryStream(imageData); //画图 Image imgControl = new Image(); imgControl.Source = BitmapFrame.Create(ms); this.container.Children.Add(imgControl); } string imagePath; private void AutoGenericRect_Click(object sender, RoutedEventArgs e) { } /// /// 开始生成 /// /// /// private void GenericPicture_Click(object sender, RoutedEventArgs e) { FillParameter(); if (string.IsNullOrEmpty(this.imagePath)) { ShowMessage("请选择要分割的图片"); return; } if (!System.IO.File.Exists(this.imagePath)) { ShowMessage("请选择要分割的图片"); return; } if (string.IsNullOrEmpty(this.ExportDir)) { ShowMessage("请选择要导出到的目录"); return; } if (!System.IO.Directory.Exists(this.ExportDir)) { ShowMessage("请选择要导出到的目录"); return; } Mat mat = CvInvoke.Imread(this.imagePath); if (this.WinSizeWidth<=0||this.WinSizeHeight<=0||this.WinSizeWidth > mat.Width||this.WinSizeHeight>mat.Height) { ShowMessage("请设置合适的win size大小"); return; } if (this.StrideSizeWidth <= 0 || this.StrideSizeHeight <= 0 || this.StrideSizeWidth > mat.Width || this.StrideSizeHeight > mat.Height) { ShowMessage("请设置合适的stride size大小"); return; } List rects = GetRects(mat.Width, mat.Height); Image image = mat.ToImage(); Image im = null; Random r = new Random(); for (int h = 0; h <= (mat.Height-WinSizeHeight); h+=this.StrideSizeHeight) { for (int w = 0; w <= (mat.Width - WinSizeWidth); w += this.StrideSizeWidth) { bool isin = IsInRect(w, h, this.WinSizeWidth, this.WinSizeHeight, rects); if (isin) { continue; } im = image.GetSubRect(new System.Drawing.Rectangle(w, h, this.WinSizeWidth, this.WinSizeHeight)); im.Save(System.IO.Path.Combine(this.ExportDir, Guid.NewGuid().ToString() + ".jpg")); } } MessageBox.Show("splitor complete"); } /// /// 添加排除区域 /// /// /// private void AddExcArea_Click(object sender, RoutedEventArgs e) { FillParameter(); SplitArea area = new SplitArea(); area.OnClose += Area_OnClose; this.container.Children.Add(area); area.MinWidth = this.WinSizeWidth; area.MinHeight = this.WinSizeHeight; } /// /// 区域删除事件 /// /// private void Area_OnClose(SplitArea obj) { this.container.Children.Remove(obj); } /// /// 选择导出文件夹 /// /// /// private void SelExportDir_Click(object sender, RoutedEventArgs e) { //选择负样本目录 forms.FolderBrowserDialog m_Dialog = new forms.FolderBrowserDialog(); m_Dialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory; forms.DialogResult result = m_Dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.Cancel) { return; } string m_Dir = m_Dialog.SelectedPath.Trim(); this.txtExportDir.Text = m_Dir; } /// /// 填充参数 /// private void FillParameter() { this.ExportDir = this.txtExportDir.Text; this.WinSizeWidth = TypeConverter.StringToInt(this.txtWinWidth.Text,0); this.WinSizeHeight = TypeConverter.StringToInt(this.txtWinHeight.Text, 0); this.StrideSizeWidth = TypeConverter.StringToInt(this.txtStrideWidth.Text, 0); this.StrideSizeHeight = TypeConverter.StringToInt(this.txtStrideHeight.Text, 0); } private void ShowMessage(string msg) { MessageBox.Show(msg); } /// /// 得到限制区域 /// /// /// /// private List GetRects(Int32 maxWidth,Int32 maxHeight) { List rects = new List(); foreach (var item in this.container.Children) { SplitArea area = item as SplitArea; if (area == null) { continue; } Rect rect = area.GetRect(); if (rect.X < 0 || rect.Y < 0 || (rect.X + rect.Width) > MaxWidth || (rect.Y + rect.Height) > MaxHeight) { continue; } rects.Add(rect); } return rects; } private bool IsInRect(Int32 x, Int32 y, Int32 width, Int32 height, List rects) { if (rects == null || rects.Count <= 0) { return false; } Int32 xe = x + width; Int32 ye = y + height; Rect r; for (int i = 0; i < rects.Count; i++) { r = rects[i]; if (x >= r.X && x <= (r.X + r.Width) && y >= r.Y && y <= (r.Y + r.Height)) { return true; } if (xe >= r.X && xe <= (r.X + r.Width) && ye >= r.Y && ye <= (r.Y + r.Height)) { return true; } } return false; } } }