Exemplo n.º 1
0
        /// <summary>
        /// </summary>
        /// <param name="m">The spectral sonogram passes as matrix of doubles.</param>
        public static void DoLocalOtsuThresholding(double[,] m, out byte[,] opByteMatrix)
        {
            int byteThreshold      = 30;
            int minPercentileBound = 5;
            int maxPercentileBound = 95;
            int temporalNh         = 15;
            int freqBinNh          = 15;

            int rowCount = m.GetLength(0);
            int colCount = m.GetLength(1);

            //double[,] normM  = MatrixTools.NormaliseInZeroOne(m);
            var ipByteMatrix = MatrixTools.ConvertMatrixOfDouble2Byte(m);
            var bd1          = DataTools.GetByteDistribution(ipByteMatrix);

            opByteMatrix = new byte[rowCount, colCount];

            // for all cols i.e. freq bins
            for (int col = freqBinNh; col < colCount - freqBinNh; col++)
            {
                // for all rows i.e. frames
                for (int row = temporalNh; row < rowCount - temporalNh; row++)
                {
                    var localMatrix = MatrixTools.Submatrix(ipByteMatrix, row - temporalNh, col - freqBinNh, row + temporalNh, col + freqBinNh);

                    // debug check for min and max - make sure it worked
                    int[] bd = DataTools.GetByteDistribution(localMatrix);

                    int[] histo         = Histogram.Histo(localMatrix, out var minIntensity, out var maxIntensity);
                    int   lowerBinBound = Histogram.GetPercentileBin(histo, minPercentileBound);
                    int   upperBinBound = Histogram.GetPercentileBin(histo, maxPercentileBound);
                    int   range         = upperBinBound - lowerBinBound;

                    //normM[row, col] = (upperBinBound - lowerBinBound);
                    if (range > byteThreshold)
                    {
                        var    thresholder = new OtsuThresholder();
                        byte[] vector      = DataTools.Matrix2Array(localMatrix);
                        int    threshold   = thresholder.CalculateThreshold(vector);
                        if (localMatrix[temporalNh, freqBinNh] > threshold)
                        {
                            opByteMatrix[row, col] = 255;
                        }
                    }
                }
            }

            // debug check for min and max - make sure it worked
            var bd2 = DataTools.GetByteDistribution(opByteMatrix);

            bd2 = null;
        }
Exemplo n.º 2
0
        public static void GetOtsuThreshold(byte[,] matrix, out byte[,] m2, out int threshold)
        {
            int width  = matrix.GetLength(1);
            int height = matrix.GetLength(0);

            byte[] vector = DataTools.Matrix2Array(matrix);

            // Create Otsu Thresholder
            OtsuThresholder thresholder = new OtsuThresholder();

            threshold = thresholder.CalculateThreshold(vector, out var outputArray);
            m2        = DataTools.Array2Matrix(outputArray, width, height);
        }
Exemplo n.º 3
0
        public static void GetOtsuThreshold(byte[,] matrix, out byte[,] m2, out int threshold, out Image <Rgb24> histogramImage)
        {
            int width  = matrix.GetLength(1);
            int height = matrix.GetLength(0);

            byte[] vector = DataTools.Matrix2Array(matrix);

            // Create Otsu Thresholder
            OtsuThresholder thresholder = new OtsuThresholder();

            threshold      = thresholder.CalculateThreshold(vector, out var outputArray);
            m2             = DataTools.Array2Matrix(outputArray, width, height);
            histogramImage = CreateHistogramFrame(thresholder, height, 256);
        }
Exemplo n.º 4
0
        private static Image CreateHistogramFrame(OtsuThresholder thresholder, int width, int height)
        {
            width = 256; // histogram is one byte width.

            int[] histData  = thresholder.GetHistData();
            int   max       = thresholder.getMaxLevelValue();
            int   threshold = thresholder.getThreshold();
            var   image     = new Bitmap(width, height);

            for (int col = 0; col < width; col++)
            {
                //int ptr = (numPixels - width) + col;
                int val = height * histData[col] / max;

                if (col == threshold)
                {
                    for (int i = 0; i < height; i++)
                    {
                        image.SetPixel(col, i, Color.Red);
                    }
                }
                else
                {
                    for (int i = 1; i <= val; i++)
                    {
                        image.SetPixel(col, height - i, Color.Black);
                    }

                    //histPlotData[ptr] = (byte)((val < i) ? (byte)255 : 0);
                }

                for (int i = 0; i < height; i++)
                {
                    image.SetPixel(0, i, Color.Gray);
                }
            }

            return(image);
        }