개정판 a860bafc
issue #0000 compare 기능 수정
Change-Id: Idbac07b170a08bc67f50d315912f314da1ef8938
Markus.ImageComparer/ImageCompareBase.cs | ||
---|---|---|
1 | 1 |
using OpenCvSharp; |
2 | 2 |
using System; |
3 |
using System.Collections.Concurrent; |
|
3 | 4 |
using System.Collections.Generic; |
4 | 5 |
using System.Drawing; |
5 | 6 |
using System.Drawing.Imaging; |
... | ... | |
10 | 11 |
using System.Text; |
11 | 12 |
using System.Threading; |
12 | 13 |
using System.Threading.Tasks; |
14 |
using System.Windows.Media.Imaging; |
|
13 | 15 |
using Point = System.Drawing.Point; |
14 | 16 |
using Size = System.Drawing.Size; |
15 | 17 |
|
... | ... | |
21 | 23 |
Mat TargatImageData = null; |
22 | 24 |
|
23 | 25 |
double contoursLongCount = 0; |
24 |
double ComparisonCount = 0;
|
|
26 |
int ComparisonCount = 0;
|
|
25 | 27 |
public CompareProgress Progress = new CompareProgress(); |
26 | 28 |
|
27 | 29 |
private void SetStatus(string message,double percentage,CompareStatus status) |
28 | 30 |
{ |
29 |
System.Diagnostics.Debug.WriteLine(percentage); |
|
31 |
//System.Diagnostics.Debug.WriteLine(percentage);
|
|
30 | 32 |
|
31 | 33 |
Progress.Message = message; |
32 | 34 |
Progress.Percentage = percentage; |
... | ... | |
63 | 65 |
Cv2.CvtColor(OriginalImageData, OriginalImageData, ColorConversionCodes.BGR2GRAY); |
64 | 66 |
Cv2.CvtColor(TargatImageData, TargatImageData, ColorConversionCodes.BGR2GRAY); |
65 | 67 |
|
66 |
Mat outputData = new Mat(TargatImageData.Size(), MatType.CV_8UC1);
|
|
68 |
Mat outputData = new Mat(new OpenCvSharp.Size(resultSize.Width, resultSize.Height), MatType.CV_8UC1);
|
|
67 | 69 |
|
68 | 70 |
Cv2.Absdiff(OriginalImageData, TargatImageData, outputData); |
69 | 71 |
|
... | ... | |
81 | 83 |
return result; |
82 | 84 |
} |
83 | 85 |
|
84 |
protected System.Drawing.Bitmap ChangeBitmapFormatAndSize(System.Drawing.Bitmap bitmap, Size newSize, PixelFormat pixelFormat) |
|
85 |
{ |
|
86 |
Bitmap result = bitmap; |
|
87 |
|
|
88 |
if (pixelFormat != bitmap.PixelFormat) |
|
89 |
{ |
|
90 |
Point originPoint = new Point(0, 0); |
|
91 |
Rectangle rect = new Rectangle(originPoint, bitmap.Size); |
|
92 |
result = bitmap.Clone(rect, pixelFormat); |
|
93 |
} |
|
94 |
|
|
95 |
if (bitmap.Size != newSize) |
|
96 |
{ |
|
97 |
result = new Bitmap(newSize.Width, newSize.Height); |
|
98 |
|
|
99 |
using (Graphics g = Graphics.FromImage(result)) |
|
100 |
{ |
|
101 |
g.DrawImage(bitmap, 0, 0, newSize.Width, newSize.Height); |
|
102 |
g.Dispose(); |
|
103 |
} |
|
104 |
} |
|
105 |
|
|
106 |
return result; |
|
107 |
} |
|
108 | 86 |
|
109 | 87 |
/// <summary> |
110 | 88 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
... | ... | |
131 | 109 |
|
132 | 110 |
OpenCvSharp.Point testpoint = new OpenCvSharp.Point(); |
133 | 111 |
|
134 |
Cv2.Threshold(data, data, 0, 30,ThresholdTypes.BinaryInv);
|
|
112 |
Cv2.Threshold(data, data, 50, 255,ThresholdTypes.BinaryInv);
|
|
135 | 113 |
|
136 |
Cv2.FindContours(data, out contours, out hierarchy,RetrievalModes.List,ContourApproximationModes.ApproxNone, testpoint);
|
|
114 |
Cv2.FindContours(data, out contours, out hierarchy,RetrievalModes.List,ContourApproximationModes.ApproxSimple, testpoint);
|
|
137 | 115 |
|
138 | 116 |
SetStatus("Comparison", 0, CompareStatus.Comparison); |
139 | 117 |
contoursLongCount = contours.Sum(x => x.Count()); |
... | ... | |
149 | 127 |
|
150 | 128 |
return results; |
151 | 129 |
} |
130 |
|
|
152 | 131 |
private List<System.Windows.Rect> GetRectList(OpenCvSharp.Point[] points, Size block) |
153 | 132 |
{ |
154 | 133 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
155 |
HashSet<System.Windows.Rect> rectSet = new HashSet<System.Windows.Rect>();
|
|
134 |
ConcurrentBag<System.Windows.Rect> rectBag = new ConcurrentBag<System.Windows.Rect>();
|
|
156 | 135 |
|
157 | 136 |
Parallel.For(0, points.Length, i => |
158 | 137 |
{ |
... | ... | |
164 | 143 |
Height = block.Height |
165 | 144 |
}; |
166 | 145 |
|
167 |
lock (rectSet)
|
|
146 |
if (!rectBag.Any(r => r.IntersectsWith(rect)))
|
|
168 | 147 |
{ |
169 |
if (!rectSet.Any(r => r.IntersectsWith(rect))) |
|
170 |
{ |
|
171 |
rectSet.Add(rect); |
|
172 |
result.Add(rect); |
|
173 |
} |
|
148 |
rectBag.Add(rect); |
|
149 |
result.Add(rect); |
|
174 | 150 |
} |
175 | 151 |
|
176 |
int comparisonCount = 0; |
|
177 |
Interlocked.Increment(ref comparisonCount); |
|
178 |
|
|
179 |
ComparisonCount = comparisonCount; |
|
152 |
Interlocked.Increment(ref ComparisonCount); |
|
180 | 153 |
|
181 |
SetStatus("Comparison", ComparisonCount / (double)contoursLongCount * 100, CompareStatus.Comparison); |
|
154 |
SetStatus("Comparison", (double)ComparisonCount / (double)contoursLongCount * 100, CompareStatus.Comparison);
|
|
182 | 155 |
}); |
183 | 156 |
|
184 | 157 |
return result; |
185 | 158 |
} |
186 | 159 |
|
160 |
protected System.Drawing.Bitmap ChangeBitmapFormatAndSize(System.Drawing.Bitmap bitmap, Size newSize, PixelFormat pixelFormat) |
|
161 |
{ |
|
162 |
Bitmap result = bitmap; |
|
163 |
|
|
164 |
if (pixelFormat != bitmap.PixelFormat) |
|
165 |
{ |
|
166 |
Point originPoint = new Point(0, 0); |
|
167 |
Rectangle rect = new Rectangle(originPoint, bitmap.Size); |
|
168 |
result = bitmap.Clone(rect, pixelFormat); |
|
169 |
} |
|
170 |
|
|
171 |
if (bitmap.Size != newSize) |
|
172 |
{ |
|
173 |
result = new Bitmap(newSize.Width, newSize.Height); |
|
174 |
|
|
175 |
using (Graphics g = Graphics.FromImage(result)) |
|
176 |
{ |
|
177 |
g.DrawImage(bitmap, 0, 0, newSize.Width, newSize.Height); |
|
178 |
g.Dispose(); |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
return result; |
|
183 |
} |
|
184 |
|
|
185 |
|
|
187 | 186 |
private List<System.Windows.Rect> GetRectList2(OpenCvSharp.Point[] points,Size block) |
188 | 187 |
{ |
189 | 188 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
내보내기 Unified diff