static NativeMethods() { IntPtr userData; ErrorCallback = ErrorExceptionCallback; cvRedirectError(ErrorCallback, IntPtr.Zero, out userData); }
public static extern IntPtr cvRedirectError(CvErrorCallback error_handler, IntPtr userdata, ref IntPtr prev_userdata);
public static extern IntPtr redirectError(CvErrorCallback errCallback, IntPtr userdata, ref IntPtr prevUserdata);
public static extern IntPtr RedirectError( CvErrorCallback errorHandler, IntPtr userdata, IntPtr prevUserdata);
/// <summary> /// Sets a new error handler that can be one of standard handlers or a custom handler that has the certain interface. The handler takes the same parameters as cvError function. If the handler returns non-zero value, the program is terminated, otherwise, it continues. The error handler may check the current error mode with cvGetErrMode to make a decision. /// </summary> /// <param name="errorHandler">The new error handler</param> /// <param name="userdata">Arbitrary pointer that is transparently passed to the error handler.</param> /// <param name="prevUserdata">Pointer to the previously assigned user data pointer.</param> /// <returns></returns> public static IntPtr RedirectError( CvErrorCallback errorHandler, IntPtr userdata, IntPtr prevUserdata) { return IntPtr.Zero; }
static void Main(string[] args) { DateTime start = DateTime.Now; ConsoleSpinner spin = new ConsoleSpinner(); Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .WriteTo.Console() .WriteTo.File("log.txt") .CreateLogger(); //string dir = @"c:\Users\AL\Documents\Visual Studio 2017\Projects\DupFinder\test"; string dir = @"B:\DiskE\BACKUP"; Log.Information("Analyzing {0}...", dir); DirectoryInfo dirInfo = new DirectoryInfo(dir); var files = dirInfo.EnumerateDirectories() .AsParallel() .SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories)) .Where(x => Array.IndexOf(Ext, x.Extension.ToLowerInvariant()) >= 0) .ToArray(); TimeSpan duration = DateTime.Now - start; Log.Information("Found {0} file(s), duration {1} sec", files.Count(), duration.TotalSeconds); Log.Information("Calculating images' hashes..."); CvErrorCallback cvErr = CVError; IntPtr zero = IntPtr.Zero; NativeMethods.redirectError(cvErr, zero, ref zero); Console.SetError(TextWriter.Null); int c = 0; Parallel.ForEach(files, f => { try { RadialVarianceHash h1 = RadialVarianceHash.Create(); BlockMeanHash h2 = BlockMeanHash.Create(); byte[] data = System.IO.File.ReadAllBytes(f.FullName); using (Mat mat = Mat.FromImageData(data, ImreadModes.Color)) { InputOutputArray h1o = new Mat(); InputOutputArray h2o = new Mat(); h1.Compute(mat, h1o); h2.Compute(mat, h2o); Files.Add(new File { FileInfo = f, RadialVarianceHash = h1o, BlockMeanHash = h2o, }); spin.Turn((double)(c++) / (double)files.Count()); } } catch (Exception ex) { if (ex.Source != "OpenCvSharp") { Log.Error("Error with {0}:\n\t{1}", f.FullName, ex.Message); } } }); Log.Information("Finding duplicates..."); RadialVarianceHash hh1 = RadialVarianceHash.Create(); BlockMeanHash hh2 = BlockMeanHash.Create(); ulong saveSize = 0; bool dupMsg = true; int total = Files.Count; int counter = 0; for (int x = 0; x < Files.Count; x++) { if (Files.ElementAt(x).Duplicate != true) { counter++; dupMsg = true; for (int y = x + 1; y < Files.Count; y++) { if (Files.ElementAt(y).Duplicate == true) { continue; } double compare1 = hh1.Compare(InputArray.Create(Files.ElementAt(x).RadialVarianceHash.GetMat()), InputArray.Create(Files.ElementAt(y).RadialVarianceHash.GetMat())); double compare2 = hh2.Compare(InputArray.Create(Files.ElementAt(x).BlockMeanHash.GetMat()), InputArray.Create(Files.ElementAt(y).BlockMeanHash.GetMat())); if (compare1 > 0.98 && compare2 < 3) { saveSize += (ulong)Files.ElementAt(x).FileInfo.Length; Files.ElementAt(y).Duplicate = true; total--; if (dupMsg) { Log.Information("Dups for {0}:", Files.ElementAt(x).FileInfo.FullName); dupMsg = false; } Log.Information("\t{0}", Files.ElementAt(y).FileInfo.FullName); } spin.Turn((double)counter / (double)total); } } spin.Turn((double)counter / (double)total); } Log.Information("Done, possible size save is {0} Mb", saveSize / 1024 / 1024); Console.ReadKey(); }
internal static extern IntPtr cvRedirectError(CvErrorCallback error_handler, IntPtr userdata, out IntPtr prevUserdata);