프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / Markus.ImageComparer / ImageComparer - 복사본.cs @ master

이력 | 보기 | 이력해설 | 다운로드 (13.3 KB)

1 2007ecaa taeseongkim
using Emgu.CV;
2
using Emgu.CV.Structure;
3
using System;
4
using System.Collections.Generic;
5
using System.Drawing;
6
using System.Drawing.Imaging;
7
using System.Linq;
8
using System.Text;
9
using System.Threading.Tasks;
10
11
namespace Markus.Image
12
{
13
    public class ImageComparer : ImageComparerBase
14
    {
15
        /// <summary>
16
        /// 이미지를 비교 후 원본 이미지에 Rect를 그린다.
17
        /// </summary>
18
        /// <param name="Originalbitmap">원본 이미지</param>
19
        /// <param name="TargatBitmap">비교대상 이미지</param>
20
        /// <param name="ResultRectSize">반환되는 Rect의 사이즈</param>
21
        /// <returns></returns>
22
        public Bitmap CompareDrawRects(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize)
23
        {
24
            var rects =  CompareReturnRects(Originalbitmap, TargatBitmap, ResultRectSize);
25
26
            if (rects.Count != 0)
27
            {
28
                using (Graphics g = Graphics.FromImage(Originalbitmap))
29
                {
30
                    var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height));
31
32
                    g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray());
33
                    g.Save();
34
35
                    g.Dispose();
36
                }
37
            }
38
39
            return Originalbitmap;
40
        }
41
42
        public System.Windows.Media.Imaging.BitmapSource CompareDrawRects(System.Windows.Media.Imaging.BitmapSource Originalbitmap, System.Windows.Media.Imaging.BitmapSource TargatBitmap, Size ResultRectSize)
43
        {
44
            var _Originalbitmap = CreateBitmapFromSource(Originalbitmap);
45
            var _TargatBitmap = CreateBitmapFromSource(TargatBitmap);
46
47
            var rects = CompareReturnRects(_Originalbitmap, _TargatBitmap, ResultRectSize);
48
         
49
            if (rects.Count != 0)
50
            {
51
                using (Graphics g = Graphics.FromImage(_Originalbitmap))
52
                {
53
                    var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height));
54
55
                    g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray());
56
                    g.Save();
57
58
                    g.Dispose();
59
                }
60
            }
61
62
            return CreateBitmapSourceFromBitmap(_Originalbitmap);
63
        }
64
65
        public List<System.Windows.Rect> CompareReturnRects(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize)
66
        {
67
            List<System.Windows.Rect> result = new List<System.Windows.Rect>();
68
69
            Bitmap Originalbitmap = null;
70
            Bitmap TargatBitmap = null;
71
72
            try
73
            {
74
75
                Originalbitmap = LoadPicture(OriginalbitmapUri);
76
                TargatBitmap = LoadPicture(TargatBitmapUri);
77
78
                result = CompareReturnRects(Originalbitmap, TargatBitmap, ResultRectSize);
79
            }
80
            catch (Exception)
81
            {
82
                throw;
83
            }
84
            finally
85
            {
86
                Originalbitmap.Dispose();
87
                TargatBitmap.Dispose();
88
            }
89
90
            return result;
91
        }
92
93
        /// <summary>
94
        /// 이미지를 비교 후 해당 영역을 Rect로 반환한다.
95
        /// </summary>
96
        /// <param name="Originalbitmap">원본 이미지</param>
97
        /// <param name="TargatBitmap">비교대상 이미지</param>
98
        /// <param name="ResultRectSize">반환되는 Rect의 사이즈</param>
99
        /// <returns></returns>
100
        public List<System.Windows.Rect> CompareReturnRects(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize)
101
        {
102
            List<System.Windows.Rect> result = new List<System.Windows.Rect>();
103
104
            try
105
            {
106
                Point[][] data = MathchesImageData(Originalbitmap, TargatBitmap);
107
108
                result =  GetMatchPixels(data, ResultRectSize);
109
            }
110
            catch (Exception ex)
111
            {
112
                throw ex;
113
            }
114
            finally
115
            {
116
117
            }
118
119
            return result;
120
        }
121
122
123
        /// <summary>
124
        /// 이미지를 비교 후 원본 이미지에 Rect를 그린다.
125
        /// 메모리 문제 발생
126
        /// </summary>
127
        /// <param name="Originalbitmap">원본 이미지</param>
128
        /// <param name="TargatBitmap">비교대상 이미지</param>
129
        /// <param name="ResultRectSize">반환되는 Rect의 사이즈</param>
130
        /// <returns></returns>
131
        public async Task<Bitmap> CompareDrawRectsAsync(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize)
132
        {
133
            System.Drawing.Bitmap cloneOriginal = Originalbitmap;
134
135
            var rects = await CompareReturnRectsAsync(cloneOriginal, TargatBitmap, ResultRectSize);
136
137
            if (rects.Count != 0)
138
            {
139
                using (Graphics g = Graphics.FromImage(cloneOriginal))
140
                {
141
                    var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height));
142
143
                    g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray());
144
                    g.Save();
145
146
                    g.Dispose();
147
                }
148
            }
149
150
            return cloneOriginal;
151
        }
152
153
        public async Task<Bitmap> CompareDrawRectsAsync(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize)
154
        {
155
            Bitmap Originalbitmap = null;
156
            Bitmap TargatBitmap = null;
157
158
            Originalbitmap = LoadPicture(OriginalbitmapUri);
159
            TargatBitmap = LoadPicture(TargatBitmapUri);
160
161
            System.Drawing.Bitmap cloneOriginal = Originalbitmap;
162
163
            var rects = await CompareReturnRectsAsync(cloneOriginal, TargatBitmap, ResultRectSize);
164
165
            if (rects.Count != 0)
166
            {
167
                using (Graphics g = Graphics.FromImage(cloneOriginal))
168
                {
169
                    var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height));
170
171
                    g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray());
172
                    g.Save();
173
174
                    g.Dispose();
175
                }
176
            }
177
178
            return cloneOriginal;
179
        }
180
181
182
        /// <summary>
183
        /// 이미지를 비교 후 원본 이미지에 Rect를 그린다.
184
        /// 메모리 문제 발생
185
        /// </summary>
186
        /// <param name="Originalbitmap">원본 이미지</param>
187
        /// <param name="TargatBitmap">비교대상 이미지</param>
188
        /// <param name="ResultRectSize">반환되는 Rect의 사이즈</param>
189
        /// <returns></returns>
190
        public async Task<System.Windows.Media.Imaging.BitmapSource> CompareDrawRectsAsync(System.Windows.Media.Imaging.BitmapSource Originalbitmap, System.Windows.Media.Imaging.BitmapSource TargatBitmap, Size ResultRectSize)
191
        {
192
193
            var _Originalbitmap = CreateBitmapFromSource(Originalbitmap);
194
            var _TargatBitmap = CreateBitmapFromSource(TargatBitmap);
195
196
            var rects = await CompareReturnRectsAsync(_Originalbitmap, _TargatBitmap, ResultRectSize);
197
198
            if (rects.Count != 0)
199
            {
200
                using (Graphics g = Graphics.FromImage(_Originalbitmap))
201
                {
202
                    var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height));
203
204
                    g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray());
205
                    g.Save();
206
207
                    g.Dispose();
208
                }
209
            }
210
211
            return CreateBitmapSourceFromBitmap(_Originalbitmap);
212
        }
213
214
        public Bitmap CreateBitmapFromSource(System.Windows.Media.Imaging.BitmapSource bitmapsource)
215
        {
216
            //convert image format
217
            var src = new System.Windows.Media.Imaging.FormatConvertedBitmap();
218
            src.BeginInit();
219
            src.Source = bitmapsource;
220
            src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr24;
221
            src.EndInit();
222
223
            //copy to bitmap
224
            Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
225
            var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
226
            src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
227
            bitmap.UnlockBits(data);
228
229
            return bitmap;
230
        }
231
232
        /// <summary>
233
        /// 메모리 문제 발생
234
        /// </summary>
235
        /// <param name="bitmap"></param>
236
        /// <returns></returns>
237
        public System.Windows.Media.Imaging.WriteableBitmap CreateWriteableBitmapFromBitmap(Bitmap bitmap)
238
        {
239
            System.Windows.Media.Imaging.WriteableBitmap result = null;
240
241
            if (bitmap == null)
242
                throw new ArgumentNullException("bitmap");
243
244
            try
245
            {
246
                int bytesPerPixel = 4;
247
248
                result = new System.Windows.Media.Imaging.WriteableBitmap(bitmap.Width, bitmap.Height,
249
                                                                   bitmap.HorizontalResolution ,bitmap.VerticalResolution,
250
                                                                   bitmap.PixelFormat.Convert(), null);
251
252
                Rectangle colorBitmapRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
253
                System.Windows.Int32Rect colorBitmapInt32Rect = new System.Windows.Int32Rect(0, 0, bitmap.Width, bitmap.Height);
254
255
                BitmapData data = bitmap.LockBits(colorBitmapRectangle, ImageLockMode.WriteOnly, bitmap.PixelFormat);
256
257
                result.WritePixels(colorBitmapInt32Rect, data.Scan0, data.Width * data.Height * bytesPerPixel, data.Stride);
258
259
                bitmap.UnlockBits(data);
260
            }
261
            catch (Exception ex)
262
            {
263
                //throw ex;
264
            }
265
            finally
266
            {
267
                bitmap.Dispose();
268
                bitmap = null;
269
                //GC.Collect(2);
270
            }
271
272
            return result;
273
        }
274
    
275
        /// <summary>
276
        /// 메모리 문제 발생
277
        /// </summary>
278
        /// <param name="bitmap"></param>
279
        /// <returns></returns>
280
        public System.Windows.Media.Imaging.BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap)
281
        {
282
            System.Windows.Media.Imaging.BitmapSource result = null;
283
            
284
            if (bitmap == null)
285
                throw new ArgumentNullException("bitmap");
286
287
            try
288
            {
289
                using (var hbitmap = new SafeHBitmapHandle(bitmap.GetHbitmap(), true))
290
                {
291
292
                    result = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
293
                        hbitmap.DangerousGetHandle(),
294
                        IntPtr.Zero,
295
                        System.Windows.Int32Rect.Empty,
296
                        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
297
298
                   
299
                }
300
            }
301
            catch (Exception ex)
302
            {
303
                //throw ex;
304
            }
305
            finally
306
            {
307
                bitmap.Dispose();
308
                bitmap = null;
309
                //GC.Collect(2);
310
            }
311
312
            return result;
313
        }
314
315
        public async Task<List<System.Windows.Rect>> CompareReturnRectsAsync(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize)
316
        {
317
            List<System.Windows.Rect> result = new List<System.Windows.Rect>();
318
319
            Bitmap Originalbitmap = null;
320
            Bitmap TargatBitmap = null;
321
322
            try
323
            {
324
325
                Originalbitmap = LoadPicture(OriginalbitmapUri);
326
                TargatBitmap = LoadPicture(TargatBitmapUri);
327
328
                result = await CompareReturnRectsAsync(Originalbitmap, TargatBitmap, ResultRectSize);
329
            }
330
            catch (Exception)
331
            {
332
                throw;
333
            }
334
            finally
335
            {
336
                Originalbitmap.Dispose();
337
                TargatBitmap.Dispose();
338
            }
339
340
            return result;
341
        }
342
343
        /// <summary>
344
        /// 이미지를 비교 후 해당 영역을 Rect로 반환한다.
345
        /// </summary>
346
        /// <param name="Originalbitmap">원본 이미지</param>
347
        /// <param name="TargatBitmap">비교대상 이미지</param>
348
        /// <param name="ResultRectSize">반환되는 Rect의 사이즈</param>
349
        /// <returns></returns>
350
        public async Task<List<System.Windows.Rect>> CompareReturnRectsAsync(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize)
351
        {
352
            List<System.Windows.Rect> result = new List<System.Windows.Rect>();
353
354
            try
355
            {
356
                byte[,,] data = MathchesImageData(Originalbitmap, TargatBitmap);
357
358
                result = await GetMatchPixelsAsnc(data, ResultRectSize);
359
         
360
                data = null;
361
            }
362
            catch (Exception ex)
363
            {
364
                throw ex;
365
            }
366
            finally
367
            {
368
            }
369
370
            return result;
371
        }
372
373
       
374
        protected override void Dispose(bool disposing)
375
        {
376
            base.Dispose(disposing);
377
        }
378
    }
379
}
클립보드 이미지 추가 (최대 크기: 500 MB)