Example #1
0
    /// <summary>
    /// Index image folder and search for similar images
    /// </summary>
    /// <param name="task_parameters"></param>
    /// <returns>Similar images : image -> similarity % </returns>
    private Dictionary<string, int> SearchSimilarTask(object task_parameters)
    {
        object[] parameters = (object[])task_parameters;
        var folder = (string)parameters[0];

        var similar_images = new Dictionary<string, int>();

        if (!cancellationToken.IsCancellationRequested)
            {
                Thread.Sleep(20);
                ((IProgress<string>)progressManager).Report("Indexing");
                images_index.IndexImageDirectory(folder);
                Thread.Sleep(20);

                ((IProgress<string>)progressManager).Report("Searching");
                similar_images = images_index.SearchSimilarImages(image_of_interest, option_image_hash, option_filter_colors, option_similarity_cutoff);

                ((IProgress<string>)progressManager).Report("Processing matches");
                Thread.Sleep(20);
            }

        return similar_images;
    }
Example #2
0
        public static void Main(string[] args)
        {
            var parameters = ReadCommandLineParameters(args);

            Stopwatch stopWatch = new Stopwatch();

            Console.WriteLine("Find Similar Images");
            Console.WriteLine("-------------------");
            Console.WriteLine("  image directory : {0}", parameters.image_directory);
            Console.WriteLine("image of interest : {0}", parameters.image_of_interest);
            Console.WriteLine("      search mode : {0}", parameters.search_mode);
            if (!parameters.search_mode.Equals("index"))
            {
                Console.WriteLine("       image hash : {0}", parameters.image_hash_algorithm.ToString());
                Console.WriteLine(" pixel comparison : {0}", parameters.color_filter.ToString());
                Console.WriteLine("       similarity : {0}%", parameters.min_similarity);
            }
            Console.WriteLine("-------------------");

            // Index images files in directory
            ////////////////////////////////////////////////

            stopWatch.Reset();
            stopWatch.Start();

            //var images_index = new Images.ImageIndex(parameters.image_directory);
            var images_index = new Images.ImageIndexMulti();

            images_index.IndexImageDirectory(parameters.image_directory);
            Console.WriteLine("");

            stopWatch.Stop();
            TimeSpan ts_index = stopWatch.Elapsed;

            PrintElapsedTime(ts_index);

            var image_files = images_index.ImageFilesIndexed();

            Console.WriteLine("* {1} images in directory \"{0}\"", parameters.image_directory, image_files.Count().ToString());

            if (parameters.search_mode.Equals("index"))
            {
                System.Environment.Exit(0);
            }

            // find similar images and export as html page
            ////////////////////////////////////////////////

            Console.WriteLine("* Search for similar images");
            stopWatch.Reset();
            stopWatch.Start();

            if (File.Exists(parameters.image_of_interest))
            {
                // Case :find images similar to an image of interest in a directory
                try
                {
                    if (!Path.IsPathRooted(parameters.image_of_interest))
                    {
                        parameters.image_of_interest = Path.Combine(Directory.GetCurrentDirectory(), parameters.image_of_interest);
                    }

                    var similar_images = images_index.SearchSimilarImages(parameters.image_of_interest, parameters.image_hash_algorithm, parameters.color_filter, parameters.min_similarity);
                    SaveSimilarHtml(parameters.image_of_interest, parameters.image_of_interest, parameters.min_similarity, similar_images, images_index);
                }
                catch (Exception e)
                {
                    Console.Error.WriteLine("Error  => Search similar images {0}: \n{1}", parameters.image_of_interest, e.Message);
                }
            }
            else
            {
                // Case :find groups of similar images in a directory

                var seen = new Dictionary <string, byte>();

                foreach (var image in image_files)
                {
                    if (!seen.ContainsKey(image))
                    {
                        try
                        {
                            seen.Add(image, 1);

                            var similar_images = images_index.SearchSimilarImages(image, parameters.image_hash_algorithm, parameters.color_filter, parameters.min_similarity);
                            SaveSimilarHtml(parameters.image_of_interest, image, parameters.min_similarity, similar_images, images_index);
                            foreach (var simage in similar_images.Keys)
                            {
                                if (!seen.ContainsKey(simage))
                                {
                                    seen.Add(simage, 1);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("Error  => Search similar images {0}: \n{1}", image, e);
                        }
                    }
                }
            }

            stopWatch.Stop();
            TimeSpan ts_search = stopWatch.Elapsed;

            PrintElapsedTime(ts_search);
        }