예제 #1
0
 public SlicePicker(IFilter grayscaleFilter, IInPlaceFilter edgeDetectionFilter, SliceRatingCalculator ratingCalculator, ThumbnailGeneratorUtilities thumbnailGeneratorUtilities)
 {
     this.grayscaleFilter             = grayscaleFilter;
     this.edgeDetectionFilter         = edgeDetectionFilter;
     this.ratingCalculator            = ratingCalculator;
     this.thumbnailGeneratorUtilities = thumbnailGeneratorUtilities;
 }
예제 #2
0
        public void GenerateThumbnails(ThumbnailGenerationParameters parameters)
        {
            var textureConverterFactory = new TextureConverterFactory();

            using (var textureConverter = textureConverterFactory.Create()) {
                IFilter               grayscaleFilter     = new Grayscale(0.3, 0.59, 0.11);
                IInPlaceFilter        edgeDetectionFilter = new HomogenityEdgeDetector();
                SliceRatingCalculator ratingCalculator    = new SliceRatingCalculator();
                var utilities    = new ThumbnailGeneratorUtilities(textureConverter);
                var slicePicker  = new SlicePicker(grayscaleFilter, edgeDetectionFilter, ratingCalculator, utilities);
                var aspectRatio  = 16 / (double)9;
                var sliceCount   = 5;
                var resultWidth  = 400;
                var resultHeight = (int)(resultWidth / aspectRatio);
                var sliceWidth   = resultWidth / sliceCount;

                var bitmaps = utilities.EnumerateBitmapsRandomly(parameters.SourceDirectory).Take(100).ToList();

                var slices        = slicePicker.PickSlices(bitmaps, new Size(sliceWidth, resultHeight));
                var sliceCombiner = new SliceCombiner(utilities, grayscaleFilter);
                for (var i = 0; i < parameters.ThumbnailsToGenerate; i++)
                {
                    var bitmap = sliceCombiner.CombineSlices(slices, kSlicesPerThumbnail);
                    using (var ms = new MemoryStream()) {
                        bitmap.Save(ms, ImageFormat.Jpeg);
                        ms.Position = 0;
                        var hash       = MD5.Create().ComputeHash(ms).ToHex();
                        var outputName = hash + "_" + DateTime.UtcNow.GetUnixTime().ToString() + ".jpg";
                        var outputPath = Path.Combine(parameters.DestinationDirectory, outputName);
                        ms.Position = 0;
                        using (var fs = File.OpenWrite(outputPath)) {
                            ms.CopyTo(fs);
                        }
                    }
                }
            }
        }
예제 #3
0
 public SliceCombiner(ThumbnailGeneratorUtilities thumbnailGeneratorUtilities, IFilter grayscaleFilter)
 {
     this.thumbnailGeneratorUtilities = thumbnailGeneratorUtilities;
     this.grayscaleFilter             = grayscaleFilter;
 }
        /// <summary>
        /// Must be run at end of slice selection algorithm.
        /// </summary>
        /// <param name="imagesAndHistogram"></param>
        /// <param name="utilities"></param>
        public static unsafe void ShowSliceSelectionDebugImage(ImagesAndHistogram imagesAndHistogram, ThumbnailGeneratorUtilities utilities)
        {
            var edgeImage            = imagesAndHistogram.EdgeImage;
            var edgeDensityHistogram = imagesAndHistogram.EdgeDensityHistogram;

            DrawHistogram(edgeImage, edgeDensityHistogram);
            DrawVerticalLine(edgeImage, edgeDensityHistogram.Median, 254);
            DrawVerticalLine(edgeImage, (int)edgeDensityHistogram.Mean, 253);
            DrawVerticalLine(edgeImage, (int)(edgeDensityHistogram.Median - edgeDensityHistogram.StdDev), 252);
            DrawVerticalLine(edgeImage, (int)(edgeDensityHistogram.Median + edgeDensityHistogram.StdDev), 252);

            var resultAspect = 16.0 / 10.0;
            var sliceCount   = 4;
            var sliceAspect  = resultAspect / sliceCount;
            var desiredWidth = (int)(edgeImage.Height * sliceAspect);

            var range = utilities.GetRangeOfWidth(edgeDensityHistogram, desiredWidth);

            DrawVerticalLine(edgeImage, range.Min, 250);
            DrawVerticalLine(edgeImage, range.Max, 250);
            ShowImage(edgeImage);
            Application.Run();
        }