Пример #1
0
        public void ComputeMode1()
        {
            using (var model = BlockMeanHash.Create(BlockMeanHashMode.Mode1))
                using (var img = Image("lenna.png"))
                    using (var hash = new Mat <byte>())
                    {
                        model.Compute(img, hash);
                        Assert.Equal(1, hash.Rows);
                        Assert.Equal(121, hash.Cols);
                        Assert.Equal(MatType.CV_8UC1, hash.Type());

                        var hashArray = hash.ToArray();
                        Assert.Equal(
                            new byte[]
                        {
                            135, 255, 243, 135, 131, 255, 249, 199, 193, 255, 252, 227, 224, 127, 254, 113, 128, 127, 127,
                            48, 192, 191, 25, 24, 240, 255, 4, 13, 248, 127, 135, 134, 252, 255, 99, 67, 254, 255, 184, 113,
                            255, 127, 220, 240, 195, 31, 111, 120, 224, 135, 55, 12, 248, 192, 27, 6, 126, 240, 13, 128, 63,
                            248, 6, 64, 14, 126, 3, 48, 7, 191, 3, 248, 131, 159, 1, 248, 193, 207, 0, 252, 240, 103, 0,
                            126, 248, 59, 0, 63, 254, 29, 0, 15, 255, 14, 0, 135, 127, 6, 128, 131, 63, 3, 224, 103, 206, 0,
                            240, 247, 99, 0, 248, 255, 49, 0, 252, 255, 24, 0, 254, 49, 0
                        },
                            hashArray);
                    }
        }
Пример #2
0
 public void CompareSameImage(BlockMeanHashMode mode)
 {
     using (var model = BlockMeanHash.Create(mode))
         using (var img1 = Image("lenna.png", ImreadModes.GrayScale))
         {
             double hash = model.Compare(img1, img1);
             Assert.Equal(0, hash, 6);
         }
 }
Пример #3
0
 public void CreateAndDispose()
 {
     using (var model = BlockMeanHash.Create(BlockMeanHashMode.Mode0))
     {
         GC.KeepAlive(model);
     }
     using (var model = BlockMeanHash.Create(BlockMeanHashMode.Mode1))
     {
         GC.KeepAlive(model);
     }
 }
Пример #4
0
 public void CompareDifferentImage(BlockMeanHashMode mode)
 {
     using (var model = BlockMeanHash.Create(mode))
         using (var img1 = Image("lenna.png", ImreadModes.GrayScale))
             using (var img2 = Image("building.jpg", ImreadModes.GrayScale))
             {
                 var size = new Size(256, 256);
                 using (var scaledImg1 = img1.Resize(size))
                     using (var scaledImg2 = img2.Resize(size))
                     {
                         double hash = model.Compare(scaledImg1, scaledImg2);
                         Assert.Equal(264411, hash, 6);
                     }
             }
 }
Пример #5
0
        public void ComputeMode0()
        {
            using (var model = BlockMeanHash.Create(BlockMeanHashMode.Mode0))
                using (var img = Image("lenna.png"))
                    using (var hash = new MatOfByte())
                    {
                        model.Compute(img, hash);
                        Assert.Equal(1, hash.Rows);
                        Assert.Equal(32, hash.Cols);
                        Assert.Equal(MatType.CV_8UC1, hash.Type());

                        var hashArray = hash.ToArray();
                        Assert.Equal(
                            new byte[] { 243, 61, 243, 61, 195, 63, 226, 151, 226, 159, 250, 223, 50, 206, 18, 231, 130, 226, 130, 227, 130, 243, 130, 243, 2, 243, 2, 87, 2, 127, 2, 47 },
                            hashArray);
                    }
        }
Пример #6
0
        public static ulong[] MakeSoftHash(string path)
        {
            var image     = Cv2.ImRead(path);
            var hash      = new Mat();
            var algorithm = BlockMeanHash.Create(BlockMeanHashMode.Mode1);

            if (image.Empty())
            {
                return(null);
            }
            algorithm.Compute(image, hash);
            image.Dispose();
            algorithm.Dispose();
            hash.GetArray(out byte[] buffer);
            var rr = new ulong[16];

            for (int i = 0; i < 15; i++)
            {
                rr[i] = BitConverter.ToUInt64(buffer, i * 8);
            }
            rr[15] = buffer[120];
            return(rr);
        }
Пример #7
0
        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();
        }