markus / CompareLib / CompareLib.cs @ 787a4489
이력 | 보기 | 이력해설 | 다운로드 (13.8 KB)
1 |
//using Leadtools; |
---|---|
2 |
using System; |
3 |
using System.Collections.Generic; |
4 |
using System.Linq; |
5 |
using System.Text; |
6 |
using System.Windows; |
7 |
|
8 |
using OpenCvSharp; |
9 |
namespace MARKUS |
10 |
{ |
11 |
public class CompareLib |
12 |
{ |
13 |
|
14 |
string ColorString_Stroke = @"#0000FF"; |
15 |
string ColorString_LowLight = @"#FFFFFF"; |
16 |
string ColorString_Highlight = @"#0000FF"; |
17 |
|
18 |
public CompareLib() |
19 |
{ |
20 |
//this.SetLicense(); |
21 |
} |
22 |
public string ImageCompare(System.IO.Stream originalPng, System.IO.Stream targetPng) |
23 |
{ |
24 |
using (var img1 = new ImageMagick.MagickImage(originalPng)) |
25 |
{ |
26 |
img1.ColorFuzz = new ImageMagick.Percentage(80); |
27 |
//img1.Grayscale(ImageMagick.PixelIntensityMethod.Average); |
28 |
using (var img2 = new ImageMagick.MagickImage(targetPng)) |
29 |
{ |
30 |
//img2.ColorFuzz = new ImageMagick.Percentage(80); |
31 |
//img2.Grayscale(ImageMagick.PixelIntensityMethod.Average); |
32 |
using (var imgDiff = new ImageMagick.MagickImage()) |
33 |
{ |
34 |
img1.StrokeColor = new ImageMagick.MagickColor(ColorString_Stroke); |
35 |
//img1.SetLowlightColor(ImageMagick.MagickColor.Transparent); |
36 |
img1.SetLowlightColor(new ImageMagick.MagickColor(ColorString_LowLight)); |
37 |
img1.SetHighlightColor(new ImageMagick.MagickColor(ColorString_Highlight)); |
38 |
//img1.Composite(img2, CompositeOperator.SrcOver); |
39 |
double diff = img1.Compare(img2, new ImageMagick.ErrorMetric(), imgDiff); |
40 |
//img1.Composite(imgDiff, CompositeOperator.Over); |
41 |
string outputFile = System.IO.Path.GetTempFileName().Replace(".tmp",".png"); |
42 |
imgDiff.Write(outputFile); |
43 |
return outputFile; |
44 |
} |
45 |
} |
46 |
} |
47 |
} |
48 |
|
49 |
public string ImageCompare_File(string originalPng, string targetPng) |
50 |
{ |
51 |
using (var img1 = new ImageMagick.MagickImage(originalPng)) |
52 |
{ |
53 |
img1.ColorFuzz = new ImageMagick.Percentage(80); |
54 |
using (var img2 = new ImageMagick.MagickImage(targetPng)) |
55 |
{ |
56 |
//img2.Grayscale(ImageMagick.PixelIntensityMethod.Average); |
57 |
//img2.ColorFuzz = new ImageMagick.Percentage(60); |
58 |
using (var imgDiff = new ImageMagick.MagickImage()) |
59 |
{ |
60 |
img1.StrokeColor = new ImageMagick.MagickColor(ColorString_Stroke); |
61 |
//img1.SetLowlightColor(ImageMagick.MagickColor.Transparent); |
62 |
img1.SetLowlightColor(new ImageMagick.MagickColor(ColorString_LowLight)); |
63 |
img1.SetHighlightColor(new ImageMagick.MagickColor(ColorString_Highlight)); |
64 |
//img1.Composite(img2, ImageMagick.CompositeOperator.Src); |
65 |
double diff = img1.Compare(img2, new ImageMagick.ErrorMetric(), imgDiff); |
66 |
//img1.Composite(imgDiff, CompositeOperator.Over); |
67 |
string outputFile = System.IO.Path.GetTempFileName().Replace(".tmp", ".png"); |
68 |
imgDiff.Write(outputFile); |
69 |
return outputFile; |
70 |
} |
71 |
} |
72 |
} |
73 |
} |
74 |
private static bool CheckInBound(OpenCvSharp.Rect rect, List<OpenCvSharp.Rect> list) |
75 |
{ |
76 |
foreach (var r in list) |
77 |
{ |
78 |
if (rect.X > r.X && rect.Y > r.Y && |
79 |
(rect.X + rect.Width) < (r.X + r.Width) && |
80 |
(rect.Y + rect.Height) < (r.Y + r.Height)) |
81 |
return false; |
82 |
} |
83 |
return true; |
84 |
} |
85 |
|
86 |
public List<System.Windows.Rect> GetBoundBoxInImage(string targetImage) |
87 |
{ |
88 |
List<System.Windows.Rect> pointSet = new List<System.Windows.Rect>(); |
89 |
var gray = new Mat(targetImage, ImreadModes.GrayScale); |
90 |
var org = new Mat(targetImage); |
91 |
|
92 |
var blur = new Mat(); |
93 |
Cv2.GaussianBlur(gray, blur, new OpenCvSharp.Size(9, 9), 0); |
94 |
|
95 |
#region get canny |
96 |
var canny = new Mat(); |
97 |
Cv2.Canny(blur, canny, 20, 135); |
98 |
#endregion |
99 |
|
100 |
|
101 |
#region find contours |
102 |
OpenCvSharp.Point[][] contours; |
103 |
HierarchyIndex[] hierarchyIndexes; |
104 |
//Cv2.FindContours( |
105 |
// canny, |
106 |
// out contours, |
107 |
// out hierarchyIndexes, |
108 |
// mode: RetrievalModes.External, |
109 |
// method: ContourApproximationModes.ApproxTC89KCOS); |
110 |
Cv2.FindContours( |
111 |
canny, |
112 |
out contours, |
113 |
out hierarchyIndexes, |
114 |
mode: RetrievalModes.Tree, |
115 |
method: ContourApproximationModes.ApproxTC89KCOS); |
116 |
#endregion |
117 |
|
118 |
Console.WriteLine($"contours={contours.Length}"); |
119 |
|
120 |
//get rect info from contour |
121 |
var rectList = new List<OpenCvSharp.Rect>(); |
122 |
var rectDraw = new List<OpenCvSharp.Rect>(); |
123 |
foreach (var c in contours) |
124 |
{ |
125 |
//skip too small obj |
126 |
if (c.Length > 5) |
127 |
rectList.Add(Cv2.BoundingRect(c)); |
128 |
} |
129 |
|
130 |
|
131 |
for (int i = 0; i < rectList.Count; i++) |
132 |
{ |
133 |
//only draw bigger one if fully overlap |
134 |
if (CheckInBound(rectList[i], rectList)) |
135 |
{ |
136 |
rectDraw.Add(rectList[i]); |
137 |
} |
138 |
} |
139 |
|
140 |
|
141 |
var image = org.Clone(); |
142 |
foreach (var rect in rectDraw) |
143 |
{ |
144 |
var width = rect.X + rect.Width; |
145 |
var height = rect.Y + rect.Height; |
146 |
pointSet.Add(new System.Windows.Rect(rect.X, rect.Y, rect.Width, rect.Height)); |
147 |
|
148 |
//var midPoint = getMiddlePoint(new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(width, height)); |
149 |
//Cv2.Ellipse(image, midPoint, new OpenCvSharp.Size(rect.Width / 1.2, rect.Height / 1.2), 0, 0, 359, Scalar.Red, 2, LineTypes.Link8); |
150 |
//Cv2.Rectangle(image, new OpenCvSharp.Point(rect.X, rect.Y), new OpenCvSharp.Point(width, height), Scalar.Green, 2); |
151 |
|
152 |
} |
153 |
|
154 |
//using (new Window("image", image)) |
155 |
////using (new Window("gray blur", blur)) |
156 |
////using (new Window("canny", canny)) |
157 |
|
158 |
//{ |
159 |
// Cv2.WaitKey(); |
160 |
//} |
161 |
|
162 |
return pointSet; |
163 |
} |
164 |
public static OpenCvSharp.Point getMiddlePoint(OpenCvSharp.Point p1, OpenCvSharp.Point p2) |
165 |
{ |
166 |
return new OpenCvSharp.Point { X = (p1.X + p2.X) / 2, Y = (p1.Y + p2.Y) / 2 }; |
167 |
} |
168 |
|
169 |
public System.IO.Stream GetStreamFromUrl(string url) |
170 |
{ |
171 |
byte[] imageData = null; |
172 |
|
173 |
using (var wc = new System.Net.WebClient()) |
174 |
imageData = wc.DownloadData(url); |
175 |
|
176 |
return new System.IO.MemoryStream(imageData); |
177 |
} |
178 |
|
179 |
//public System.IO.Stream ChangeCmpUrlToPng(string url) |
180 |
//{ |
181 |
// Leadtools.Codecs.RasterCodecs codecs = new Leadtools.Codecs.RasterCodecs(); |
182 |
// // Load an image at its own bits per pixel |
183 |
|
184 |
|
185 |
// Leadtools.RasterImage image = codecs.Load(GetStreamFromUrl(url), 0, Leadtools.Codecs.CodecsLoadByteOrder.Bgr, 1, 1); |
186 |
// System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
187 |
// //string tempFile = System.IO.Path.GetTempFileName(); |
188 |
// // Save the image as a 24-bit Windows BMP file |
189 |
// codecs.Save(image, stream, Leadtools.RasterImageFormat.Png, image.BitsPerPixel); |
190 |
// // Dispose of the objects we created |
191 |
// image.Dispose(); |
192 |
// codecs.Dispose(); |
193 |
|
194 |
// return stream; |
195 |
//} |
196 |
|
197 |
//강인구 수정 |
198 |
public string ChangeCmpUrlToPng_File(string url) |
199 |
{ |
200 |
System.Net.WebClient client = new System.Net.WebClient(); |
201 |
|
202 |
byte[] imageBytes = client.DownloadData(new Uri(url)); |
203 |
|
204 |
//string tempFile = System.IO.Path.GetTempFileName().Replace(@".tmp", @".cmp"); |
205 |
string tempFile = System.IO.Path.GetTempFileName().Replace(@".tmp", @".png"); |
206 |
|
207 |
System.Drawing.Image image; |
208 |
System.IO.MemoryStream ms = new System.IO.MemoryStream(imageBytes, 0, imageBytes.Length); |
209 |
ms.Write(imageBytes, 0, imageBytes.Length); |
210 |
image = System.Drawing.Image.FromStream(ms, true);//Exception occurs here |
211 |
|
212 |
image.Save(tempFile, System.Drawing.Imaging.ImageFormat.Png); |
213 |
|
214 |
return tempFile; |
215 |
|
216 |
//var image = new System.Windows.Media.Imaging.BitmapImage(new Uri(url)); |
217 |
|
218 |
//ViewerDataModel.Instance.ImageViewPath_C = defaultBitmapImage_Compare; |
219 |
//ViewerDataModel.Instance.ImageViewWidth_C = defaultBitmapImage_Compare.PixelWidth; |
220 |
//ViewerDataModel.Instance.ImageViewHeight_C = defaultBitmapImage_Compare.PixelHeight; |
221 |
|
222 |
//if (image.IsDownloading) |
223 |
//{ |
224 |
// image.DownloadCompleted += (ex, arg) => |
225 |
// { |
226 |
// System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
227 |
// tempFile = System.IO.Path.GetTempFileName().Replace(@".tmp", @".cmp"); |
228 |
// }; |
229 |
// return tempFile; |
230 |
//} |
231 |
//else |
232 |
//{ |
233 |
// return tempFile; |
234 |
//} |
235 |
|
236 |
//Leadtools.Codecs.RasterCodecs codecs = new Leadtools.Codecs.RasterCodecs(); |
237 |
//Load an image at its own bits per pixel |
238 |
|
239 |
|
240 |
//Leadtools.RasterImage image = codecs.Load(GetStreamFromUrl(url), 0, Leadtools.Codecs.CodecsLoadByteOrder.Bgr, 1, 1); |
241 |
//System.IO.MemoryStream stream = new System.IO.MemoryStream(); |
242 |
//string tempFile = System.IO.Path.GetTempFileName().Replace(@".tmp", @".png"); |
243 |
//Save the image as a 24 - bit Windows BMP file |
244 |
//codecs.Save(image, tempFile, Leadtools.RasterImageFormat.Png, image.BitsPerPixel); |
245 |
//Dispose of the objects we created |
246 |
//image.Dispose(); |
247 |
//codecs.Dispose(); |
248 |
} |
249 |
|
250 |
|
251 |
|
252 |
//public string ChangeCmpToPng(string fileName) |
253 |
//{ |
254 |
// Leadtools.Codecs.RasterCodecs codecs = new Leadtools.Codecs.RasterCodecs(); |
255 |
// // Load an image at its own bits per pixel |
256 |
// Leadtools.RasterImage image = codecs.Load(fileName,0,Leadtools.Codecs.CodecsLoadByteOrder.Bgr,1,1); |
257 |
// string tempFile = System.IO.Path.GetTempFileName(); |
258 |
// // Save the image as a 24-bit Windows BMP file |
259 |
// codecs.Save(image, tempFile, Leadtools.RasterImageFormat.Png, image.BitsPerPixel); |
260 |
// // Dispose of the objects we created |
261 |
// image.Dispose(); |
262 |
// codecs.Dispose(); |
263 |
|
264 |
// return tempFile; |
265 |
//} |
266 |
|
267 |
//public bool SetLicense() |
268 |
//{ |
269 |
// try |
270 |
// { |
271 |
// // TODO: Change this to use your license file and developer key */ |
272 |
// //string licenseFilePath = "Replace this with the path to the LEADTOOLS license file"; |
273 |
// //string developerKey = "Replace this with your developer key"; |
274 |
// //RasterSupport.SetLicense(licenseFilePath, developerKey); |
275 |
// } |
276 |
// catch (Exception ex) |
277 |
// { |
278 |
// System.Diagnostics.Debug.Write(ex.Message); |
279 |
// } |
280 |
|
281 |
// if (RasterSupport.KernelExpired) |
282 |
// { |
283 |
// string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); |
284 |
// /* Try the common LIC directory */ |
285 |
// //string licenseFileRelativePath = "C:\\LEADTOOLS 19\\Common\\License\\LEADTOOLS.LIC"; |
286 |
// //string keyFileRelativePath = "C:\\LEADTOOLS 19\\Common\\License\\LEADTOOLS.LIC.key"; |
287 |
|
288 |
// ////강인구 추가(라이선스 경로 변경) |
289 |
// string licenseFileRelativePath = @"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC"; |
290 |
// string keyFileRelativePath = @"C:\LEADTOOLS 19\Common\License\LEADTOOLS.LIC.KEY"; |
291 |
|
292 |
// //string licenseFileRelativePath = @"F:\full_license.key\full_license.lic"; |
293 |
// //string keyFileRelativePath = @"F:\full_license.key\full_license.key"; |
294 |
|
295 |
// if (System.IO.File.Exists(licenseFileRelativePath) && System.IO.File.Exists(keyFileRelativePath)) |
296 |
// { |
297 |
// string developerKey = System.IO.File.ReadAllText(keyFileRelativePath); |
298 |
// try |
299 |
// { |
300 |
// RasterSupport.SetLicense(licenseFileRelativePath, developerKey); |
301 |
// } |
302 |
// catch (Exception ex) |
303 |
// { |
304 |
// System.Diagnostics.Debug.Write(ex.Message); |
305 |
// } |
306 |
// } |
307 |
// } |
308 |
|
309 |
// if (RasterSupport.KernelExpired) |
310 |
// { |
311 |
// string msg = "Your license file is missing, invalid or expired. LEADTOOLS will not function. Please contact LEAD Sales for information on obtaining a valid license."; |
312 |
// string logmsg = string.Format("*** NOTE: {0} ***{1}", msg, Environment.NewLine); |
313 |
// System.Diagnostics.Debugger.Log(0, null, "*******************************************************************************" + Environment.NewLine); |
314 |
// System.Diagnostics.Debugger.Log(0, null, logmsg); |
315 |
// System.Diagnostics.Debugger.Log(0, null, "*******************************************************************************" + Environment.NewLine); |
316 |
|
317 |
// System.Diagnostics.Process.Start("https://www.leadtools.com/downloads/evaluation-form.asp?evallicenseonly=true"); |
318 |
|
319 |
// return false; |
320 |
// } |
321 |
|
322 |
// return true; |
323 |
//} |
324 |
} |
325 |
} |