asmrobot
2019-11-21 589ed88a5924a7494e21b95b6bbff5e46ff49ddd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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;
        }
 
 
    }
}