Skip to content

Commit

Permalink
Fixes errors from previous merge commits
Browse files Browse the repository at this point in the history
  • Loading branch information
atruskie committed Feb 19, 2020
1 parent 63e6299 commit 2c134cb
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 301 deletions.
7 changes: 6 additions & 1 deletion src/Acoustics.Shared/Drawing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Font GetArial(float size)
return SystemFonts.CreateFont(Arial, size);
}

public static Configuration NoParallelConfiguration => new Configuration() {
public static readonly Configuration NoParallelConfiguration = new Configuration() {
MaxDegreeOfParallelism = 1,
};

Expand All @@ -63,6 +63,11 @@ public static Size ToSize(this SizeF size)
return (Size)size;
}

public static Pen ToPen(this Color color, float width = 1)
{
return new Pen(color, width);
}

public static void DrawTextSafe(this IImageProcessingContext context, string text, Font font,
Color color, PointF location)
{
Expand Down
6 changes: 2 additions & 4 deletions src/AnalysisPrograms/AED.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public static Tuple<AcousticEvent[], AudioRecording, BaseSonogram> Detect(
var sonogram = (BaseSonogram)new SpectrogramStandard(config, recording.WavReader);

AcousticEvent[] events = CallAed(sonogram, aedConfiguration, segmentStartOffset, segmentDuration);
TowseyLibrary.Log.WriteIfVerbose("AED # events: " + events.Length);
Log.Debug("AED # events: " + events.Length);
return Tuple.Create(events, recording, sonogram);
}

Expand Down Expand Up @@ -216,8 +216,6 @@ public static Image DrawSonogram(BaseSonogram sonogram, IEnumerable<AcousticEven
public static void Execute(Arguments arguments)
{
MainEntry.WarnIfDeveloperEntryUsed();

TowseyLibrary.Log.Verbosity = 1;
string date = "# DATE AND TIME: " + DateTime.Now;
LoggedConsole.WriteLine("# Running acoustic event detection.");
LoggedConsole.WriteLine(date);
Expand Down Expand Up @@ -247,7 +245,7 @@ public static void Execute(Arguments arguments)
WriteEventsFileStatic(outputCsvPath, results.Item1);
Log.Info("CSV file saved to: " + outputCsvPath.FullName);

TowseyLibrary.Log.WriteLine("Finished");
Log.Success("Finished");
}

public override AnalysisResult2 Analyze<T>(AnalysisSettings analysisSettings, SegmentSettings<T> segmentSettings)
Expand Down
259 changes: 46 additions & 213 deletions src/AnalysisPrograms/Audio2Sonogram.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/AnalysisPrograms/DrawLongDurationSpectrograms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,8 @@ public static Image<Rgb24> DrawFalseColorSpectrograms(Arguments args, string fil
int trackHeight = 20;
var timeScale = ImageTrack.DrawTimeRelativeTrack(fullDuration, image1.Width, trackHeight);

colourMap = args.ColourMap2 ?? LDSpectrogramRGB.DefaultColorMap2;
var image2 = cs1.DrawFalseColorSpectrogramChromeless("NEGATIVE", colourMap);
colorMap = args.ColourMap2 ?? LDSpectrogramRGB.DefaultColorMap2;
var image2 = cs1.DrawFalseColorSpectrogramChromeless("NEGATIVE", colorMap, blueEnhanceParameter);
var list = new [] { titleImage, image1, timeScale, image2 };
var combinedImage = ImageTools.CombineImagesVertically(list);
return combinedImage;
Expand Down
2 changes: 2 additions & 0 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ namespace AnalysisPrograms.Recognizers
using AudioAnalysisTools.StandardSpectrograms;
using AudioAnalysisTools.WavTools;
using log4net;
using SixLabors.ImageSharp;
using TowseyLibrary;
using Path = System.IO.Path;

/// <summary>
/// This class calls algorithms for generic syllable/component types.
Expand Down
3 changes: 1 addition & 2 deletions src/AnalysisPrograms/Sandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AnalysisPrograms
using System.Threading.Tasks;
using AnalysisPrograms.Production.Arguments;
using McMaster.Extensions.CommandLineUtils;
using Log = TowseyLibrary.Log;
using Log = System.LoggedConsole;

/// <summary>
/// Activity Code for this class:= sandpit
Expand Down Expand Up @@ -40,7 +40,6 @@ public class Arguments : SubCommandBase
public override Task<int> Execute(CommandLineApplication app)
{
var tStart = DateTime.Now;
Log.Verbosity = 1;
Log.WriteLine("# Start Time = " + tStart.ToString(CultureInfo.InvariantCulture));

// CONTENT DESCRIPTION
Expand Down
1 change: 1 addition & 0 deletions src/AudioAnalysisTools/CrossCorrelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace AudioAnalysisTools
{
using System;
using Accord.Math;
using DSP;
using TowseyLibrary;

Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/DSP/NoiseRemoval_Briggs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public static Image<Rgb24> DrawSonogram(double[,] data, TimeSpan recordingDurati

var image = BaseSonogram.GetSonogramImage(data, minPercentile, maxPercentile);

Image titleBar = BaseSonogram.DrawTitleBarOfGrayScaleSpectrogram(title, image.Width);
var titleBar = BaseSonogram.DrawTitleBarOfGrayScaleSpectrogram(title, image.Width);
TimeSpan minuteOffset = TimeSpan.Zero;
TimeSpan labelInterval = TimeSpan.FromSeconds(5);
image = BaseSonogram.FrameSonogram(image, titleBar, minuteOffset, X_interval, xAxisPixelDuration, labelInterval, nyquist, herzInterval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace AudioAnalysisTools.LongDurationSpectrograms

using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.Primitives;

using TowseyLibrary;
using Path = System.IO.Path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ public void DrawNegativeFalseColorSpectrogram(DirectoryInfo outputDirectory, str
/// Chromeless means WITHOUT all the trimmings, such as title bar axis labels, grid lines etc.
/// However it does add in notated error segments.
/// </summary>
public Image<Rgb24> DrawFalseColorSpectrogramChromeless(string colorMode, string colorMap)
public Image<Rgb24> DrawFalseColorSpectrogramChromeless(string colorMode, string colorMap, double blueEnhanceParameter)
{
if (!this.ContainsMatrixForKeys(colorMap))
{
Expand Down Expand Up @@ -907,7 +907,7 @@ public static Image<Rgb24> DrawTitleBarOfFalseColourSpectrogram(string title, in
/// Black is a frequent color in LDFC spectrograms, but gray is highly unlikely,
/// and therefore its presence stands out as indicating an error in one or more of the rgb values.
/// </summary>
public static Image<Rgb24> DrawRgbColourMatrix(double[,] redM, double[,] grnM, double[,] bluM, bool doReverseColour, double blueEnhanceParameter)
public static Image<Rgb24> DrawRgbColorMatrix(double[,] redM, double[,] grnM, double[,] bluM, bool doReverseColor, double blueEnhanceParameter)
{
int rows = redM.GetLength(0); //number of rows
int cols = redM.GetLength(1); //number
Expand Down
24 changes: 10 additions & 14 deletions src/AudioAnalysisTools/StandardSpectrograms/BaseSonogram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ public static double[] GetAvSpectrum_HighestPercentile(double[,] matrix, int hig
/// <param name="frameStep">frame step allows correct time scale to be drawn.</param>
/// <param name="title">Descriptive title of the spectrogram.</param>
/// <returns>The framed spectrogram image.</returns>
public static Image GetImageAnnotatedWithLinearHertzScale(Image image, int sampleRate, int frameStep, string title)
public static Image<Rgb24> GetImageAnnotatedWithLinearHertzScale(Image<Rgb24> image, int sampleRate, int frameStep, string title)
{
var titleBar = DrawTitleBarOfGrayScaleSpectrogram(title, image.Width);
var startTime = TimeSpan.Zero;
Expand All @@ -711,27 +711,26 @@ public static Image GetImageAnnotatedWithLinearHertzScale(Image image, int sampl
/// This method draws only top and bottom time scales and adds the title bar.
/// It does NOT include the frequency grid lines.
/// </summary>
public static Image FrameSonogram(
Image sonogramImage,
Image titleBar,
public static Image<Rgb24> FrameSonogram(
Image<Rgb24> sonogramImage,
Image<Rgb24> titleBar,
TimeSpan minuteOffset,
TimeSpan xAxisTicInterval,
TimeSpan xAxisPixelDuration,
TimeSpan labelInterval)
{
int imageWidth = sonogramImage.Width;
var timeBmp = ImageTrack.DrawShortTimeTrack(minuteOffset, xAxisPixelDuration, xAxisTicInterval, labelInterval, imageWidth, "Seconds");
Image[] imageArray = { titleBar, timeBmp, sonogramImage, timeBmp };
return ImageTools.CombineImagesVertically(imageArray);
return ImageTools.CombineImagesVertically(titleBar, timeBmp, sonogramImage, timeBmp);
}

/// <summary>
/// This method assumes that the height of the passed sonogram image is half of the original frame size.
/// This assumption allows the frequency scale grid lines to be placed at the correct intervals.
/// </summary>
public static Image<Rgb24> FrameSonogram(
Image sonogramImage,
Image titleBar,
Image<Rgb24> sonogramImage,
Image<Rgb24> titleBar,
TimeSpan minuteOffset,
TimeSpan xAxisTicInterval,
TimeSpan xAxisPixelDuration,
Expand All @@ -749,17 +748,14 @@ public static Image<Rgb24> FrameSonogram(

int imageWidth = sonogramImage.Width;
var timeBmp = ImageTrack.DrawShortTimeTrack(minuteOffset, xAxisPixelDuration, xAxisTicInterval, labelInterval, imageWidth, "Seconds");
Image[] imageArray = { titleBar, timeBmp, sonogramImage, timeBmp };
return ImageTools.CombineImagesVertically(imageArray);
return ImageTools.CombineImagesVertically(titleBar, timeBmp, sonogramImage, timeBmp);
}

public static Image DrawTitleBarOfGrayScaleSpectrogram(string title, int width)
public static Image<Rgb24> DrawTitleBarOfGrayScaleSpectrogram(string title, int width)
{
var bmp = new Bitmap(width, SpectrogramConstants.HEIGHT_OF_TITLE_BAR);
var bmp = Drawing.NewImage(width, SpectrogramConstants.HEIGHT_OF_TITLE_BAR, Color.Black);
bmp.Mutate(g =>
{
g.Clear(Color.Black);

// var stringFont = Drawing.Tahoma9;
var stringFont = Drawing.Arial9;

Expand Down
84 changes: 26 additions & 58 deletions src/AudioAnalysisTools/StandardSpectrograms/ImageTrack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1358,40 +1358,13 @@ public static Image<Rgb24> DrawTimeRelativeTrack(TimeSpan fullDuration, int trac
int halfheight = trackHeight / 3;
for (int x = 1; x < cols; x++)
{
<<<<<<< HEAD
int tickPosition = x;
g.DrawLine(whitePen, tickPosition, 0, tickPosition, trackHeight);
TimeSpan elapsedTimeSpan = TimeSpan.FromMilliseconds(xAxisPixelDurationInMilliseconds * tickPosition);
if (xAxisPixelDurationInMilliseconds <= 1000)
{
<<<<<<< HEAD
time = String.Format("{0}", elapsedTimeSpan);
}
else if (xAxisPixelDurationInMilliseconds < 60000)
{
time = String.Format("{0:d2}{1:d2}", elapsedTimeSpan.Hours, elapsedTimeSpan.Minutes);
}
else
{
time = String.Format("{0:f0}", elapsedTimeSpan.TotalHours);
=======
time = $"{elapsedTimeSpan}";
}
else if (xAxisPixelDurationInMilliseconds < 60000)
=======
if (x % halfInterval == 0)
>>>>>>> Converting to .NET Core and ImageSharp
{
g.DrawLine(whitePen, x, 0, x, halfheight);
}

if (x % xPixelInterval == 0)
{
<<<<<<< HEAD
time = $"{elapsedTimeSpan.TotalHours:f0}";
>>>>>>> Fixing .NET Core incompatibilities
}
=======
int tickPosition = x;
g.DrawLine(whitePen, tickPosition, 0, tickPosition, trackHeight);
TimeSpan elapsedTimeSpan =
Expand All @@ -1408,7 +1381,6 @@ public static Image<Rgb24> DrawTimeRelativeTrack(TimeSpan fullDuration, int trac
{
time = $"{elapsedTimeSpan.TotalHours:f0}";
}
>>>>>>> Converting to .NET Core and ImageSharp

g.DrawText(time, stringFont, Color.White, new PointF(tickPosition, 2)); //draw time
}
Expand Down Expand Up @@ -1572,23 +1544,15 @@ public static byte[] GetXaxisTicLocations(int width, TimeSpan timeSpan)
return ba; //byte array
}

<<<<<<< HEAD
/// <summary>
/// Draws time track with labels to indicate hh:mm:ss.
/// </summary>
public static Bitmap DrawTimeTrack(TimeSpan duration, int width)
{
int height = HeightOfTimeScale;
Pen blackPen = new Pen(Color.Black);
Pen grayPen = new Pen(Color.DarkGray);
=======
public static Image<Rgb24> DrawTimeTrack(TimeSpan duration, int width)
{
int height = HeightOfTimeScale;
Pen blackPen = new Pen(Color.Black, 1);
Pen grayPen = new Pen(Color.DarkGray, 1);
var bgBrush = new SolidBrush(Color.FromRgb(240, 240, 240));
>>>>>>> Converting to .NET Core and ImageSharp

//DateTime start = new DateTime(0);
double secondsPerPixel = duration.TotalSeconds / width;
Expand Down Expand Up @@ -1661,44 +1625,48 @@ public static Image<Rgb24> DrawTimeTrack(TimeSpan duration, int width)
/// <summary>
/// This time track is labeled to be convenient for time durations around 1-20 minutes.
/// </summary>
public static Bitmap DrawShortTimeTrack(TimeSpan offsetMinute, TimeSpan xAxisPixelDuration, TimeSpan xAxisTicInterval, TimeSpan labelInterval, int trackWidth, string title)
public static Image<Rgb24> DrawShortTimeTrack(TimeSpan offsetMinute, TimeSpan xAxisPixelDuration, TimeSpan xAxisTicInterval, TimeSpan labelInterval, int trackWidth, string title)
{
int trackHeight = HeightOfTimeScale;
var bmp = new Bitmap(trackWidth, trackHeight);
var g = Graphics.FromImage(bmp);
g.Clear(Color.White);
var bmp = Drawing.NewImage(trackWidth, trackHeight, Color.White);



double elapsedTime = offsetMinute.TotalSeconds;
double pixelDuration = xAxisPixelDuration.TotalSeconds;
int labelSecondsInterval = (int)labelInterval.TotalSeconds;
var blackPen = new Pen(Color.Black);
var stringFont = new Font("Arial", 8);
var blackPen = Color.Black.ToPen();
var stringFont = Drawing.Arial8;

// for columns, draw in second lines
double xInterval = (int)(xAxisTicInterval.TotalMilliseconds / xAxisPixelDuration.TotalMilliseconds);

// for pixels in the line
for (int x = 1; x < trackWidth; x++)
bmp.Mutate(g =>
{
elapsedTime += pixelDuration;
if (x % xInterval <= pixelDuration)
// for pixels in the line
for (int x = 1; x < trackWidth; x++)
{
g.DrawLine(blackPen, x, 0, x, trackHeight);
int totalSeconds = (int)Math.Round(elapsedTime);
if (totalSeconds % labelSecondsInterval == 0)
elapsedTime += pixelDuration;
if (x % xInterval <= pixelDuration)
{
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
string time = $"{minutes}m{seconds}s";
g.DrawString(time, stringFont, Brushes.Black, new PointF(x + 1, 1)); //draw time
g.DrawLine(blackPen, x, 0, x, trackHeight);
int totalSeconds = (int)Math.Round(elapsedTime);
if (totalSeconds % labelSecondsInterval == 0)
{
int minutes = totalSeconds / 60;
int seconds = totalSeconds % 60;
string time = $"{minutes}m{seconds}s";
g.DrawText(time, stringFont, Color.Black, new PointF(x + 1, 1)); //draw time
}
}
}
}

g.DrawLine(blackPen, 0, 0, trackWidth, 0); //draw upper boundary
g.DrawLine(blackPen, 0, trackHeight - 1, trackWidth, trackHeight - 1); //draw lower boundary
g.DrawLine(blackPen, trackWidth, 0, trackWidth, trackHeight - 1); //draw right end boundary
g.DrawString(title, stringFont, Brushes.Black, new PointF(1, 1));
g.DrawLine(blackPen, 0, 0, trackWidth, 0); //draw upper boundary
g.DrawLine(blackPen, 0, trackHeight - 1, trackWidth, trackHeight - 1); //draw lower boundary
g.DrawLine(blackPen, trackWidth, 0, trackWidth, trackHeight - 1); //draw right end boundary
g.DrawText(title, stringFont, Color.Black, new PointF(1, 1));
});

return bmp;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ public void TestDrawRgbColorMatrix()
grnM[3, 3] = 0.01;
bluM[3, 3] = 0.11;

var image = (Image<Rgb24>)LDSpectrogramRGB.DrawRgbColourMatrix(redM, grnM, bluM, doReverseColour: true);
var image = (Image<Rgb24>)LDSpectrogramRGB.DrawRgbColorMatrix(redM, grnM, bluM, doReverseColor: true, 0.5);

Assert.That.PixelIsColor(new Point(1, 1), Color.FromRgb(128, 128, 128), image);
Assert.That.PixelIsColor(new Point(2, 2), Color.FromRgb(128, 128, 128), image);

// empty values are rendered as white because of `doReverseColour`
Assert.That.ImageRegionIsColor(Rectangle.FromLTRB(0,0, 1,5), Color.FromArgb(255, 255, 255), image);
Assert.That.ImageRegionIsColor(Rectangle.FromLTRB(4,0, 5,5), Color.FromArgb(255, 255, 255), image);
Assert.That.ImageRegionIsColor(Rectangle.FromLTRB(0,0, 1,5), Color.FromRgb(255, 255, 255), image);
Assert.That.ImageRegionIsColor(Rectangle.FromLTRB(4,0, 5,5), Color.FromRgb(255, 255, 255), image);
}
}
}

0 comments on commit 2c134cb

Please sign in to comment.