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
|
{
|
/// <summary>
|
/// Splitor.xaml 的交互逻辑
|
/// </summary>
|
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; }
|
|
|
|
|
/// <summary>
|
/// 打开背景图
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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)
|
{
|
|
}
|
|
/// <summary>
|
/// 开始生成
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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<Rect> rects = GetRects(mat.Width, mat.Height);
|
Image<Gray, byte> image = mat.ToImage<Gray, byte>();
|
|
Image<Gray, byte> 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");
|
}
|
|
/// <summary>
|
/// 添加排除区域
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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;
|
}
|
|
/// <summary>
|
/// 区域删除事件
|
/// </summary>
|
/// <param name="obj"></param>
|
private void Area_OnClose(SplitArea obj)
|
{
|
this.container.Children.Remove(obj);
|
}
|
|
/// <summary>
|
/// 选择导出文件夹
|
/// </summary>
|
/// <param name="sender"></param>
|
/// <param name="e"></param>
|
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;
|
}
|
|
/// <summary>
|
/// 填充参数
|
/// </summary>
|
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);
|
}
|
|
/// <summary>
|
/// 得到限制区域
|
/// </summary>
|
/// <param name="maxWidth"></param>
|
/// <param name="maxHeight"></param>
|
/// <returns></returns>
|
private List<Rect> GetRects(Int32 maxWidth,Int32 maxHeight)
|
{
|
List<Rect> rects = new List<Rect>();
|
|
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<Rect> 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;
|
}
|
|
|
}
|
}
|