/// <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; }