개정판 67994a4d
파일명 수정
Change-Id: I3d0fe919b64a7e994fcf2e6a27c6397976bb2316
ImageComparer/Markus.ImageComparer/ImageComparer.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.Drawing; |
|
4 |
using System.Drawing.Imaging; |
|
5 |
using System.Linq; |
|
6 |
using System.Text; |
|
7 |
using System.Threading.Tasks; |
|
8 |
|
|
9 |
namespace Markus.Image |
|
10 |
{ |
|
11 |
public class ImageCompare : ImageCompareBase |
|
12 |
{ |
|
13 |
/// <summary> |
|
14 |
/// 이미지를 비교 후 원본 이미지에 Rect를 그린다. |
|
15 |
/// </summary> |
|
16 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
17 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
18 |
/// <param name="ResultRectSize">반환되는 Rect의 사이즈</param> |
|
19 |
/// <returns></returns> |
|
20 |
public Bitmap CompareDrawRects(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize) |
|
21 |
{ |
|
22 |
var rects = CompareReturnRects(Originalbitmap, TargatBitmap, ResultRectSize); |
|
23 |
|
|
24 |
if (rects.Count != 0) |
|
25 |
{ |
|
26 |
using (Graphics g = Graphics.FromImage(Originalbitmap)) |
|
27 |
{ |
|
28 |
var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height)).ToList(); |
|
29 |
|
|
30 |
g.DrawRectangles(new Pen(Brushes.Red, 5f), rect.ToArray()); |
|
31 |
g.Save(); |
|
32 |
|
|
33 |
g.Dispose(); |
|
34 |
} |
|
35 |
} |
|
36 |
|
|
37 |
return Originalbitmap; |
|
38 |
} |
|
39 |
|
|
40 |
public System.Windows.Media.Imaging.BitmapSource CompareDrawRects(System.Windows.Media.Imaging.BitmapSource Originalbitmap, System.Windows.Media.Imaging.BitmapSource TargatBitmap, Size ResultRectSize) |
|
41 |
{ |
|
42 |
var _Originalbitmap = CreateBitmapFromSource(Originalbitmap); |
|
43 |
var _TargatBitmap = CreateBitmapFromSource(TargatBitmap); |
|
44 |
|
|
45 |
var rects = CompareReturnRects(_Originalbitmap, _TargatBitmap, ResultRectSize); |
|
46 |
|
|
47 |
if (rects.Count != 0) |
|
48 |
{ |
|
49 |
using (Graphics g = Graphics.FromImage(_Originalbitmap)) |
|
50 |
{ |
|
51 |
var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height)); |
|
52 |
|
|
53 |
g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray()); |
|
54 |
g.Save(); |
|
55 |
|
|
56 |
g.Dispose(); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
return CreateBitmapSourceFromBitmap(_Originalbitmap); |
|
61 |
} |
|
62 |
|
|
63 |
public List<System.Windows.Rect> CompareReturnRects(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize) |
|
64 |
{ |
|
65 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
66 |
|
|
67 |
Bitmap Originalbitmap = null; |
|
68 |
Bitmap TargatBitmap = null; |
|
69 |
|
|
70 |
try |
|
71 |
{ |
|
72 |
Originalbitmap = LoadPicture(OriginalbitmapUri); |
|
73 |
TargatBitmap = LoadPicture(TargatBitmapUri); |
|
74 |
|
|
75 |
result = CompareReturnRects(Originalbitmap, TargatBitmap, ResultRectSize); |
|
76 |
} |
|
77 |
catch (Exception) |
|
78 |
{ |
|
79 |
throw; |
|
80 |
} |
|
81 |
finally |
|
82 |
{ |
|
83 |
Originalbitmap.Dispose(); |
|
84 |
TargatBitmap.Dispose(); |
|
85 |
} |
|
86 |
|
|
87 |
return result; |
|
88 |
} |
|
89 |
|
|
90 |
/// <summary> |
|
91 |
/// 이미지를 비교 후 해당 영역을 Rect로 반환한다. |
|
92 |
/// </summary> |
|
93 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
94 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
95 |
/// <param name="ResultRectSize">반환되는 Rect의 사이즈</param> |
|
96 |
/// <returns></returns> |
|
97 |
public List<System.Windows.Rect> CompareReturnRects(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize) |
|
98 |
{ |
|
99 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
100 |
|
|
101 |
try |
|
102 |
{ |
|
103 |
List<System.Windows.Rect> rects = new List<System.Windows.Rect>(); |
|
104 |
|
|
105 |
var data = MathchesImageData(Originalbitmap, TargatBitmap); |
|
106 |
|
|
107 |
result = GetMatchPixels(data, ResultRectSize); |
|
108 |
|
|
109 |
//if (rects.Count() > 0) |
|
110 |
//{ |
|
111 |
// result = Merge(rects, ResultRectSize.Height); |
|
112 |
//} |
|
113 |
|
|
114 |
//result = JoinRectList(rects); |
|
115 |
} |
|
116 |
catch (Exception ex) |
|
117 |
{ |
|
118 |
throw ex; |
|
119 |
} |
|
120 |
finally |
|
121 |
{ |
|
122 |
|
|
123 |
} |
|
124 |
|
|
125 |
return result; |
|
126 |
} |
|
127 |
|
|
128 |
/// <summary> |
|
129 |
/// 이미지를 비교 후 원본 이미지에 Rect를 그린다. |
|
130 |
/// 메모리 문제 발생 |
|
131 |
/// </summary> |
|
132 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
133 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
134 |
/// <param name="ResultRectSize">반환되는 Rect의 사이즈</param> |
|
135 |
/// <returns></returns> |
|
136 |
public async Task<Bitmap> CompareDrawRectsAsync(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize) |
|
137 |
{ |
|
138 |
System.Drawing.Bitmap cloneOriginal = Originalbitmap; |
|
139 |
|
|
140 |
var rects = await CompareReturnRectsAsync(cloneOriginal, TargatBitmap, ResultRectSize); |
|
141 |
|
|
142 |
if (rects.Count != 0) |
|
143 |
{ |
|
144 |
using (Graphics g = Graphics.FromImage(cloneOriginal)) |
|
145 |
{ |
|
146 |
|
|
147 |
var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height)); |
|
148 |
|
|
149 |
g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray()); |
|
150 |
g.Save(); |
|
151 |
|
|
152 |
g.Dispose(); |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
return cloneOriginal; |
|
157 |
} |
|
158 |
|
|
159 |
/// <summary> |
|
160 |
/// 이미지를 비교 후 원본 이미지에 Rect를 그린다. |
|
161 |
/// 메모리 문제 발생 |
|
162 |
/// </summary> |
|
163 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
164 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
165 |
/// <param name="ResultRectSize">반환되는 Rect의 사이즈</param> |
|
166 |
/// <returns></returns> |
|
167 |
public async Task<System.Windows.Media.Imaging.BitmapSource> CompareDrawRectsAsync(System.Windows.Media.Imaging.BitmapSource Originalbitmap, System.Windows.Media.Imaging.BitmapSource TargatBitmap, Size ResultRectSize) |
|
168 |
{ |
|
169 |
|
|
170 |
var _Originalbitmap = CreateBitmapFromSource(Originalbitmap); |
|
171 |
var _TargatBitmap = CreateBitmapFromSource(TargatBitmap); |
|
172 |
|
|
173 |
var rects = await CompareReturnRectsAsync(_Originalbitmap, _TargatBitmap, ResultRectSize); |
|
174 |
|
|
175 |
if (rects.Count != 0) |
|
176 |
{ |
|
177 |
using (Graphics g = Graphics.FromImage(_Originalbitmap)) |
|
178 |
{ |
|
179 |
var rect = rects.Select(x => new System.Drawing.Rectangle((int)x.X, (int)x.Y, (int)x.Width, (int)x.Height)); |
|
180 |
|
|
181 |
g.DrawRectangles(new Pen(Brushes.Blue, 3f), rect.ToArray()); |
|
182 |
g.Save(); |
|
183 |
|
|
184 |
g.Dispose(); |
|
185 |
} |
|
186 |
} |
|
187 |
|
|
188 |
return CreateBitmapSourceFromBitmap(_Originalbitmap); |
|
189 |
} |
|
190 |
|
|
191 |
public Bitmap CreateBitmapFromSource(System.Windows.Media.Imaging.BitmapSource bitmapsource) |
|
192 |
{ |
|
193 |
//convert image format |
|
194 |
var src = new System.Windows.Media.Imaging.FormatConvertedBitmap(); |
|
195 |
src.BeginInit(); |
|
196 |
src.Source = bitmapsource; |
|
197 |
src.DestinationFormat = System.Windows.Media.PixelFormats.Bgr24; |
|
198 |
src.EndInit(); |
|
199 |
|
|
200 |
//copy to bitmap |
|
201 |
Bitmap bitmap = new Bitmap(src.PixelWidth, src.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
|
202 |
var data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); |
|
203 |
src.CopyPixels(System.Windows.Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); |
|
204 |
bitmap.UnlockBits(data); |
|
205 |
|
|
206 |
return bitmap; |
|
207 |
} |
|
208 |
|
|
209 |
/// <summary> |
|
210 |
/// 메모리 문제 발생 |
|
211 |
/// </summary> |
|
212 |
/// <param name="bitmap"></param> |
|
213 |
/// <returns></returns> |
|
214 |
public System.Windows.Media.Imaging.WriteableBitmap CreateWriteableBitmapFromBitmap(Bitmap bitmap) |
|
215 |
{ |
|
216 |
System.Windows.Media.Imaging.WriteableBitmap result = null; |
|
217 |
|
|
218 |
if (bitmap == null) |
|
219 |
throw new ArgumentNullException("bitmap"); |
|
220 |
|
|
221 |
try |
|
222 |
{ |
|
223 |
int bytesPerPixel = 4; |
|
224 |
|
|
225 |
result = new System.Windows.Media.Imaging.WriteableBitmap(bitmap.Width, bitmap.Height, |
|
226 |
bitmap.HorizontalResolution ,bitmap.VerticalResolution, |
|
227 |
bitmap.PixelFormat.Convert(), null); |
|
228 |
|
|
229 |
Rectangle colorBitmapRectangle = new Rectangle(0, 0, bitmap.Width, bitmap.Height); |
|
230 |
System.Windows.Int32Rect colorBitmapInt32Rect = new System.Windows.Int32Rect(0, 0, bitmap.Width, bitmap.Height); |
|
231 |
|
|
232 |
BitmapData data = bitmap.LockBits(colorBitmapRectangle, ImageLockMode.WriteOnly, bitmap.PixelFormat); |
|
233 |
|
|
234 |
result.WritePixels(colorBitmapInt32Rect, data.Scan0, data.Width * data.Height * bytesPerPixel, data.Stride); |
|
235 |
|
|
236 |
bitmap.UnlockBits(data); |
|
237 |
} |
|
238 |
catch (Exception ex) |
|
239 |
{ |
|
240 |
//throw ex; |
|
241 |
} |
|
242 |
finally |
|
243 |
{ |
|
244 |
bitmap.Dispose(); |
|
245 |
bitmap = null; |
|
246 |
//GC.Collect(2); |
|
247 |
} |
|
248 |
|
|
249 |
return result; |
|
250 |
} |
|
251 |
|
|
252 |
/// <summary> |
|
253 |
/// 메모리 문제 발생 |
|
254 |
/// </summary> |
|
255 |
/// <param name="bitmap"></param> |
|
256 |
/// <returns></returns> |
|
257 |
public System.Windows.Media.Imaging.BitmapSource CreateBitmapSourceFromBitmap(Bitmap bitmap) |
|
258 |
{ |
|
259 |
System.Windows.Media.Imaging.BitmapSource result = null; |
|
260 |
|
|
261 |
if (bitmap == null) |
|
262 |
throw new ArgumentNullException("bitmap"); |
|
263 |
|
|
264 |
try |
|
265 |
{ |
|
266 |
using (var hbitmap = new SafeHBitmapHandle(bitmap.GetHbitmap(), true)) |
|
267 |
{ |
|
268 |
|
|
269 |
result = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( |
|
270 |
hbitmap.DangerousGetHandle(), |
|
271 |
IntPtr.Zero, |
|
272 |
System.Windows.Int32Rect.Empty, |
|
273 |
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); |
|
274 |
|
|
275 |
|
|
276 |
} |
|
277 |
} |
|
278 |
catch (Exception ex) |
|
279 |
{ |
|
280 |
//throw ex; |
|
281 |
} |
|
282 |
finally |
|
283 |
{ |
|
284 |
bitmap.Dispose(); |
|
285 |
bitmap = null; |
|
286 |
//GC.Collect(2); |
|
287 |
} |
|
288 |
|
|
289 |
return result; |
|
290 |
} |
|
291 |
|
|
292 |
public async Task<List<System.Windows.Rect>> CompareReturnRectsAsync(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize) |
|
293 |
{ |
|
294 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
295 |
|
|
296 |
Bitmap Originalbitmap = null; |
|
297 |
Bitmap TargatBitmap = null; |
|
298 |
|
|
299 |
try |
|
300 |
{ |
|
301 |
|
|
302 |
Originalbitmap = LoadPicture(OriginalbitmapUri); |
|
303 |
TargatBitmap = LoadPicture(TargatBitmapUri); |
|
304 |
|
|
305 |
result = await CompareReturnRectsAsync(Originalbitmap, TargatBitmap, ResultRectSize); |
|
306 |
} |
|
307 |
catch (Exception) |
|
308 |
{ |
|
309 |
throw; |
|
310 |
} |
|
311 |
finally |
|
312 |
{ |
|
313 |
Originalbitmap.Dispose(); |
|
314 |
TargatBitmap.Dispose(); |
|
315 |
} |
|
316 |
|
|
317 |
return result; |
|
318 |
} |
|
319 |
|
|
320 |
public async Task<System.Windows.Media.Imaging.BitmapSource> CompareDrawRectsAsync(string OriginalbitmapUri, string TargatBitmapUri, Size ResultRectSize) |
|
321 |
{ |
|
322 |
System.Windows.Media.Imaging.BitmapSource result = null; |
|
323 |
|
|
324 |
Bitmap Originalbitmap = null; |
|
325 |
Bitmap TargatBitmap = null; |
|
326 |
|
|
327 |
try |
|
328 |
{ |
|
329 |
|
|
330 |
Originalbitmap = LoadPicture(OriginalbitmapUri); |
|
331 |
TargatBitmap = LoadPicture(TargatBitmapUri); |
|
332 |
|
|
333 |
var bitmap = await CompareDrawRectsAsync(Originalbitmap, TargatBitmap, ResultRectSize); |
|
334 |
|
|
335 |
result = CreateBitmapSourceFromBitmap(bitmap); |
|
336 |
} |
|
337 |
catch (Exception) |
|
338 |
{ |
|
339 |
throw; |
|
340 |
} |
|
341 |
finally |
|
342 |
{ |
|
343 |
Originalbitmap.Dispose(); |
|
344 |
TargatBitmap.Dispose(); |
|
345 |
} |
|
346 |
|
|
347 |
return result; |
|
348 |
} |
|
349 |
|
|
350 |
/// <summary> |
|
351 |
/// 이미지를 비교 후 해당 영역을 Rect로 반환한다. |
|
352 |
/// </summary> |
|
353 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
354 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
355 |
/// <param name="ResultRectSize">반환되는 Rect의 사이즈</param> |
|
356 |
/// <returns></returns> |
|
357 |
public async Task<List<System.Windows.Rect>> CompareReturnRectsAsync(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap, Size ResultRectSize) |
|
358 |
{ |
|
359 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
360 |
|
|
361 |
try |
|
362 |
{ |
|
363 |
var data = MathchesImageData(Originalbitmap, TargatBitmap); |
|
364 |
|
|
365 |
result = await GetMatchPixelsAsnc(data, ResultRectSize); |
|
366 |
|
|
367 |
//result = JoinRectList(result); |
|
368 |
//if (result.Count() > 0) |
|
369 |
//{ |
|
370 |
// result = Merge(result, ResultRectSize.Height); |
|
371 |
//} |
|
372 |
|
|
373 |
data = null; |
|
374 |
} |
|
375 |
catch (Exception ex) |
|
376 |
{ |
|
377 |
throw ex; |
|
378 |
} |
|
379 |
finally |
|
380 |
{ |
|
381 |
} |
|
382 |
|
|
383 |
return result; |
|
384 |
} |
|
385 |
|
|
386 |
|
|
387 |
protected override void Dispose(bool disposing) |
|
388 |
{ |
|
389 |
base.Dispose(disposing); |
|
390 |
} |
|
391 |
} |
|
392 |
} |
ImageComparer/Markus.ImageComparer/ImageComparerBase.cs | ||
---|---|---|
1 |
using OpenCvSharp; |
|
2 |
using System; |
|
3 |
using System.Collections.Generic; |
|
4 |
using System.Drawing; |
|
5 |
using System.Drawing.Imaging; |
|
6 |
using System.IO; |
|
7 |
using System.Linq; |
|
8 |
using System.Net; |
|
9 |
using System.Runtime.InteropServices; |
|
10 |
using System.Text; |
|
11 |
using System.Threading.Tasks; |
|
12 |
using Point = System.Drawing.Point; |
|
13 |
using Size = System.Drawing.Size; |
|
14 |
|
|
15 |
namespace Markus.Image |
|
16 |
{ |
|
17 |
public class ImageCompareBase : IDisposable |
|
18 |
{ |
|
19 |
Mat OriginalImageData = null; |
|
20 |
Mat TargatImageData = null; |
|
21 |
|
|
22 |
double contoursLongCount = 0; |
|
23 |
double ComparisonCount = 0; |
|
24 |
public CompareProgress Progress = new CompareProgress(); |
|
25 |
|
|
26 |
private void SetStatus(string message,double percentage,CompareStatus status) |
|
27 |
{ |
|
28 |
System.Diagnostics.Debug.WriteLine(percentage); |
|
29 |
|
|
30 |
Progress.Message = message; |
|
31 |
Progress.Percentage = percentage; |
|
32 |
Progress.Status = status; |
|
33 |
} |
|
34 |
|
|
35 |
/// <summary> |
|
36 |
/// Originalbitmap에서 TargatBitmap과 비교하여 틀린 부분의 데이터를 Emgu.CV.TDepth형식으로 반환한다. |
|
37 |
/// </summary> |
|
38 |
/// <param name="Originalbitmap">원본 이미지</param> |
|
39 |
/// <param name="TargatBitmap">비교대상 이미지</param> |
|
40 |
/// <returns>Emgu.CV.TDepth형식의 byte[,,]</returns> |
|
41 |
protected Mat MathchesImageData(System.Drawing.Bitmap Originalbitmap, System.Drawing.Bitmap TargatBitmap) |
|
42 |
{ |
|
43 |
Mat result = new Mat(); |
|
44 |
|
|
45 |
try |
|
46 |
{ |
|
47 |
SetStatus("Image Load", 0, CompareStatus.Loading); |
|
48 |
|
|
49 |
Originalbitmap = ChangeBitmapFormatAndSize(Originalbitmap, Originalbitmap.Size, PixelFormat.Format24bppRgb); |
|
50 |
TargatBitmap = ChangeBitmapFormatAndSize(TargatBitmap, Originalbitmap.Size, PixelFormat.Format24bppRgb); |
|
51 |
|
|
52 |
// 원본이미지의 크키와 Format24bppRgb로 타켓 이미지를 변경 |
|
53 |
// 크기가 틀린 경우 비교시 바이트배열 오류 발생 |
|
54 |
OriginalImageData = OpenCvSharp.Extensions.BitmapConverter.ToMat(Originalbitmap); |
|
55 |
TargatImageData = OpenCvSharp.Extensions.BitmapConverter.ToMat(TargatBitmap); |
|
56 |
|
|
57 |
if (OriginalImageData.Size() != TargatImageData.Size()) |
|
58 |
{ |
|
59 |
Cv2.Resize(TargatImageData, TargatImageData, OriginalImageData.Size()); |
|
60 |
} |
|
61 |
|
|
62 |
Cv2.CvtColor(OriginalImageData, OriginalImageData, ColorConversionCodes.BGR2GRAY); |
|
63 |
Cv2.CvtColor(TargatImageData, TargatImageData, ColorConversionCodes.BGR2GRAY); |
|
64 |
|
|
65 |
Mat outputData = new Mat(TargatImageData.Size(), MatType.CV_8UC1); |
|
66 |
|
|
67 |
Cv2.Absdiff(OriginalImageData, TargatImageData, outputData); |
|
68 |
|
|
69 |
// 틀린부분을 반환 |
|
70 |
Cv2.BitwiseNot(outputData, result); |
|
71 |
} |
|
72 |
catch (Exception ex) |
|
73 |
{ |
|
74 |
throw ex; |
|
75 |
} |
|
76 |
finally |
|
77 |
{ |
|
78 |
} |
|
79 |
|
|
80 |
return result; |
|
81 |
} |
|
82 |
|
|
83 |
protected System.Drawing.Bitmap ChangeBitmapFormatAndSize(System.Drawing.Bitmap bitmap, Size newSize, PixelFormat pixelFormat) |
|
84 |
{ |
|
85 |
Bitmap result = bitmap; |
|
86 |
|
|
87 |
if (pixelFormat != bitmap.PixelFormat) |
|
88 |
{ |
|
89 |
Point originPoint = new Point(0, 0); |
|
90 |
Rectangle rect = new Rectangle(originPoint, bitmap.Size); |
|
91 |
result = bitmap.Clone(rect, pixelFormat); |
|
92 |
} |
|
93 |
|
|
94 |
if (bitmap.Size != newSize) |
|
95 |
{ |
|
96 |
result = new Bitmap(newSize.Width, newSize.Height); |
|
97 |
|
|
98 |
using (Graphics g = Graphics.FromImage(result)) |
|
99 |
{ |
|
100 |
g.DrawImage(bitmap, 0, 0, newSize.Width, newSize.Height); |
|
101 |
g.Dispose(); |
|
102 |
} |
|
103 |
} |
|
104 |
|
|
105 |
return result; |
|
106 |
} |
|
107 |
|
|
108 |
/// <summary> |
|
109 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
|
110 |
/// </summary> |
|
111 |
/// <param name="data"></param> |
|
112 |
/// <param name="block"></param> |
|
113 |
/// <returns></returns> |
|
114 |
protected List<System.Windows.Rect> GetMatchPixels(Mat data, Size block) |
|
115 |
{ |
|
116 |
SetStatus("Detection", 0, CompareStatus.Detection); |
|
117 |
|
|
118 |
int width = data.Cols; |
|
119 |
int height = data.Rows; |
|
120 |
|
|
121 |
List<System.Windows.Rect> results = new List<System.Windows.Rect>(); |
|
122 |
|
|
123 |
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); |
|
124 |
stopwatch.Start(); |
|
125 |
|
|
126 |
Mat outputMat = new Mat(data.Size(), MatType.CV_8UC1); |
|
127 |
|
|
128 |
HierarchyIndex[] hierarchy; |
|
129 |
OpenCvSharp.Point[][] contours; |
|
130 |
|
|
131 |
OpenCvSharp.Point testpoint = new OpenCvSharp.Point(); |
|
132 |
|
|
133 |
Cv2.Threshold(data, data, 0, 0,ThresholdTypes.BinaryInv); |
|
134 |
|
|
135 |
Cv2.FindContours(data, out contours, out hierarchy,RetrievalModes.List,ContourApproximationModes.ApproxNone, testpoint); |
|
136 |
|
|
137 |
SetStatus("Comparison", 0, CompareStatus.Comparison); |
|
138 |
contoursLongCount = contours.Sum(x => x.Count()); |
|
139 |
|
|
140 |
var rects = contours.AsParallel() |
|
141 |
.Select(points => GetRectList(points, block)) |
|
142 |
.SelectMany(x => x); |
|
143 |
|
|
144 |
results.AddRange(rects); |
|
145 |
|
|
146 |
return results; |
|
147 |
} |
|
148 |
|
|
149 |
private List<System.Windows.Rect> GetRectList(OpenCvSharp.Point[] points,Size block) |
|
150 |
{ |
|
151 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
152 |
|
|
153 |
for (int i = 0; i < points.Count(); i++) |
|
154 |
{ |
|
155 |
var rect = new System.Windows.Rect |
|
156 |
{ |
|
157 |
X = points[i].X - block.Width / 2, |
|
158 |
Y = points[i].Y - block.Height / 2, |
|
159 |
Width = block.Width, |
|
160 |
Height = block.Height |
|
161 |
}; |
|
162 |
|
|
163 |
if (result.Count(r => r.IntersectsWith(rect)) == 0) |
|
164 |
{ |
|
165 |
result.Add(rect); |
|
166 |
} |
|
167 |
|
|
168 |
ComparisonCount++; |
|
169 |
|
|
170 |
SetStatus("Comparison", ComparisonCount/contoursLongCount*100, CompareStatus.Comparison); |
|
171 |
} |
|
172 |
|
|
173 |
return result; |
|
174 |
} |
|
175 |
|
|
176 |
/// <summary> |
|
177 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
|
178 |
/// </summary> |
|
179 |
/// <param name="data"></param> |
|
180 |
/// <param name="block"></param> |
|
181 |
/// <returns></returns> |
|
182 |
protected async Task<List<System.Windows.Rect>> GetMatchPixelsAsnc(Mat data, Size block) |
|
183 |
{ |
|
184 |
return await Task.Factory.StartNew<List<System.Windows.Rect>>(() => |
|
185 |
{ |
|
186 |
return GetMatchPixels(data, block); |
|
187 |
}); |
|
188 |
} |
|
189 |
|
|
190 |
protected Bitmap LoadPicture(string url) |
|
191 |
{ |
|
192 |
HttpWebRequest wreq; |
|
193 |
HttpWebResponse wresp; |
|
194 |
Stream mystream; |
|
195 |
Bitmap bmp; |
|
196 |
|
|
197 |
bmp = null; |
|
198 |
mystream = null; |
|
199 |
wresp = null; |
|
200 |
try |
|
201 |
{ |
|
202 |
wreq = (HttpWebRequest)WebRequest.Create(url); |
|
203 |
wreq.AllowWriteStreamBuffering = true; |
|
204 |
|
|
205 |
wresp = (HttpWebResponse)wreq.GetResponse(); |
|
206 |
|
|
207 |
if ((mystream = wresp.GetResponseStream()) != null) |
|
208 |
bmp = new Bitmap(mystream); |
|
209 |
} |
|
210 |
finally |
|
211 |
{ |
|
212 |
if (mystream != null) |
|
213 |
{ |
|
214 |
mystream.Close(); |
|
215 |
mystream.Dispose(); |
|
216 |
} |
|
217 |
|
|
218 |
if (wresp != null) |
|
219 |
{ |
|
220 |
wresp.Close(); |
|
221 |
wresp.Dispose(); |
|
222 |
} |
|
223 |
} |
|
224 |
return (bmp); |
|
225 |
} |
|
226 |
|
|
227 |
#region IDisposable Support |
|
228 |
|
|
229 |
private bool disposedValue = false; |
|
230 |
|
|
231 |
// 중복 호출을 검색하려면 |
|
232 |
protected virtual void Dispose(bool disposing) |
|
233 |
{ |
|
234 |
if (!disposedValue) |
|
235 |
{ |
|
236 |
if (disposing) |
|
237 |
{ |
|
238 |
// TODO: 관리되는 상태(관리되는 개체)를 삭제합니다. |
|
239 |
} |
|
240 |
// TODO: 관리되지 않는 리소스(관리되지 않는 개체)를 해제하고 아래의 종료자를 재정의합니다. |
|
241 |
// TODO: 큰 필드를 null로 설정합니다. |
|
242 |
disposedValue = true; |
|
243 |
} |
|
244 |
} |
|
245 |
|
|
246 |
// TODO: 위의 Dispose(bool disposing)에 관리되지 않는 리소스를 해제하는 코드가 포함되어 있는 경우에만 종료자를 재정의합니다. |
|
247 |
// |
|
248 |
//~DisposeClass() |
|
249 |
// { |
|
250 |
// // 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요. |
|
251 |
// Dispose(false); // |
|
252 |
// } |
|
253 |
|
|
254 |
// 삭제 가능한 패턴을 올바르게 구현하기 위해 추가된 코드입니다. |
|
255 |
public void Dispose() |
|
256 |
{ |
|
257 |
// 이 코드를 변경하지 마세요. 위의 Dispose(bool disposing)에 정리 코드를 입력하세요. |
|
258 |
Dispose(true); |
|
259 |
|
|
260 |
|
|
261 |
if (OriginalImageData != null) |
|
262 |
{ |
|
263 |
OriginalImageData.Dispose(); |
|
264 |
} |
|
265 |
|
|
266 |
if (TargatImageData != null) |
|
267 |
{ |
|
268 |
TargatImageData.Dispose(); |
|
269 |
} |
|
270 |
|
|
271 |
// TODO: 위의 종료자가 재정의된 경우 다음 코드 줄의 주석 처리를 제거합니다. |
|
272 |
// GC.SuppressFinalize(this); |
|
273 |
GC.Collect(); |
|
274 |
GC.SuppressFinalize(this); |
|
275 |
GC.Collect(); |
|
276 |
} |
|
277 |
#endregion |
|
278 |
} |
|
279 |
} |
ImageComparer/Markus.ImageComparer/Markus.Image.csproj | ||
---|---|---|
64 | 64 |
</ItemGroup> |
65 | 65 |
<ItemGroup> |
66 | 66 |
<Compile Include="CompareStatus.cs" /> |
67 |
<Compile Include="ImageComparer.cs" />
|
|
68 |
<Compile Include="ImageComparerBase.cs" />
|
|
67 |
<Compile Include="ImageCompare.cs" /> |
|
68 |
<Compile Include="ImageCompareBase.cs" /> |
|
69 | 69 |
<Compile Include="OpenCVExtensions.cs" /> |
70 | 70 |
<Compile Include="PixelFormatsConverter.cs" /> |
71 | 71 |
<Compile Include="Properties\AssemblyInfo.cs" /> |
내보내기 Unified diff