개정판 9f61d7b6
ImageCompare CompareStatus 추가
- 진행 상테를 Progress로 저장
Change-Id: I6d326cb72fb5a695a93cb0d1c2f7dd2f305f4ee2
ImageComparer/ComparerTestWPF/ComparerTestWPF.csproj | ||
---|---|---|
13 | 13 |
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> |
14 | 14 |
<WarningLevel>4</WarningLevel> |
15 | 15 |
<Deterministic>true</Deterministic> |
16 |
<PublishUrl>publish\</PublishUrl> |
|
17 |
<Install>true</Install> |
|
18 |
<InstallFrom>Disk</InstallFrom> |
|
19 |
<UpdateEnabled>false</UpdateEnabled> |
|
20 |
<UpdateMode>Foreground</UpdateMode> |
|
21 |
<UpdateInterval>7</UpdateInterval> |
|
22 |
<UpdateIntervalUnits>Days</UpdateIntervalUnits> |
|
23 |
<UpdatePeriodically>false</UpdatePeriodically> |
|
24 |
<UpdateRequired>false</UpdateRequired> |
|
25 |
<MapFileExtensions>true</MapFileExtensions> |
|
26 |
<ApplicationRevision>0</ApplicationRevision> |
|
27 |
<ApplicationVersion>1.0.0.%2a</ApplicationVersion> |
|
28 |
<IsWebBootstrapper>false</IsWebBootstrapper> |
|
29 |
<UseApplicationTrust>false</UseApplicationTrust> |
|
30 |
<BootstrapperEnabled>true</BootstrapperEnabled> |
|
16 | 31 |
</PropertyGroup> |
17 | 32 |
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
18 | 33 |
<PlatformTarget>x64</PlatformTarget> |
... | ... | |
111 | 126 |
<Name>Markus.Image</Name> |
112 | 127 |
</ProjectReference> |
113 | 128 |
</ItemGroup> |
129 |
<ItemGroup> |
|
130 |
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1"> |
|
131 |
<Visible>False</Visible> |
|
132 |
<ProductName>.NET Framework 3.5 SP1</ProductName> |
|
133 |
<Install>false</Install> |
|
134 |
</BootstrapperPackage> |
|
135 |
</ItemGroup> |
|
114 | 136 |
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> |
115 | 137 |
</Project> |
ImageComparer/ComparerTestWPF/MainWindow.xaml.cs | ||
---|---|---|
59 | 59 |
BitmapSource bitmapSource = null; |
60 | 60 |
BitmapSource TargetSource = null; |
61 | 61 |
|
62 |
using (Markus.Image.ImageComparer imageComparer = new Markus.Image.ImageComparer())
|
|
62 |
using (Markus.Image.ImageCompare imageCompare = new Markus.Image.ImageCompare())
|
|
63 | 63 |
{ |
64 |
|
|
65 | 64 |
var original = new System.Drawing.Bitmap(TxtOriginalImage.Text); |
66 | 65 |
var target = new System.Drawing.Bitmap(TxtTargetImage.Text); |
67 | 66 |
|
68 |
var result = imageComparer.CompareDrawRects(original, target, new System.Drawing.Size(20, 20));
|
|
67 |
var result = imageCompare.CompareDrawRects(original, target, new System.Drawing.Size(20, 20)); |
|
69 | 68 |
|
70 |
bitmapSource = imageComparer.CreateWriteableBitmapFromBitmap(result);
|
|
69 |
bitmapSource = imageCompare.CreateWriteableBitmapFromBitmap(result); |
|
71 | 70 |
|
72 | 71 |
//var result2 = imageComparer.CompareDrawRects(target, original, new System.Drawing.Size(20, 20)); |
73 | 72 |
//TargetSource = imageComparer.CreateBitmapSourceFromBitmap(result2); |
... | ... | |
94 | 93 |
BitmapSource bitmapSource = null; |
95 | 94 |
BitmapSource TargetSource = null; |
96 | 95 |
|
97 |
using (Markus.Image.ImageComparer imageComparer = new Markus.Image.ImageComparer())
|
|
96 |
using (Markus.Image.ImageCompare imageComparer = new Markus.Image.ImageCompare())
|
|
98 | 97 |
{ |
99 | 98 |
|
100 | 99 |
var original = new System.Drawing.Bitmap(TxtOriginalImage.Text); |
... | ... | |
131 | 130 |
private async void btTaskUri_Click(object sender, RoutedEventArgs e) |
132 | 131 |
{ |
133 | 132 |
|
134 |
using (Markus.Image.ImageComparer imageComparer = new Markus.Image.ImageComparer())
|
|
133 |
using (Markus.Image.ImageCompare imageComparer = new Markus.Image.ImageCompare())
|
|
135 | 134 |
{ |
136 | 135 |
var result = await imageComparer.CompareReturnRectsAsync("http://192.168.0.67:5977/TileSource/000000_Tile/110001/11000102/1.png" |
137 | 136 |
, "http://192.168.0.67:5977/TileSource/000000_Tile/110001/11000101/1.png", new System.Drawing.Size(20, 20)); |
ImageComparer/Markus.ImageComparer/CompareStatus.cs | ||
---|---|---|
1 |
using System; |
|
2 |
using System.Collections.Generic; |
|
3 |
using System.ComponentModel; |
|
4 |
using System.Linq; |
|
5 |
using System.Runtime.CompilerServices; |
|
6 |
using System.Text; |
|
7 |
using System.Threading.Tasks; |
|
8 |
|
|
9 |
namespace Markus.Image |
|
10 |
{ |
|
11 |
public class CompareProgress : INotifyPropertyChanged |
|
12 |
{ |
|
13 |
private string message; |
|
14 |
private double percentage; |
|
15 |
private CompareStatus status; |
|
16 |
|
|
17 |
public string Message { get => message; |
|
18 |
set |
|
19 |
{ |
|
20 |
if (value != this.message) |
|
21 |
{ |
|
22 |
this.message = value; |
|
23 |
NotifyPropertyChanged(); |
|
24 |
} |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
public double Percentage { get => percentage; |
|
29 |
set |
|
30 |
{ |
|
31 |
if (value != this.percentage) |
|
32 |
{ |
|
33 |
this.percentage = value; |
|
34 |
NotifyPropertyChanged(); |
|
35 |
} |
|
36 |
} |
|
37 |
} |
|
38 |
|
|
39 |
public CompareStatus Status { get => status; |
|
40 |
set |
|
41 |
{ |
|
42 |
if (value != this.status) |
|
43 |
{ |
|
44 |
this.status = value; |
|
45 |
NotifyPropertyChanged(); |
|
46 |
} |
|
47 |
} |
|
48 |
} |
|
49 |
|
|
50 |
public event PropertyChangedEventHandler PropertyChanged; |
|
51 |
|
|
52 |
// This method is called by the Set accessor of each property. |
|
53 |
// The CallerMemberName attribute that is applied to the optional propertyName |
|
54 |
// parameter causes the property name of the caller to be substituted as an argument. |
|
55 |
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") |
|
56 |
{ |
|
57 |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
|
58 |
} |
|
59 |
} |
|
60 |
|
|
61 |
public enum CompareStatus |
|
62 |
{ |
|
63 |
Loading = 0, Comparison = 1, Detection = 2 |
|
64 |
} |
|
65 |
} |
ImageComparer/Markus.ImageComparer/ImageComparer.cs | ||
---|---|---|
8 | 8 |
|
9 | 9 |
namespace Markus.Image |
10 | 10 |
{ |
11 |
public class ImageComparer : ImageComparerBase
|
|
11 |
public class ImageCompare : ImageCompareBase
|
|
12 | 12 |
{ |
13 | 13 |
/// <summary> |
14 | 14 |
/// 이미지를 비교 후 원본 이미지에 Rect를 그린다. |
... | ... | |
37 | 37 |
return Originalbitmap; |
38 | 38 |
} |
39 | 39 |
|
40 |
// JoinRects: will return a rectangle composed of r1 and r2. |
|
41 |
private System.Windows.Rect JoinRects(System.Windows.Rect r1, System.Windows.Rect r2) |
|
42 |
{ |
|
43 |
return new System.Windows.Rect(Math.Min(r1.X, r2.X), |
|
44 |
Math.Min(r1.Y, r2.Y), |
|
45 |
Math.Max(r1.Y + r1.Width, r2.Y + r2.Width), |
|
46 |
Math.Max(r1.X + r1.Height, r2.X + r2.Height)); |
|
47 |
} |
|
48 |
|
|
49 |
private System.Windows.Rect RectSizeUp(System.Windows.Rect rect,int UpSize) |
|
50 |
{ |
|
51 |
return new System.Windows.Rect(rect.X - UpSize, rect.Y - UpSize, rect.Width + UpSize, rect.Height + UpSize); |
|
52 |
} |
|
53 |
|
|
54 | 40 |
public System.Windows.Media.Imaging.BitmapSource CompareDrawRects(System.Windows.Media.Imaging.BitmapSource Originalbitmap, System.Windows.Media.Imaging.BitmapSource TargatBitmap, Size ResultRectSize) |
55 | 41 |
{ |
56 | 42 |
var _Originalbitmap = CreateBitmapFromSource(Originalbitmap); |
... | ... | |
83 | 69 |
|
84 | 70 |
try |
85 | 71 |
{ |
86 |
|
|
87 | 72 |
Originalbitmap = LoadPicture(OriginalbitmapUri); |
88 | 73 |
TargatBitmap = LoadPicture(TargatBitmapUri); |
89 | 74 |
|
... | ... | |
140 | 125 |
return result; |
141 | 126 |
} |
142 | 127 |
|
143 |
private List<System.Windows.Rect> JoinRectList(List<System.Windows.Rect> rects) |
|
144 |
{ |
|
145 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
146 |
|
|
147 |
if (rects.Count() > 0) |
|
148 |
{ |
|
149 |
System.Windows.Rect rect = rects[0]; |
|
150 |
System.Windows.Rect joinRect = rects[0]; |
|
151 |
|
|
152 |
while (rects.Count() > 0) |
|
153 |
{ |
|
154 |
var joinItems = rects.Where(x => RectSizeUp(joinRect, 1).Contains(x)).ToList(); |
|
155 |
|
|
156 |
if (joinItems.Count() == 0) |
|
157 |
{ |
|
158 |
result.Add(joinRect); |
|
159 |
} |
|
160 |
else |
|
161 |
{ |
|
162 |
for (int i = 0; i < joinItems.Count(); i++) |
|
163 |
{ |
|
164 |
rect = JoinRects(rect, joinItems[i]); |
|
165 |
rects.Remove(joinItems[i]); |
|
166 |
} |
|
167 |
|
|
168 |
if (rects.Count() > 0) |
|
169 |
{ |
|
170 |
rects.RemoveAt(0); |
|
171 |
} |
|
172 |
|
|
173 |
result.Add(joinRect); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
return result; |
|
179 |
} |
|
180 |
|
|
181 |
private List<System.Windows.Rect> Merge(List<System.Windows.Rect> r,int rectHeight) |
|
182 |
{ |
|
183 |
// Computing the bound |
|
184 |
System.Windows.Rect bound = new System.Windows.Rect(r[0].X, r[0].Y, r[0].Width, r[0].Height); |
|
185 |
for (int i = 1; i < r.Count(); ++i) |
|
186 |
{ |
|
187 |
bound.X = Math.Min(bound.X, r[i].X); |
|
188 |
bound.Y = Math.Min(bound.Y, r[i].Y); |
|
189 |
bound.Width = Math.Max(bound.Right, r[i].Right) - bound.X; |
|
190 |
bound.Height = Math.Max(bound.Bottom, r[i].Bottom) - bound.Y; |
|
191 |
} |
|
192 |
|
|
193 |
// Create number of rectangle will be created by vertical strip expansion |
|
194 |
System.Windows.Rect[] m = new System.Windows.Rect[(int)bound.Height / (int)rectHeight]; |
|
195 |
for (int i = 0; i < m.Length; ++i) |
|
196 |
{ |
|
197 |
m[i] = new System.Windows.Rect(bound.X, bound.Y + i * rectHeight, bound.Y, rectHeight); |
|
198 |
} |
|
199 |
|
|
200 |
for (int i = 0; i < r.Count(); ++i) |
|
201 |
{ |
|
202 |
int index = ((int)r[i].Y - (int)bound.Y) / rectHeight; |
|
203 |
|
|
204 |
if (m[index].Width <= 0) |
|
205 |
{ |
|
206 |
m[index].Width = r[i].Width; |
|
207 |
m[index].X = r[i].X; |
|
208 |
} |
|
209 |
else |
|
210 |
{ |
|
211 |
int right = (int)m[index].Right; |
|
212 |
m[index].X = Math.Min(m[index].X, r[i].X); |
|
213 |
m[index].Width = Math.Max(right, r[i].Right) - m[index].X; |
|
214 |
} |
|
215 |
} |
|
216 |
|
|
217 |
// Merge horozontally |
|
218 |
for (int i = m.Length - 1; i > 0; --i) |
|
219 |
{ |
|
220 |
// can only merge when two rect has the same X and Width |
|
221 |
if ((m[i].X == m[i - 1].X) && (m[i].Width == m[i - 1].Width)) |
|
222 |
{ |
|
223 |
m[i - 1].Height += m[i].Height; // expanse the rectangle |
|
224 |
m[i].Width = 0; // remove one rectangle |
|
225 |
} |
|
226 |
} |
|
227 |
|
|
228 |
// Remove all the empty rectangle |
|
229 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
230 |
for (int i = m.Length - 1; i >= 0; --i) |
|
231 |
{ |
|
232 |
if (m[i].Width > 0) |
|
233 |
result.Add(m[i]); |
|
234 |
} |
|
235 |
|
|
236 |
return result; |
|
237 |
} |
|
238 | 128 |
/// <summary> |
239 | 129 |
/// 이미지를 비교 후 원본 이미지에 Rect를 그린다. |
240 | 130 |
/// 메모리 문제 발생 |
ImageComparer/Markus.ImageComparer/ImageComparerBase.cs | ||
---|---|---|
15 | 15 |
|
16 | 16 |
namespace Markus.Image |
17 | 17 |
{ |
18 |
public class ImageComparerBase : IDisposable
|
|
18 |
public class ImageCompareBase : IDisposable |
|
19 | 19 |
{ |
20 | 20 |
Mat OriginalImageData = null; |
21 | 21 |
Mat TargatImageData = null; |
22 | 22 |
|
23 |
/// <summary> |
|
24 |
/// 이미지 비교에 사용되는 점수 |
|
25 |
/// </summary> |
|
26 |
double gMatchScore = 0.9; |
|
23 |
double contoursLongCount = 0; |
|
24 |
double ComparisonCount = 0; |
|
25 |
public CompareProgress Progress = new CompareProgress(); |
|
27 | 26 |
|
28 |
/// <summary> |
|
29 |
/// 이미지의 크기와 포멧을 변경한다. |
|
30 |
/// </summary> |
|
31 |
/// <param name="bitmap"></param> |
|
32 |
/// <param name="newSize"></param> |
|
33 |
/// <param name="pixelFormat"></param> |
|
34 |
/// <returns></returns> |
|
35 |
protected System.Drawing.Bitmap ChangeBitmapFormatAndSize(System.Drawing.Bitmap bitmap, Size newSize, PixelFormat pixelFormat) |
|
27 |
private void SetStatus(string message,double percentage,CompareStatus status) |
|
36 | 28 |
{ |
37 |
Bitmap result = bitmap; |
|
38 |
|
|
39 |
//if (pixelFormat != result.PixelFormat) |
|
40 |
//{ |
|
41 |
// Point originPoint = new Point(0, 0); |
|
42 |
// Rectangle rect = new Rectangle(originPoint, bitmap.Size); |
|
43 |
// result = bitmap.Clone(rect, pixelFormat); |
|
44 |
//} |
|
45 |
|
|
46 |
if (result.Size != newSize || pixelFormat != result.PixelFormat) |
|
47 |
{ |
|
48 |
|
|
49 |
using (Graphics g = Graphics.FromImage(bitmap)) |
|
50 |
{ |
|
51 |
ColorMatrix cm = new ColorMatrix(new float[][]{ new |
|
52 |
float[]{0.3f,0.3f,0.3f,0,0}, |
|
53 |
new float[]{0.59f,0.59f,0.59f,0,0}, |
|
54 |
new float[]{0.11f,0.11f,0.11f,0,0}, |
|
55 |
new float[]{0,0,0,1,0,0}, |
|
56 |
new float[]{0,0,0,0,1,0}, |
|
57 |
new float[]{0,0,0,0,0,1}}); |
|
58 |
|
|
59 |
ImageAttributes ia = new ImageAttributes(); |
|
60 |
ia.SetColorMatrix(cm); |
|
61 |
|
|
62 |
g.DrawImage(result, new Rectangle(0, 0, newSize.Width, newSize.Height), |
|
63 |
0, 0, newSize.Width, newSize.Height, GraphicsUnit.Pixel, ia); |
|
64 |
g.Dispose(); |
|
65 |
} |
|
66 |
} |
|
29 |
System.Diagnostics.Debug.WriteLine(percentage); |
|
67 | 30 |
|
68 |
return result; |
|
31 |
Progress.Message = message; |
|
32 |
Progress.Percentage = percentage; |
|
33 |
Progress.Status = status; |
|
69 | 34 |
} |
70 | 35 |
|
71 | 36 |
/// <summary> |
... | ... | |
80 | 45 |
|
81 | 46 |
try |
82 | 47 |
{ |
83 |
Mat mask = new Mat();
|
|
48 |
SetStatus("Image Load", 0, CompareStatus.Loading);
|
|
84 | 49 |
|
85 | 50 |
// 원본이미지의 크키와 Format24bppRgb로 타켓 이미지를 변경 |
86 | 51 |
// 크기가 틀린 경우 비교시 바이트배열 오류 발생 |
87 |
// 이미지 포멧은 24bit이하로 emgu CV가 작동 |
|
88 |
//Originalbitmap = ChangeBitmapFormatAndSize(Originalbitmap, Originalbitmap.Size, PixelFormat.Format24bppRgb); |
|
89 |
//TargatBitmap = ChangeBitmapFormatAndSize(TargatBitmap, Originalbitmap.Size, PixelFormat.Format24bppRgb); |
|
90 |
|
|
91 | 52 |
OriginalImageData = OpenCvSharp.Extensions.BitmapConverter.ToMat(Originalbitmap); |
92 | 53 |
TargatImageData = OpenCvSharp.Extensions.BitmapConverter.ToMat(TargatBitmap); |
93 | 54 |
|
... | ... | |
95 | 56 |
{ |
96 | 57 |
Cv2.Resize(TargatImageData, TargatImageData, OriginalImageData.Size()); |
97 | 58 |
} |
98 |
|
|
99 |
//Cv2.ImWrite("d:\\testsave\\original.png", OriginalImageData); |
|
100 |
//Cv2.ImWrite("d:\\testsave\\Targat.png", TargatImageData); |
|
101 |
|
|
102 |
|
|
59 |
|
|
103 | 60 |
Cv2.CvtColor(OriginalImageData, OriginalImageData, ColorConversion.BgrToGray); |
104 | 61 |
Cv2.CvtColor(TargatImageData, TargatImageData, ColorConversion.BgrToGray); |
105 |
//Cv2.CvtColor(TargatImageData, TargatImageData, ColorConversion.RgbToGray); |
|
106 |
//Cv2.ImWrite("d:\\testsave\\original_Mask.png", OriginalImageData); |
|
107 |
|
|
108 |
|
|
109 |
//Cv2.Threshold(OriginalImageData, OriginalImageData, 127, 255,ThresholdType.Binary); |
|
110 |
//Cv2.Threshold(TargatImageData, TargatImageData, 127, 255, ThresholdType.Otsu); |
|
111 | 62 |
|
112 | 63 |
Mat outputData = new Mat(TargatImageData.Size(), MatType.CV_8UC1); |
113 | 64 |
|
114 | 65 |
Cv2.Absdiff(OriginalImageData, TargatImageData, outputData); |
115 | 66 |
|
116 |
//Cv2.ImWrite("d:\\testsave\\outputData.png", outputData); |
|
117 |
|
|
118 |
//Computes absolute different between this image and the other image |
|
119 |
// 원본이미지와 타겟이미지를 처리 |
|
67 |
Mat outputtest = new Mat(TargatImageData.Size(), MatType.CV_8UC1); |
|
68 |
Cv2.MatchTemplate(OriginalImageData, TargatImageData, outputtest, MatchTemplateMethod.CCoeffNormed); |
|
69 |
Cv2.ImWrite(@"D:\testsave\test1.png", outputtest); |
|
120 | 70 |
|
121 | 71 |
// 틀린부분을 반환 |
122 | 72 |
Cv2.BitwiseNot(outputData, result); |
123 |
|
|
124 |
|
|
125 |
// Cv2.ImWrite("d:\\testsave\\result.png", outputData); |
|
126 |
|
|
127 | 73 |
} |
128 | 74 |
catch (Exception ex) |
129 | 75 |
{ |
... | ... | |
135 | 81 |
|
136 | 82 |
return result; |
137 | 83 |
} |
138 |
/// <summary> |
|
139 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
|
140 |
/// </summary> |
|
141 |
/// <param name="data"></param> |
|
142 |
/// <param name="block"></param> |
|
143 |
/// <returns></returns> |
|
144 |
protected List<System.Windows.Rect> GetMatchPixels_test(OpenCvSharp.CPlusPlus.Mat data, Size block) |
|
145 |
{ |
|
146 |
int width = data.Cols; |
|
147 |
int height = data.Rows; |
|
148 |
|
|
149 |
List<System.Windows.Rect> results = new List<System.Windows.Rect>(); |
|
150 |
|
|
151 |
var heightRange = from h in Enumerable.Range(1, height) |
|
152 |
where h % block.Height == 0 |
|
153 |
select h; |
|
154 |
|
|
155 |
var widthRange = from w in Enumerable.Range(1, width) |
|
156 |
where w % block.Width == 0 |
|
157 |
select w; |
|
158 |
|
|
159 |
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); |
|
160 |
stopwatch.Start(); |
|
161 |
|
|
162 |
Mat outputMat = new Mat(); |
|
163 |
|
|
164 |
HierarchyIndex[] hierarchy; |
|
165 |
//OpenCvSharp.CPlusPlus.Point[][] contours; |
|
166 |
OpenCvSharp.CPlusPlus.Point[][] contours; |
|
167 |
|
|
168 |
Cv2.ImWrite("d:\\testsave\\CvtColor_before.png", data); |
|
169 |
Cv2.CvtColor(data, outputMat, ColorConversion.BgrToGray); |
|
170 |
|
|
171 |
Cv2.ImWrite("d:\\testsave\\CvtColor_after.png", outputMat); |
|
172 |
|
|
173 |
OpenCvSharp.CPlusPlus.Point testpoint = new OpenCvSharp.CPlusPlus.Point(); |
|
174 |
|
|
175 |
Cv2.Threshold(outputMat, outputMat, 127, 255, ThresholdType.Binary); |
|
176 |
|
|
177 |
Cv2.FindContours(outputMat, out contours, out hierarchy, ContourRetrieval.Tree, ContourChain.ApproxSimple, testpoint); |
|
178 |
|
|
179 |
for (int i = 0; i < contours.Count(); i++) |
|
180 |
{ |
|
181 |
Cv2.DrawContours(data, contours, i, Scalar.Red, 1, LineType.AntiAlias); |
|
182 |
} |
|
183 |
Cv2.ImWrite("d:\\testsave\\draw.png", data); |
|
184 |
|
|
185 |
|
|
186 |
//foreach (var y in heightRange) |
|
187 |
//{ |
|
188 |
// foreach (var x in widthRange) |
|
189 |
// { |
|
190 |
// var rect = DataMatchScore(data, x, y, width, height, block); |
|
191 |
|
|
192 |
// if (rect != null) |
|
193 |
// { |
|
194 |
// results.Add(rect.Value); |
|
195 |
// } |
|
196 |
// } |
|
197 |
//} |
|
198 |
|
|
199 |
//var items = data.ToDataFloat(); |
|
200 |
|
|
201 |
//var handle = GCHandle.Alloc(data); |
|
202 |
|
|
203 |
//foreach (var y in heightRange) |
|
204 |
//{ |
|
205 |
// foreach (var x in widthRange) |
|
206 |
// { |
|
207 |
// var rect = DataMatchScore(data, x, y, width, height, block); |
|
208 |
|
|
209 |
// if (rect != null) |
|
210 |
// { |
|
211 |
// results.Add(rect.Value); |
|
212 |
// } |
|
213 |
// } |
|
214 |
//} |
|
215 |
//handle.Free(); |
|
216 |
|
|
217 |
//Parallel.ForEach(heightRange, (y) => |
|
218 |
//{ |
|
219 |
// Parallel.ForEach(widthRange, (x) => |
|
220 |
// { |
|
221 |
// var rect = DataMatchScore(data, x, y, width, height, block); |
|
222 |
|
|
223 |
// if (rect != null) |
|
224 |
// { |
|
225 |
// results.Add(rect.Value); |
|
226 |
// } |
|
227 |
// }); |
|
228 |
//}); |
|
229 |
|
|
230 |
|
|
231 |
//Parallel.ForEach(heightRange, (y) => |
|
232 |
//{ |
|
233 |
// Parallel.ForEach(widthRange, (x) => |
|
234 |
// { |
|
235 |
// var rect = DataMatchScore(data, x, y, width, height, block); |
|
236 |
|
|
237 |
// if (rect != null) |
|
238 |
// { |
|
239 |
// results.Add(rect.Value); |
|
240 |
// } |
|
241 |
// }); |
|
242 |
//}); |
|
243 |
|
|
244 |
System.Diagnostics.Debug.WriteLine("1 - " + new TimeSpan(stopwatch.ElapsedTicks)); |
|
245 |
return results; |
|
246 |
} |
|
247 |
|
|
248 | 84 |
|
249 | 85 |
/// <summary> |
250 | 86 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
... | ... | |
254 | 90 |
/// <returns></returns> |
255 | 91 |
protected List<System.Windows.Rect> GetMatchPixels(OpenCvSharp.CPlusPlus.Mat data, Size block) |
256 | 92 |
{ |
93 |
SetStatus("Detection", 0, CompareStatus.Detection); |
|
94 |
|
|
257 | 95 |
int width = data.Cols; |
258 | 96 |
int height = data.Rows; |
259 | 97 |
|
260 | 98 |
List<System.Windows.Rect> results = new List<System.Windows.Rect>(); |
261 | 99 |
|
262 |
//var heightRange = from h in Enumerable.Range(1, height) |
|
263 |
// where h % block.Height == 0 |
|
264 |
// select h; |
|
265 |
|
|
266 |
//var widthRange = from w in Enumerable.Range(1, width) |
|
267 |
// where w % block.Width == 0 |
|
268 |
// select w; |
|
269 |
|
|
270 | 100 |
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch(); |
271 | 101 |
stopwatch.Start(); |
272 | 102 |
|
273 | 103 |
Mat outputMat = new Mat(data.Size(), MatType.CV_8UC1); |
274 | 104 |
|
275 | 105 |
HierarchyIndex[] hierarchy; |
276 |
//OpenCvSharp.CPlusPlus.Point[][] contours; |
|
277 | 106 |
OpenCvSharp.CPlusPlus.Point[][] contours; |
278 | 107 |
|
279 |
//Cv2.CvtColor(data, outputMat,ColorConversion.RgbToGray); |
|
280 |
|
|
281 | 108 |
OpenCvSharp.CPlusPlus.Point testpoint = new OpenCvSharp.CPlusPlus.Point(); |
282 |
|
|
283 |
Cv2.Threshold(data, data, 200, 255,ThresholdType.Otsu);
|
|
109 |
|
|
110 |
Cv2.Threshold(data, data, 90, 255,ThresholdType.BinaryInv);
|
|
284 | 111 |
|
285 | 112 |
Cv2.FindContours(data, out contours, out hierarchy, ContourRetrieval.List, ContourChain.ApproxNone, testpoint); |
286 | 113 |
|
287 |
int blockWidthMargin = block.Width / 2;
|
|
288 |
int blockHeightMargin = block.Height / 2;
|
|
114 |
SetStatus("Comparison", 0, CompareStatus.Comparison);
|
|
115 |
contoursLongCount = contours.Sum(x => x.Count());
|
|
289 | 116 |
|
290 |
var rects = contours.SelectMany(p => p).OrderBy(p=>p.X + p.Y) |
|
291 |
.Select(p=> |
|
292 |
new System.Windows.Rect { |
|
293 |
X = p.X - blockWidthMargin, |
|
294 |
Y = p.Y - blockHeightMargin, |
|
295 |
Width = block.Width, |
|
296 |
Height = block.Height |
|
297 |
}) |
|
298 |
.ToArray(); |
|
117 |
var rects = contours.AsParallel() |
|
118 |
.Select(points => GetRectList(points, block)) |
|
119 |
.SelectMany(x => x); |
|
299 | 120 |
|
300 |
for (int i = 0; i < rects.Count(); i++) |
|
301 |
{ |
|
302 |
if (results.Count(r => r.IntersectsWith(rects[i])) == 0) |
|
303 |
{ |
|
304 |
results.Add(rects[i]); |
|
305 |
} |
|
306 |
} |
|
307 |
|
|
308 |
//for (int i = 0; i < contours.Count(); i++) |
|
309 |
//{ |
|
310 |
// for (int j = 0; j < contours[i].Count(); j++) |
|
311 |
// { |
|
312 |
// if (results.Count(x => x.Contains())) |
|
313 |
// { |
|
314 |
// results.Add(contours) |
|
315 |
// } |
|
316 |
// } |
|
317 |
|
|
318 |
// Cv2.DrawContours(data, contours, i, Scalar.Red, 1, LineType.AntiAlias); |
|
319 |
//} |
|
320 |
//Cv2.ImWrite("d:\\testsave\\draw.png", data); |
|
321 |
|
|
322 |
System.Diagnostics.Debug.WriteLine("1 - " + new TimeSpan(stopwatch.ElapsedTicks)); |
|
121 |
results.AddRange(rects); |
|
122 |
|
|
323 | 123 |
return results; |
324 | 124 |
} |
325 | 125 |
|
326 |
|
|
327 |
/// <summary> |
|
328 |
/// MathchesImageData의 틀린 부분을 Rect로 반환 |
|
329 |
/// </summary> |
|
330 |
/// <param name="data"></param> |
|
331 |
/// <param name="currentX"></param> |
|
332 |
/// <param name="currentY"></param> |
|
333 |
/// <param name="ImageWidth"></param> |
|
334 |
/// <param name="ImageHeight"></param> |
|
335 |
/// <param name="block"></param> |
|
336 |
/// <returns></returns> |
|
337 |
protected System.Windows.Rect? DataMatchScore(OpenCvSharp.CPlusPlus.Mat mat, int currentX, int currentY, int ImageWidth, int ImageHeight, Size block) |
|
126 |
private List<System.Windows.Rect> GetRectList(OpenCvSharp.CPlusPlus.Point[] points,Size block) |
|
338 | 127 |
{ |
339 |
System.Windows.Rect? result = null; |
|
340 |
|
|
341 |
int x = currentX; |
|
342 |
int y = currentY; |
|
343 |
int width = ImageWidth; |
|
344 |
int height = ImageHeight; |
|
128 |
List<System.Windows.Rect> result = new List<System.Windows.Rect>(); |
|
345 | 129 |
|
346 |
for (int i = 0; i < block.Width; i++)
|
|
130 |
for (int i = 0; i < points.Count(); i++)
|
|
347 | 131 |
{ |
348 |
int wi = x + i; |
|
349 |
if (wi >= width) break; |
|
350 |
|
|
351 |
for (int j = 0; j < block.Height; j++) |
|
132 |
var rect = new System.Windows.Rect |
|
352 | 133 |
{ |
353 |
int hj = y + j; |
|
354 |
if (hj >= height) break; |
|
355 |
|
|
356 |
try |
|
357 |
{ |
|
358 |
//System.Diagnostics.Debug.WriteLine($"point :{wi},{hj}"); |
|
359 |
if (mat.GetValue<float>(wi,hj) < gMatchScore) |
|
360 |
{ |
|
361 |
result = new System.Windows.Rect(y, x, block.Width, block.Height); |
|
362 |
return result; |
|
363 |
} |
|
364 |
} |
|
365 |
catch (Exception e) |
|
366 |
{ |
|
367 |
System.Diagnostics.Debug.WriteLine(e); |
|
368 |
} |
|
134 |
X = points[i].X - block.Width / 2, |
|
135 |
Y = points[i].Y - block.Height / 2, |
|
136 |
Width = block.Width, |
|
137 |
Height = block.Height |
|
138 |
}; |
|
369 | 139 |
|
140 |
if (result.Count(r => r.IntersectsWith(rect)) == 0) |
|
141 |
{ |
|
142 |
result.Add(rect); |
|
370 | 143 |
} |
371 |
} |
|
372 |
|
|
373 |
return result; |
|
374 |
} |
|
375 |
|
|
376 |
/// <summary> |
|
377 |
/// MathchesImageData의 틀린 부분을 Rect로 반환 |
|
378 |
/// </summary> |
|
379 |
/// <param name="data"></param> |
|
380 |
/// <param name="currentX"></param> |
|
381 |
/// <param name="currentY"></param> |
|
382 |
/// <param name="ImageWidth"></param> |
|
383 |
/// <param name="ImageHeight"></param> |
|
384 |
/// <param name="block"></param> |
|
385 |
/// <returns></returns> |
|
386 |
protected System.Windows.Rect? DataMatchScore(IEnumerable<ImageData<float>> data, int currentX, int currentY, int ImageWidth, int ImageHeight, Size block) |
|
387 |
{ |
|
388 |
System.Windows.Rect? result = null; |
|
389 | 144 |
|
390 |
int x = currentX; |
|
391 |
int y = currentY; |
|
392 |
int width = ImageWidth; |
|
393 |
int height = ImageHeight; |
|
145 |
ComparisonCount++; |
|
394 | 146 |
|
395 |
for (int i = 0; i < block.Width; i++) |
|
396 |
{ |
|
397 |
int wi = x + i; |
|
398 |
if (wi >= width) break; |
|
399 |
|
|
400 |
for (int j = 0; j < block.Height; j++) |
|
401 |
{ |
|
402 |
int hj = y + j; |
|
403 |
if (hj >= height) break; |
|
404 |
|
|
405 |
try |
|
406 |
{ |
|
407 |
//System.Diagnostics.Debug.WriteLine($"point :{wi},{hj}"); |
|
408 |
if (data.FirstOrDefault(d=> d.Row == wi && d.Col == hj)?.Value < gMatchScore) |
|
409 |
{ |
|
410 |
result = new System.Windows.Rect(y, x, block.Width, block.Height); |
|
411 |
return result; |
|
412 |
} |
|
413 |
} |
|
414 |
catch (Exception e) |
|
415 |
{ |
|
416 |
System.Diagnostics.Debug.WriteLine(e); |
|
417 |
} |
|
418 |
|
|
419 |
} |
|
147 |
SetStatus("Comparison", ComparisonCount/contoursLongCount*100, CompareStatus.Comparison); |
|
420 | 148 |
} |
421 | 149 |
|
422 | 150 |
return result; |
423 | 151 |
} |
152 |
|
|
424 | 153 |
/// <summary> |
425 | 154 |
/// Image<TColor, TDepth>의 틀린 부분을 Rect로 반환 |
426 | 155 |
/// </summary> |
ImageComparer/Markus.ImageComparer/Markus.Image.csproj | ||
---|---|---|
65 | 65 |
<Reference Include="WindowsBase" /> |
66 | 66 |
</ItemGroup> |
67 | 67 |
<ItemGroup> |
68 |
<Compile Include="CompareStatus.cs" /> |
|
68 | 69 |
<Compile Include="ImageComparer.cs" /> |
69 | 70 |
<Compile Include="ImageComparerBase.cs" /> |
70 | 71 |
<Compile Include="OpenCVExtensions.cs" /> |
내보내기 Unified diff