Exemplo n.º 1
0
        public void IndexFiles(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null)
        {
            List <PHashImageRecord> listOfRecords = new List <PHashImageRecord>();
            string compressHash   = string.Empty;
            int    totalFileCount = imageFiles.Length;

            for (int i = 0; i < totalFileCount; i++)
            {
                var fi = imageFiles[i];
                using (Bitmap bmp = new Bitmap(Image.FromFile(fi.FullName)))
                {
                    compressHash = SimilarImage.GetCompressedImageHashAsString(bmp);
                }

                PHashImageRecord record = new PHashImageRecord
                {
                    Id           = i,
                    ImageName    = fi.Name,
                    ImagePath    = fi.FullName,
                    CompressHash = compressHash
                };
                listOfRecords.Add(record);
                IndexBgWorker.ReportProgress(i);
            }
            BinaryAlgoRepository <List <PHashImageRecord> > repo = new BinaryAlgoRepository <List <PHashImageRecord> >();

            repo.Save(listOfRecords);
        }
Exemplo n.º 2
0
        virtual protected void CompareImages(string FileName1, string FileName2)
        {
            var Hash1 = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(FileName1)), Levels);
            var Hash2 = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(FileName2)), Levels);

            Console.Write("{0:0.000000}", SimilarImage.CompareHashes(Hash1, Hash2));
        }
Exemplo n.º 3
0
        public List <ImageRecord> QueryImage(string queryImagePath, object argument = null)
        {
            List <ImageRecord> rtnImageList = new List <ImageRecord>();

            string queryImageCompressHash;

            using (Bitmap bmp = new Bitmap(System.Drawing.Image.FromFile(queryImagePath)))
            {
                queryImageCompressHash = SimilarImage.GetCompressedImageHashAsString(bmp);
            }
            BinaryAlgoRepository <List <PHashImageRecord> > repo = new BinaryAlgoRepository <List <PHashImageRecord> >();
            List <PHashImageRecord> AllImage = repo.Load();

            foreach (var imgInfo in AllImage)
            {
                var dist = SimilarImage.CompareHashes(queryImageCompressHash, imgInfo.CompressHash);
                if (dist > 0.8)
                {
                    imgInfo.Distance = dist;
                    rtnImageList.Add(imgInfo);
                }
            }

            rtnImageList = rtnImageList.OrderByDescending(x => x.Distance).ToList();

            return(rtnImageList);
        }
Exemplo n.º 4
0
        public void IndexFilesAsync(FileInfo[] imageFiles, BackgroundWorker IndexBgWorker, object argument = null)
        {
            ConcurrentBag <PHashImageRecord> listOfRecords = new ConcurrentBag <PHashImageRecord>();

            string compressHash   = string.Empty;
            int    totalFileCount = imageFiles.Length;

            int i = 0; long nextSequence;
            //In the class scope: long nextSequence;
            Object lockMe = new Object();

            Parallel.ForEach(imageFiles, currentImageFile =>
            {
                var fi = currentImageFile;
                using (Bitmap bmp = new Bitmap(Image.FromFile(fi.FullName)))
                {
                    compressHash = SimilarImage.GetCompressedImageHashAsString(bmp);
                }

                lock (lockMe)
                {
                    nextSequence = i++;
                }

                PHashImageRecord record = new PHashImageRecord
                {
                    Id           = nextSequence,
                    ImageName    = fi.Name,
                    ImagePath    = fi.FullName,
                    CompressHash = compressHash
                };

                listOfRecords.Add(record);

                IndexBgWorker.ReportProgress(i);
            });
            BinaryAlgoRepository <List <PHashImageRecord> > repo = new BinaryAlgoRepository <List <PHashImageRecord> >();

            repo.Save(listOfRecords.ToList());
        }
Exemplo n.º 5
0
        public void TestMethod1()
        {
            var Path    = "../../../../Samples";
            var Hash1_a = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test1_a.png")));
            var Hash1_b = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test1_b.png")));
            var Hash1_c = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test1_c.png")));

            var Hash2 = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test2.png")));

            var Hash3_a = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test3_a.jpg")));
            var Hash3_b = SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(Path + "/test3_b.jpg")));

            Assert.IsTrue(SimilarImage.CompareHashesBool(Hash1_a, Hash1_b));
            Assert.IsTrue(SimilarImage.CompareHashesBool(Hash1_a, Hash1_c));

            Assert.IsFalse(SimilarImage.CompareHashesBool(Hash1_a, Hash2));
            Assert.IsFalse(SimilarImage.CompareHashesBool(Hash1_a, Hash3_a));

            Assert.IsTrue(SimilarImage.CompareHashesBool(Hash3_a, Hash3_b));
            Assert.IsFalse(SimilarImage.CompareHashesBool(Hash3_a, Hash2));

            Assert.AreEqual(SimilarImage.CompareHashes(Hash1_a, Hash1_b), SimilarImage.CompareHashes(Hash1_b, Hash1_a));
        }
Exemplo n.º 6
0
 virtual protected void DisplayHash(string FileName)
 {
     Console.Write("{0}", SimilarImage.GetCompressedImageHashAsString(new Bitmap(Image.FromFile(FileName)), Levels));
 }