Beispiel #1
0
        private static void Main(string[] args)
        {
            bool   success  = false;
            string fileName = "";

            if (args.Length >= 1)
            {
                directory = Path.GetDirectoryName(args[0]);
                fileName  = Path.GetFileNameWithoutExtension(args[0]);

                success = !string.IsNullOrEmpty(fileName);
            }

            if (!success)
            {
                Console.WriteLine("Could not parse arguments: FoodClassifier.exe <filepath>");
                Console.ReadKey();
                return;
            }
            if (string.IsNullOrEmpty(directory))
            {
                directory = "";
            }

            var bitmap = new BitmapImage(new Uri(args[0], string.IsNullOrEmpty(directory) ? UriKind.Relative : UriKind.Absolute));

            // give this directory to the bitmap operations class
            BitmapOperations.saveDirectory = directory;

            // Scale the image up if it is too small or down if it is too big
            double scale = 1.0;

            if (bitmap.PixelHeight < 400 && bitmap.PixelWidth < 400)
            {
                scale = Math.Min(400.0 / bitmap.PixelWidth, 400.0 / bitmap.PixelHeight);
            }
            else if (bitmap.PixelHeight > 1000 && bitmap.PixelWidth > 1000)
            {
                scale = Math.Min(1000.0 / bitmap.PixelWidth, 1000.0 / bitmap.PixelHeight);
            }
            var resizedBitmap = new BitmapImage();

            resizedBitmap.BeginInit();
            resizedBitmap.UriSource         = bitmap.UriSource;
            resizedBitmap.DecodePixelHeight = (int)(scale * bitmap.PixelHeight);
            resizedBitmap.DecodePixelWidth  = (int)(scale * bitmap.PixelWidth);
            resizedBitmap.EndInit();

            // Reformat to BGR
            var properFormatBitmap = new FormatConvertedBitmap();

            properFormatBitmap.BeginInit();
            properFormatBitmap.Source            = resizedBitmap;
            properFormatBitmap.DestinationFormat = PixelFormats.Bgr32;
            properFormatBitmap.EndInit();

            var writeableBitmap = new WriteableBitmap(properFormatBitmap); // The ready to go bitmap
            var cvImage         = new Image <Gray, byte>(new Bitmap(args[0]));

            cvImage = cvImage.Resize(scale, INTER.CV_INTER_CUBIC);

            // var classifications = ClassifyBitmap( writeableBitmap, cvImage );

            BitmapOperations.analyzeBitmapGradient(bitmap);
        }