Exemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="arguments"></param>
        public static void Execute(Arguments arguments)
        {
            string date = "# DATE AND TIME: " + DateTime.Now;

            LoggedConsole.WriteLine("Test of OtsuThresholder class");
            LoggedConsole.WriteLine(date);
            LoggedConsole.WriteLine();

            // Load Source image
            Image srcImage = null;

            try
            {
                srcImage = ImageTools.ReadImage2Bitmap(arguments.InputImageFile.FullName);
            }
            catch (IOException ioE)
            {
                Console.Error.WriteLine(ioE);
                Environment.Exit(1);
            }

            int width  = srcImage.Width;
            int height = srcImage.Height;

            /*
             *
             * // Get raw image data
             * byte[,] M = ConvertColourImageToGreyScaleMatrix((Bitmap)srcImage);
             *
             * // Sanity check image
             * if ((width * height) != (M.GetLength(0) * M.GetLength(1)))
             * {
             *  Console.Error.WriteLine("Unexpected image data size.");
             *  Environment.Exit(1);
             * }
             *
             * // Output Image info
             * //Console.WriteLine("Loaded image: '%s', width: %d, height: %d, num bytes: %d\n", filename, width, height, srcData.Length);
             *
             * byte[] vector = DataTools.Matrix2Array(M);
             * byte[] outputArray;
             *
             * // Create Otsu Thresholder
             * OtsuThresholder thresholder = new OtsuThresholder();
             * int threshold = thresholder.CalculateThreshold(vector, out outputArray);
             *
             * byte[,] opByteMatrix = DataTools.Array2Matrix(outputArray, width, height);
             */

            byte[,] matrix     = ConvertColourImageToGreyScaleMatrix((Bitmap)srcImage);
            double[,] ipMatrix = MatrixTools.ConvertMatrixOfByte2Double(matrix);

            byte[,] opByteMatrix;
            Image  histoImage;
            double threshold;

            GetGlobalOtsuThreshold(ipMatrix, out opByteMatrix, out threshold, out histoImage);
            Console.WriteLine("Threshold: {0}", threshold);

            Image opImage = ConvertMatrixToGreyScaleImage(opByteMatrix);

            Image[] imageArray = { srcImage, opImage, histoImage };

            Image images = ImageTools.CombineImagesVertically(imageArray);

            images.Save(arguments.OutputFileName.FullName);
        }